Interview Prep/Ansible/Jinja2, Conditionals and Loops

What is the difference between `loop` and the older `with_*` keywords?

IntermediateConceptJinja2, Conditionals and Loops
Answer

loop is the modern, uniform keyword that takes a list. with_items, with_dict, with_fileglob and friends are the older forms, implemented as lookups and with subtly different flattening behaviour: with_items flattens one level of nested lists, loop does not.

- ansible.builtin.user: { name: "{{ item }}", state: present }
  loop: [alice, bob]
- ansible.builtin.user: { name: "{{ item }}" }
  with_items: [alice, bob]     # legacy equivalent

Convert with loop plus a filter (dict2items, flatten) when migrating.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops