FreeKB - Ansible loop parameter (loop through items)
Ansible - loop parameter (loop through items)

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 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']"
}

 


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.

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |