How does `when` interact with `loop`?
IntermediateConceptJinja2, Conditionals and Loops
Answer
The condition is evaluated separately for each item, so item is usable in it, and items for which it is false are skipped individually.
- ansible.builtin.debug: msg="{{ item.name }}"
loop: "{{ users }}"
when: item.active
The registered result still contains an entry for each skipped item. Filtering the list in loop: itself (selectattr) is cleaner and gives shorter output.
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 does `when` work and which operators can you use?Beginner