ansible Ansible: Looping Nested loops


Example

You can create nested loops using with_nested.

from vars:

keys:
  - key1
  - key2
  - key3
  - key4

then the loop:

- name: Distribute SSH keys among multiple users
  lineinfile: dest=/home/{{ item[0] }}/.ssh/authorized_keys line={{ item[1] }} state=present
  with_nested:
    - [ 'calvin', 'josh', 'alice' ]
    - '{{ keys }}'

This task will loop over each user and populate their authorized_keys file with the 4 keys defined in the list.