The following parameters can be used to loop through an array / dictionary / 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, the debug module and with_items parameter are used to loop over an array of fruit (a fruit loop!).
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 items
debug:
var: item
with_items:
- apple
- banana
- orange
- grapes
Which should return the following.
TASK [loop through items]
ok: [server1.example.com] => (item=apple) => {
"item": "apple"
}
ok: [server1.example.com] => (item=banana) => {
"item": "banana"
}
ok: [server1.example.com] => (item=orange) => {
"item": "orange"
}
ok: [server1.example.com] => (item=grapes) => {
"item": "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:
var: item
with_items:
- [ apple, banana, orange, grapes ]
However, the loop parameter . . .
- name: loop through the 'fruit' array
debug:
var: item
loop:
- [ apple, banana, orange, grapes ]
would produce the following.
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
The register parameter is store the standard output (stdout), standard error (stderr) and return code (rc) of a command. In this example, the stdout/stderr/rc of the ps command will be stored in a variable named "out".
- name: ps command
shell: ps
register: out
Here is how you would loop over the standard out of the ps command.
- name: loop through the ps command
debug:
var: item
with_items: "{{ out.stdout_lines }}"
Be aware that sometimes register will have JSON that contains the results array. Notice in this example that the results array contains two items, foo and bar.
ok: [localhost] => {
"out": {
"results": [
"stdout": "foo",
"stdout": "bar"
]
]
}
}
In this scenario, you would need to loop through the results array.
- name: loop through the ps command
debug:
var: item.stdout
with_items: "{{ ps.results }}"
key value pairs
In this example, the loop parameter is used to loop over the food hash.
- name: loop through the 'food' hash
debug:
var: item
with_items:
- { key: 'fruit', value: 'banana' }
- { key: 'fruit', value: 'orange' }
- { key: 'veggy', value: 'onion' }
- { key: 'veggy', value: 'pepper' }
Which should return the following. Refer to Ansible - Loop through a hash for more details on looping over a hash.
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"
}
}