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:
http://jinja.pocoo.org/docs/2.10/templates/#defined
So you can simply use:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if my_var is defined and my_var %} | |
defined | |
{% endif %} |
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if my_var is defined and my_var | bool %} | |
defined | |
{% endif %} |