49

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.

Simply Seth
  • 3,246
  • 17
  • 51
  • 77

5 Answers5

87

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
techraf
  • 64,883
  • 27
  • 193
  • 198
  • This seems to be the correct way to use vars in when block. I'm using this logic on my ansible playbook and it's always being executing, with false or true values. Do you know why is not working? – eduardosufan Jun 24 '19 at 21:19
  • 7
    In Ansible 2.8 and later use destroy|bool to evaluate boolean variable. – vitro Jun 28 '19 at 19:00
27

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.

Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31
Henrik Pingel
  • 3,083
  • 1
  • 19
  • 27
5

I have been struggling on this issue too. My problem was, that the variable was not declared inside the script but was given as a extra parameter. In this case you need to convert to bool explicitly

when: destroy|bool

or

when: not destroy|bool
1

In a similar problem that I ran into, I was trying to include_role under my tasks. The roles were to be triggered based on a boolean condition. The below syntax worked for me:

tasks:
- name: trigger role 
  include_role:
    name: "role_path_here"
  when:
  - call_the_role|bool

call_the_role variable was not declared in this file. Since my problem was more specific to using include_role I took help/ reference from https://github.com/ansible/ansible/issues/18124

-12

The include kept happening before the when.

So I just made the include dynamic.

---- defaults/main.yml
mode: "create"

---- tasks/main.yml
- include: "{{ mode + '.yml' }}"
Simply Seth
  • 3,246
  • 17
  • 51
  • 77
  • 3
    I'm new to ansible and browsing to learn, can someone at least explain why this answer was being downvoted? – Will Feb 24 '19 at 03:10
  • 1
    Because it's not clear how does this solve the original issue. The author should have been more explicit as how and why this brings resolution to the original question. – Yoanis Gil Oct 06 '20 at 15:20