I'm running into the silliest issue. I cannot figure out how to test for boolean in an Ansible 2.2 task file.
In vars/main.yml
, I have:
destroy: false
In the playbook, I have:
roles: - {'role': 'vmdeploy','destroy': true}
In the task file, I have the following:
- include: "create.yml" when: "{{ destroy|bool }} == 'false'"
I've 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 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 would skip the include.
Ansible supports conditional evaluations before executing a specific task on the target hosts. If the set condition is true, Ansible will go ahead and perform the task. If the condition is not true (unmet), Ansible will skip the specified task.
The when clause is a raw Jinja2 expression without double curly braces (see group_by_module). When you run the task or playbook, Ansible evaluates the test for all hosts. On any host where the test passes (returns a value of True), Ansible runs that task.
Defining multiple when conditions in Ansible I want to reboot Debian or Ubuntu Linux system after kernel update, and the inventory hostname must be aws-proxy-server . If both conditions are true, then issue the reboot command using the Ansible reboot module. Otherwise, skip the reboot option.
There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers. C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.
To run a task when destroy
is true
:
--- - hosts: localhost connection: local vars: destroy: true tasks: - debug: when: destroy
and when destroy
is false
:
--- - hosts: localhost connection: local vars: destroy: false tasks: - debug: when: not destroy
There is no need to use the bool
Jinja filter if the value of the variable is defined under hostvars
.
To cast values as certain types, such as when you input a string as “True” from a vars_prompt and the system doesn’t know it is a boolean value.
So a simple
when: not destroy
should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With