Using True False with Ansible When Clause

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.