Conditionals based on variables
You can also create conditionals based on variables defined in the playbooks or inventory. Because conditionals require boolean input (a test must evaluate as True to trigger the condition), you must apply the|boolfilter to non-boolean variables, such as string variables with content like ‘yes’, ‘on’, ‘1’, or ‘true’. You can define variables like this:
With the variables above, Ansible would run one of these tasks and skip the other:
If a required variable has not been set, you can skip or fail using Jinja2’sdefinedtest. For example:
This is especially useful in combination with the conditional import ofvarsfiles (see below).
As the examples show, you do not need to use{{ }}to use variables inside conditionals, as these are already implied.