Quiz Entry - updated: 2026.11.12
How do you write a complex condition that mixes and and or so it is read correctly and stays readable?
Group each alternative in parentheses, and use YAML's folded block > to spread the long expression over several lines.
The condition "RHEL 9 or Fedora 34":
when: >
( ansible_facts['distribution'] == "RedHat" and
ansible_facts['distribution_major_version'] == "9" )
or
( ansible_facts['distribution'] == "Fedora" and
ansible_facts['distribution_major_version'] == "34" )
Two mechanisms are at work:
- Parentheses make the grouping explicit: each bracket pins one distribution to its own version. Without them it is easy to write something like
distribution == "RedHat" and version == "9" or distribution == "Fedora", which accepts every Fedora version. >(folded block scalar) is YAML, not Ansible: it joins the following indented lines into one string, replacing the line breaks with spaces. Ansible receives a single long expression, while the file stays readable.
Go deeper:
YAML syntax (official docs) — folded
>and literal|block values.Conditionals based on Ansible facts (official docs) — grouped conditions with parentheses.