Ansible : Schleifen realisieren

Der Eintrag dient mir hauptsächlich als Gedächtnisstütze, braucht man nicht jeden Tag :-)

Eine einfache Schleife mit einem Counter von 1 bis 4 kann man so realisieren.
- hosts: localhost
  tasks:
    - name: loop test
      debug:
        msg: "{{ item }}"
      with_sequence: count=4
Liefert dann
TASK [loop test] ***************************************************************************************************
ok: [localhost] => (item=1) => {
    "msg": "1"
}
ok: [localhost] => (item=2) => {
    "msg": "2"
}
ok: [localhost] => (item=3) => {
    "msg": "3"
}
ok: [localhost] => (item=4) => {
    "msg": "4"
}

Verschachtelte Schleifen mit 2 dics kann man so realisieren (in diesem Beispiel wurden Benutzer die Rechte auf MySQL Datenbanken eingetragen)
- hosts: localhost
  tasks:
    - name: loop test
      debug:
        msg: "name {{ item[0] }} db : {{ item[1] }}.*:ALL"
      with_nested:
        - [ 'user01', 'user02' ]
        - [ 'db01', 'db02', 'db03' ]
Liefert dann
TASK [loop test] ***************************************************************************************************
ok: [localhost] => (item=[u'user01', u'db01']) => {
    "msg": "name user01 db : db01.*:ALL"
}
ok: [localhost] => (item=[u'user01', u'db02']) => {
    "msg": "name user01 db : db02.*:ALL"
}
ok: [localhost] => (item=[u'user01', u'db03']) => {
    "msg": "name user01 db : db03.*:ALL"
}
ok: [localhost] => (item=[u'user02', u'db01']) => {
    "msg": "name user02 db : db01.*:ALL"
}
ok: [localhost] => (item=[u'user02', u'db02']) => {
    "msg": "name user02 db : db02.*:ALL"
}
ok: [localhost] => (item=[u'user02', u'db03']) => {
    "msg": "name user02 db : db03.*:ALL"
}

Jetzt gibt es noch die Möglichkeit mit inner und outer loop zu arbeiten. Gestartet wird dann das playbook outer_loop.yaml

outer_loop.yaml
- hosts: localhost
  tasks:
    - include_tasks: inner_loop.yaml
      with_sequence: count=4
      loop_control:
        loop_var: IDX
inner_loop.yaml
- debug:
    msg: "create directory /opt/{{ item}}-{{ IDX }}"
  with_items:
    - dic1
    - dic2
    - dic3
Das liefert dann
TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=dic1) => {
    "msg": "create directory /opt/dic1-1"
}
ok: [localhost] => (item=dic2) => {
    "msg": "create directory /opt/dic2-1"
}
ok: [localhost] => (item=dic3) => {
    "msg": "create directory /opt/dic3-1"
}

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=dic1) => {
    "msg": "create directory /opt/dic1-2"
}
ok: [localhost] => (item=dic2) => {
    "msg": "create directory /opt/dic2-2"
}
ok: [localhost] => (item=dic3) => {
    "msg": "create directory /opt/dic3-2"
}

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=dic1) => {
    "msg": "create directory /opt/dic1-3"
}
ok: [localhost] => (item=dic2) => {
    "msg": "create directory /opt/dic2-3"
}
ok: [localhost] => (item=dic3) => {
    "msg": "create directory /opt/dic3-3"
}

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=dic1) => {
    "msg": "create directory /opt/dic1-4"
}
ok: [localhost] => (item=dic2) => {
    "msg": "create directory /opt/dic2-4"
}
ok: [localhost] => (item=dic3) => {
    "msg": "create directory /opt/dic3-4"
}

Quellen :
Ansible Documentation - Loops
Ansible Documentation - Complex Loops
“Sicher ist, dass nichts sicher ist. Selbst das nicht.”
Joachim Ringelnatz