Ansible - Loop

> Infrastructure as code > Ansible

1 - About

3 - Management

3.1 - Nested

loop in Ansible are never nested but you can combine list

- name: give users access to multiple databases
  mysql_user:
    name: "{{ item[0] }}"
    priv: "{{ item[1] }}.*:ALL"
    append_privs: yes
    password: "foo"
  loop: "{{ ['alice', 'bob'] |product(['clientdb', 'employeedb', 'providerdb'])|list }}"
Advertising

3.2 - With Items

- name: Install mapr-core
  vars:
    packages_Suse: ['mapr-compat-suse', 'mapr-core', 'mapr-core-internal']
    packages_RedHat: ['mapr-core', 'mapr-core-internal']
    packages_Debian: ['mapr-core', 'mapr-core-internal']
  package: name={{ item }} state=present
  with_items: "{{ vars['packages_' + ansible_os_family] }}"
  register: core_result

4 - Documentation / Reference