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.
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 do you combine several conditions?Beginner