Bootstrap FreeKB - Ansible - Looping through a List
Ansible - Looping through a List

Updated:   |  Ansible articles

The following parameters can be used to loop through a list.

IMPORTANT

When possible, Ansible recommends using the loop parameter, as the loop parameter is meant to supercede the with_items option.

 

In this example, an list of fruit is creating using the vars plugin and the debug module is used to output the entire list.

---
- hosts: localhost
  vars:
    fruit: [ apple, banana, orange, grapes ]
  tasks:
  - name: output the contents of the 'fruit' list
    debug: 
      var: fruit
...

 

Which should return the following. Notice the [ ] character, which means this is an list. The values are wrapped in double quotes, meaning the values are strings.

TASK [output the contents of the 'fruit' list]
ok: [localhost] => {
    "fruit": [
        "apple",
        "banana",
        "orange",
        "grapes"
    ]
}

 

In this example, the debug module and loop parameter are used to loop over an list of fruit (a fruit loop!).

---
- hosts: localhost
  vars:
    fruit: [ apple, banana, orange, grapes ]
  tasks:
  - name: loop through the 'fruit' list
    debug: 
      msg: "{{ item }}"
    loop: "{{ fruit }}"
...

 

Which should return the following.

TASK [loop through the 'fruit' list] 
ok: [server1.example.com] => (item=apple) => {
    "msg": "apple"
}
ok: [server1.example.com] => (item=banana) => {
    "msg": "banana"
}
ok: [server1.example.com] => (item=orange) => {
    "msg": "orange"
}
ok: [server1.example.com] => (item=grapes) => {
    "msg": "grapes"
}

 

Or the index number of an item in the list can be used to only return a specific item.

---
- hosts: localhost
  vars:
    fruit: [ apple, banana, orange, grapes ]
  tasks:
  - name: print item.1 in the 'fruit' list
    debug: 
      msg: "{{ item.1 }}"
...

 

Which should return the following.

ok: [server1.example.com] => (item=banana) => {
    "msg": "banana"
}

 

Or, the index_of module can be used to get the index number of an element in a list, to avoid looping through each element in the list, which can be useful in reducing the amount of output returned when looping through a large list.

---
- hosts: localhost
  vars:
    fruit: [ apple, banana, orange, grapes ]
  tasks:
  - debug:
      msg: "banana index number = {{ lookup('ansible.utils.index_of', fruit, 'regex', 'banana') }}"
...

 

Running this playbook should return the following.

ok: [localhost] => {
    "msg": "banana index number = 1"
}

 

Often, you will find yourself in a situation where JSON formatted data is being stored in a variable. In this example, the find module is being used to store the files below the /tmp directory in a variable named "out". Assuming the /tmp directory contains two (or more) files or directories, you will want to loop over the "out" variable, like this.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: display the contents of the 'out' variable
    debug:
      msg: "{{ item }}"
    with_items: "{{ out.files }}"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9603c3 in the box below so that we can be sure you are a human.