Using True False with Ansible When Clause

I ran into the stupidest problem. I cannot figure out how to test boolean in Ansible 2.2 task file.

In vars/main.yml , I have:

 destroy: false 

The textbook has:

 roles: - {'role': 'vmdeploy','destroy': true} 

In the task file, I have the following:

 - include: "create.yml" when: "{{ destroy|bool }} == 'false'" 

I tried various combinations below:

 when: "{{ destroy|bool }} == false" when: "{{ destroy|bool }} == 'false'" when: "{{ destroy|bool == false}}" when: "{{ destroy == false}}" when: "{{ destroy == 'false'}}" when: destroy|bool == false when: destroy|bool == 'false' when: not destroy|bool 

In all of the above cases, I still get:

 statically included: .../vmdeploy/tasks/create.yml 

Debug output:

 - debug: msg: "{{ destroy }}" --- ok: [atlcicd009] => { "msg": true } 

The desired result is that it skips include.

+7
boolean ansible ansible-playbook
source share
3 answers

The inclusion continued until when.

So, I just turned on include dynamics.

 ---- defaults/main.yml mode: "create" ---- tasks/main.yml - include: "{{ mode + '.yml' }}" 
-one
source share

To complete the task if destroy is true :

 --- - hosts: localhost connection: local vars: destroy: true tasks: - debug: when: destroy 

and when destroy false :

 --- - hosts: localhost connection: local vars: destroy: false tasks: - debug: when: not destroy 
+10
source share

There is no need to use the bool filter Jinja filter when the value of a variable is defined under hostvars.

To distinguish values ​​as specific types, for example, when you enter a string as "True" from vars_prompt, and the system does not know that it is a boolean.

So simple

 when: not destroy 

should do the trick.

+1
source share

All Articles