Bootstrap FreeKB - Ansible - Loop through list using the loop parameter
Ansible - Loop through list using the loop parameter

Updated:   |  Ansible articles

The following parameters can be used to loop through each item in an array / list.

In this example, the debug module and loop parameter are used to loop over an array of fruit (a fruit loop!). Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

The loop parameters will, by default, use the {{ item }} variable for each item in the list. Or, the loop_control parameter with loop_var can be used to define your own variable for each item in the list.

- name: "loop through the 'fruit' array"
  debug: 
    msg: "{{ item }}"
  loop:
    - apple
    - banana
    - orange
    - grapes

 

Which should return the following.

TASK [loop through the 'fruit' array] 
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"
}

 

Better yet, the array can be defined. Here is how you would create an array of fruit using the vars plugin.

vars:
  fruit: 
    - apple
    - banana
    - orange
    - grapes

 

And here is one way to loop through a JSON array.

- name: loop through the 'fruit' array
  debug: 
    var: item
  loop: "{{ fruit }}"

 


with_items vs loop

Be aware that there are differences between the with_items and loop parameters. For example, the following would produce the same output as above.

- name: "loop through the 'fruit' array"
  debug: 
    msg: "{{ item }}"
  with_items:
    - [ apple, banana, orange, grapes ]

 

However, the loop parameter . . .

- name: "loop through the 'fruit' array"
  debug: 
    msg: "{{ item }}"
  loop:
    - [ apple, banana, orange, grapes ]

 

would produce the following. Note that some or all of the output can be suppressed or limited using the no_log or loop_control parameters.

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

 


index_of

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

---
- hosts: localhost
  gather_facts: false
  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"
}

 


Register parameter

Let's say you are storing a list in the register module, like this. In this example, the contents of the /tmp directory on the managed mode (e.g. target system) will be stored in a variable named "out". 

- name: list command
  shell: "ls /tmp"
  register: out

 

Here is how to loop through the list and print the output using the debug module.

- name: tmp directory loop
  debug:
    msg: "{{ item }}"
  loop: "{{ out.stdout_lines }}"

 

Running this playbook will return something like this, which is the contents of the /tmp directory.

TASK [tmp directory loop]
ok: [server1.example.com] => (item=foo) => {"msg": "foo"}
ok: [server1.example.com] => (item=bar) => {"msg": "bar"}

 


Hash (key value pairs)

In this example, the loop parameter is used to loop over the food hash.

- name: "loop through the 'food' hash"
  debug: 
    msg: "{{ item }}"
  loop:
    - { key: 'fruit', value: 'banana' }
    - { key: 'fruit', value: 'orange' }
    - { key: 'veggy', value: 'onion' }
    - { key: 'veggy', value: 'pepper' }

 

Which should return the following.

TASK [loop through the 'food' hash] 
ok: [server1.example.com] => (item=apple) => {
    "msg": {
        "key": "fruit",
        "value": "apple"
    }
}
ok: [server1.example.com] => (item={'key': 'fruit', 'value': 'banana'}) => {
    "msg": {
        "key": "fruit",
        "value": "banana"
    }
}
ok: [server1.example.com] => (item={'key': 'veggy', 'value': 'onion'}) => {
    "msg": {
        "key": "veggy",
        "value": "onion"
    }
}
ok: [server1.example.com] => (item={'key': 'veggy', 'value': 'pepper'}) => {
    "msg": {
        "key": "veggy",
        "value": "pepper"
    }
}

 

Refer to Ansible - Loop through a hash for more details on looping over a hash.

 




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 f92c8e in the box below so that we can be sure you are a human.