Skip to content

Loops

Official Ansible Loop

Loop named

YAML
    loop: "{{ CfgConfigZonning.list }}"
    loop_control:
      loop_var: data

Nested Loop

YAML
---
- hosts: localhost
  gather_facts: False

  tasks:
  - name: Loop nested
    debug:
      msg: "{{ item[0] }} ::  {{ item[1] }}"
    with_nested:
      - [ 'alice', 'bob' ]
      - [ 'clientdb', 'employeedb', 'providerdb' ]
...

Boucle for

YAML
---
- hosts: localhost
  gather_facts: False

  tasks:

  - name: Transform la variable list[0] en integer
    set_fact:
      firstSeqList: "{{ list[0] | int }}

  - name: init pair / inpaire : InitRange = 0 / 1
    set_fact:
      initRange: "{{ firstSeqList | int % 2 }}"

  - name: boucle for de 2 en 2 : range([start, ]stop[, step])
    debug:
      msg:
        - "{{ item }}"
    loop: "{{ range( initRange | int , lastSeqList | int + 1, 2 ) | list }}"
...

Double loop Ansible

Double loop Ansible
- name: Loop 2
  hosts: localhost
  gather_facts: false
  vars:
    objs:
      - key1: "directory1"
        key2:
          - "file11"
          - "file12"
      - key1: "directory2"
        key2:
          - "file21"
          - "file22"
  tasks:
    - name: Create directories
      ansible.builtin.debug:
        msg: "Create directories : {{ item.key1 }}"
      with_items: "{{ objs }}"

    - name: 1 Create files
      ansible.builtin.debug:
        msg: "Create files : {{ item.0.key1 }}/{{ item.1 }}"
      with_subelements:
        - "{{ objs }}"
        - key2

    - name: 2 Create files
      ansible.builtin.debug:
        msg: "Create files : {{ item.0.key1 }}/{{ item.1 }}"
      loop: "{{ objs | subelements('key2') }}"
TASK [1 Create files] *******************************************
ok: [localhost] => (item=[{'key1': 'directory1'}, 'file11']) => {
    "msg": "Create files : directory1/file11"
}
ok: [localhost] => (item=[{'key1': 'directory1'}, 'file12']) => {
    "msg": "Create files : directory1/file12"
}
ok: [localhost] => (item=[{'key1': 'directory2'}, 'file21']) => {
    "msg": "Create files : directory2/file21"
}
ok: [localhost] => (item=[{'key1': 'directory2'}, 'file22']) => {
    "msg": "Create files : directory2/file22"
}

TASK [2 Create files] *************************************************************************
ok: [localhost] => (item=[{'key1': 'directory1', 'key2': ['file11', 'file12']}, 'file11']) => {
    "msg": "Create files : directory1/file11"
}
ok: [localhost] => (item=[{'key1': 'directory1', 'key2': ['file11', 'file12']}, 'file12']) => {
    "msg": "Create files : directory1/file12"
}
ok: [localhost] => (item=[{'key1': 'directory2', 'key2': ['file21', 'file22']}, 'file21']) => {
    "msg": "Create files : directory2/file21"
}
ok: [localhost] => (item=[{'key1': 'directory2', 'key2': ['file21', 'file22']}, 'file22']) => {
    "msg": "Create files : directory2/file22"
}

Until and loop

Migrating from with_X to loop

with_list

with_items

with_indexed_items

with_flattened

with_together

with_dict

with_sequence

with_subelements

with_nested/with_cartesian

with_random_choice