Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using True False with Ansible When Clause

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.

like image 697
Simply Seth Avatar asked Sep 22 '16 13:09

Simply Seth


People also ask

When condition is true in Ansible?

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.

What is when condition in Ansible?

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.

Can we define multiple conditions in Ansible?

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.

What is true and false in bool?

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.


2 Answers

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 
like image 113
techraf Avatar answered Oct 10 '22 23:10

techraf


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.

like image 44
Henrik Pingel Avatar answered Oct 11 '22 00:10

Henrik Pingel