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.
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