In old-style Ansible, I used to use with_items together with dictionaries. To given potential examples:
- name: deploy files
template:
src: "files/{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- {src: 'foo', dest: "/path/to/somewhere"}
- {src: 'bar', dest: "/somewhere/else"}
- {src: 'baz', dest: "/different/path/"}
- name: Install packages
npm:
name: '{{ item.name }}'
version: '{{ item.version }}'
with_items:
- {src: 'foo', version: '1.0'}
- {src: 'bar', version: '1.5'}
- {src: 'baz', version: '1.2'}
These days we are supposed to use loop. if trying as a drop-in replacement, this would be like
- name: deploy files
template:
src: "files/{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- {src: 'foo', dest: "/path/to/somewhere"}
- {src: 'bar', dest: "/somewhere/else"}
- {src: 'baz', dest: "/different/path/"}
wihch fails with
TASK [deploy files] ************
fatal: [host]: FAILED! => {
"msg": "'src_path' is undefined"
}
Indeed, the migration guide (and StackOverflow answers such as Ansible: iterate over a list of dictionaries - loop vs. with_items) do say and boil down to “use flatten” for dictionaries. But this assumes that the list of dicts is stored in a variable. But what if it is not as it is defined in-line? Do I just have to move the data to a named variable?
EDIT: for an attempt with drop-in replacement.