1

here is my problem

I run a playbook on jenkins with extra variables, I want to test in my playbook is variables are defined

ansible [core 2.12.1] , jenkins plugin 1.1

here is the command from jenkins :

ansible-playbook create_user.yml -i hosts -e Site=UK -e BU=Test -e date_exp=

my code for testing is below

- name: Set expiration date 
  when : not date_exp
  set_fact:
   date_exp: "01/01/2024"

- name: Expiration date
  debug:
   msg:
    - "{{ date_exp}}"

tried so many things: not , is undefined , =="" nothing works

2
  • Do you want date_exp= to be recognized as False? Commented Oct 15, 2022 at 8:50
  • false or empty, whatever
    – Wanexa
    Commented Oct 15, 2022 at 8:51

1 Answer 1

1

Test the length of the string

  when: date_exp|length == 0

Example of a complete playbook for testing

shell> cat pb.yml
- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        var: date_exp
    - debug:
        var: date_exp|type_debug
    - debug:
        msg: var is empty
      when: date_exp|length == 0
shell> ansible-playbook pb.yml -e date_exp=

PLAY [localhost] ******************************************************************************

TASK [debug] **********************************************************************************
ok: [localhost] => 
  date_exp: ''

TASK [debug] **********************************************************************************
ok: [localhost] => 
  date_exp|type_debug: str

TASK [debug] **********************************************************************************
ok: [localhost] => 
  msg: var is empty

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell> ansible-playbook pb.yml -e date_exp=xxx

PLAY [localhost] ******************************************************************************

TASK [debug] **********************************************************************************
ok: [localhost] => 
  date_exp: xxx

TASK [debug] **********************************************************************************
ok: [localhost] => 
  date_exp|type_debug: str

TASK [debug] **********************************************************************************
skipping: [localhost]

PLAY RECAP ************************************************************************************
localhost: ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
1
  • already tried this one too, don't work
    – Wanexa
    Commented Oct 15, 2022 at 9:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .