Interview Prep/Ansible/Jinja2, Conditionals and Loops

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.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops