Interview Prep/Ansible/Jinja2, Conditionals and Loops

How does `when` work and which operators can you use?

BeginnerConceptJinja2, Conditionals and Loops
Answer

when takes a raw Jinja2 expression, written without braces, evaluated per host. If it is false the task is skipped.

- ansible.builtin.dnf: { name: httpd }
  when: ansible_facts['os_family'] == 'RedHat'
- ansible.builtin.debug: msg="big box"
  when: ansible_facts['memtotal_mb'] > 8192
- ansible.builtin.debug: msg="prod"
  when: env in ['prod', 'dr'] and not maintenance | bool

Operators: ==, !=, >, <, in, not, and, or, and tests like is defined.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops