LOGBOOK

HELP

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:

  1. 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.
  2. > (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:

From Quiz: ITIA / Ansible Task Control: Loops, Conditionals, Handlers and Error Handling | Updated: Nov 12, 2026