How are booleans and strings treated in conditions?
IntermediateConceptJinja2, Conditionals and Loops
Answer
Values set from YAML (true, false) are real booleans, but values from -e key=value, shell output or INI inventories are strings. A string like "false" is not a false value, so a condition on it may be wrong, and recent ansible-core releases (2.19+) raise an error for non-boolean conditionals instead of guessing.
Be explicit:
when: deploy_enabled | bool
when: result.stdout | length > 0
when: mylist | length > 0 # not: when: mylist
bool converts yes/true/1/on (case-insensitive) to true.
More on Jinja2, Conditionals and Loops
What role does Jinja2 play in Ansible?BeginnerWhat are the three Jinja2 delimiters?BeginnerShow a template with a loop and a conditional, and how it is deployed.BeginnerWhich filters do you use every day?BeginnerHow do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?IntermediateHow does `when` work and which operators can you use?Beginner