How does with_items differ from loop, and why does the difference matter when you convert old playbooks?
For a simple list they behave the same; but with_items silently flattens a list of lists one level, while loop does not.
Before Ansible 2.5, loops were written with with_ plus the name of a lookup plugin, and with_items is the one you will meet most:
vars:
data:
- user0
- user1
- user2
tasks:
- name: "with_items"
ansible.builtin.debug:
msg: "{{ item }}"
with_items: "{{ data }}"
For this list, loop: "{{ data }}" gives exactly the same three iterations. The trap is nested input. Given [1, [2, 3], 4], with_items flattens it and iterates four times (1, 2, 3, 4), while loop iterates three times and the second item is the list [2, 3]. To get the old behaviour with loop, flatten explicitly:
loop: "{{ [1, [2, 3], 4] | flatten(1) }}"
So a mechanical search-and-replace of with_items by loop is safe only when you know the list is flat.
Go deeper:
Migrating from with_X to loop: with_items (official docs) — the flattening difference and the
flatten(1)fix.Comparing loop and with_* (official docs) — when each one is the right tool.