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

Wanexa
  • 71
  • 1
  • 1
  • 2

1 Answers1

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
Vladimir Botka
  • 5,138
  • 8
  • 20