Monday, December 10, 2018

Ansible and Jinja2: Check if variable is defined and it's True

Jinja2 provides you with a built in test:

http://jinja.pocoo.org/docs/2.10/templates/#defined
So you can simply use:

However, if you're using Ansible variables, even thought you might have defined some of them as False they might still be treated as True as Jinja2 may simply see them as string variables.
So you should, and even encouraged to use the built-in bool filter. So the final and the safest answer is this:

Ansible and Jinja2: Check if variable is defined and it's True

Jinja2 provides you with a built in test: http://jinja.pocoo.org/docs/2.10/templates/#defined So you can simply use: However, if you...