Ansible nested loop

Ansible's syntax also supports the idea of nested looping. Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task.

Now we can create nested loops using with_nested.
#cat nested.yaml
---
- hosts: linux
  tasks:
  - name: print nested loop
    debug: msg=" ansible {{ item[0] }} on day {{ item[1] }} is {{ item[2] }}"
    with_nested:
    - [ 'training', 'lab', 'handson' ]
    - [ 1, 2, 3 ]
    - [ 'good', 'bad', 'great' ]
To run playbook,
# ansible-playbook nested.yaml
PLAY [all] **************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.1.2]

TASK [print nested loop] ************************************************************************************************************************************
ok: [192.168.1.2] => (item=[u'training', 1, u'good']) => {
    "msg": " ansible training on day 1 is good"
}
ok: [192.168.1.2] => (item=[u'training', 1, u'bad']) => {
    "msg": " ansible training on day 1 is bad"
}
ok: [192.168.1.2] => (item=[u'training', 1, u'great']) => {
    "msg": " ansible training on day 1 is great"
}
ok: [192.168.1.2] => (item=[u'training', 2, u'good']) => {
    "msg": " ansible training on day 2 is good"
}
ok: [192.168.1.2] => (item=[u'training', 2, u'bad']) => {
    "msg": " ansible training on day 2 is bad"
}
ok: [192.168.1.2] => (item=[u'training', 2, u'great']) => {
    "msg": " ansible training on day 2 is great"
}
ok: [192.168.1.2] => (item=[u'training', 3, u'good']) => {
    "msg": " ansible training on day 3 is good"
}
ok: [192.168.1.2] => (item=[u'training', 3, u'bad']) => {
    "msg": " ansible training on day 3 is bad"
}
ok: [192.168.1.2] => (item=[u'training', 3, u'great']) => {
    "msg": " ansible training on day 3 is great"
}
ok: [192.168.1.2] => (item=[u'lab', 1, u'good']) => {
    "msg": " ansible lab on day 1 is good"
}
ok: [192.168.1.2] => (item=[u'lab', 1, u'bad']) => {
    "msg": " ansible lab on day 1 is bad"
}
ok: [192.168.1.2] => (item=[u'lab', 1, u'great']) => {
    "msg": " ansible lab on day 1 is great"
}
ok: [192.168.1.2] => (item=[u'lab', 2, u'good']) => {
    "msg": " ansible lab on day 2 is good"
}
ok: [192.168.1.2] => (item=[u'lab', 2, u'bad']) => {
    "msg": " ansible lab on day 2 is bad"
}
ok: [192.168.1.2] => (item=[u'lab', 2, u'great']) => {
    "msg": " ansible lab on day 2 is great"
}
ok: [192.168.1.2] => (item=[u'lab', 3, u'good']) => {
    "msg": " ansible lab on day 3 is good"
}
ok: [192.168.1.2] => (item=[u'lab', 3, u'bad']) => {
    "msg": " ansible lab on day 3 is bad"
}
ok: [192.168.1.2] => (item=[u'lab', 3, u'great']) => {
    "msg": " ansible lab on day 3 is great"
}
ok: [192.168.1.2] => (item=[u'handson', 1, u'good']) => {
    "msg": " ansible handson on day 1 is good"
}
ok: [192.168.1.2] => (item=[u'handson', 1, u'bad']) => {
    "msg": " ansible handson on day 1 is bad"
}
ok: [192.168.1.2] => (item=[u'handson', 1, u'great']) => {
    "msg": " ansible handson on day 1 is great"
}
ok: [192.168.1.2] => (item=[u'handson', 2, u'good']) => {
    "msg": " ansible handson on day 2 is good"
}
ok: [192.168.1.2] => (item=[u'handson', 2, u'bad']) => {
    "msg": " ansible handson on day 2 is bad"
}
ok: [192.168.1.2] => (item=[u'handson', 2, u'great']) => {
    "msg": " ansible handson on day 2 is great"
}
ok: [192.168.1.2] => (item=[u'handson', 3, u'good']) => {
    "msg": " ansible handson on day 3 is good"
}
ok: [192.168.1.2] => (item=[u'handson', 3, u'bad']) => {
    "msg": " ansible handson on day 3 is bad"
}
ok: [192.168.1.2] => (item=[u'handson', 3, u'great']) => {
    "msg": " ansible handson on day 3 is great"
}

PLAY RECAP **************************************************************************************************************************************************
192.168.1.2                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0




Recent Comments

No comments

Leave a Comment