Score:1

Ansible & Jenkins

ss flag

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

br flag
Do you want `date_exp=` to be recognized as `False`?
Wanexa avatar
ss flag
false or empty, whatever
Score:1
br flag

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
Wanexa avatar
ss flag
already tried this one too, don't work
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.