Interview Prep/Ansible/Jinja2, Conditionals and Loops

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.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops