+

Search Tips   |   Advanced Search

Loops

Common Ansible loops include changing ownership on several files and/or directories with the file module, creating multiple users with the user module, and repeating a polling step until a certain result is reached. Ansible offers two keywords for creating loops: loop and with_<lookup>.


Comparing loop and with_*

you would need:

it's cleaner to keep:


Standard loops

Iterating over a simple list

Repeated tasks can be written as standard loops over a simple list of strings. You can define the list directly in the task:

You can define the list in a variables file, or in the 'vars' section of your play, then refer to the name of the list in the task:

Either of these examples would be the equivalent of:

You can pass a list directly to a parameter for some plugins. Most of the packaging modules, like yum and apt, have this capability. When available, passing the list to a parameter is better than looping over the task. For example:

Check the module documentation to see if you can pass a list to any particular module's parameter(s).


Iterating over a list of hashes

If you have a list of hashes, you can reference subkeys in a loop. For example:

When combining conditionals with a loop, the when: statement is processed separately for each item. See Basic conditionals with when for examples.


Iterating over a dictionary

To loop over a dict, use the dict2items:

Here, we are iterating over tag_data and printing the key and the value from it.


Registering variables with a loop

You can register the output of a loop as a variable. For example:

When you use register with a loop, the data structure placed in the variable will contain a results attribute that is a list of all responses from the module. This differs from the data structure returned when using register without a loop:

Subsequent loops over the registered variable to inspect the results may look like:

During iteration, the result of the current item will be placed in the variable:


Complex loops

Iterating over nested lists

You can use Jinja2 expressions to iterate over complex lists. For example, a loop can combine nested lists:


Retrying a task until a condition is met

You can use the until keyword to retry a task until a certain condition is met. Here's an example:

This task runs up to 5 times with a delay of 10 seconds between each attempt. If the result of any attempt has 'all systems go' in its stdout, the task succeeds. The default value for 'retries' is 3 and 'delay' is 5.

To see the results of individual retries, run the play with -vv.

When you run a task with until and register the result as a variable, the registered variable will include a key called 'attempts', which records the number of the retries for the task.

You must set the until parameter if you want a task to retry. If until is not defined, the value for the retries parameter is forced to 1.


Looping over inventory

To loop over your inventory, or just a subset of it, you can use a regular loop with the ansible_play_batch or groups variables:

There is also a specific lookup plugin inventory_hostnames that can be used like this:

More information on the patterns can be found in Patterns: targeting hosts and groups.


Ensuring list input for loop: using query rather than lookup

The loop keyword requires a list as input, but the lookup keyword returns a string of comma-separated values by default. Ansible 2.5 introduced a new Jinja2 function named query that always returns a list, offering a simpler interface and more predictable output from lookup plugins when using the loop keyword.

You can force lookup to return a list to loop by using wantlist=True, or you can use query instead.

These examples do the same thing:


Adding controls to loops

The loop_control keyword lets you manage your loops in useful ways.


Limiting loop output with label

When looping over complex data structures, the console output of your task can be enormous. To limit the displayed output, use the label directive with loop_control:

The output of this task will display just the name field for each item instead of the entire contents of the multi-line {{ item }} variable.

This is for making console output more readable, not protecting sensitive data. If there is sensitive data in loop, set no_log: yes on the task to prevent disclosure.


Pausing within a loop

To control the time (in seconds) between the execution of each item in a task loop, use the pause directive with loop_control:


Tracking progress through a loop with index_var

To keep track of where you are in a loop, use the index_var directive with loop_control. This directive specifies a variable name to contain the current loop index:

index_var is 0 indexed.


Defining inner and outer variable names with loop_var

You can nest two looping tasks using include_tasks. However, by default Ansible sets the loop variable item for each loop. This means the inner, nested loop will overwrite the value of item from the outer loop. You can specify the name of the variable for each loop using loop_var with loop_control:

If Ansible detects that the current loop is using a variable which has already been defined, it will raise an error to fail the task.


Extended loop variables

As of Ansible 2.8 you can get extended loop information using the extended option to loop control. This option will expose the following information.

Variable Description
ansible_loop.allitems The list of all items in the loop
ansible_loop.index The current iteration of the loop. (1 indexed)
ansible_loop.index0 The current iteration of the loop. (0 indexed)
ansible_loop.revindex The number of iterations from the end of the loop (1 indexed)
ansible_loop.revindex0 The number of iterations from the end of the loop (0 indexed)
ansible_loop.first True if first iteration
ansible_loop.last True if last iteration
ansible_loop.length The number of items in the loop
ansible_loop.previtem The item from the previous iteration of the loop. Undefined during the first iteration.
ansible_loop.nextitem The item from the following iteration of the loop. Undefined during the last iteration.


Accessing the name of your loop_var

As of Ansible 2.8 you can get the name of the value provided to loop_control.loop_var using the ansible_loop_var variable

For role authors, writing roles that allow loops, instead of dictating the required loop_var value, you can gather the value via:


Migrating from with_X to loop

In most cases, loops work best with the loop keyword instead of with_X style loops. The loop syntax is usually best expressed using filters instead of more complex use of query or lookup.

These examples show how to convert many common with_ style loops to loop and filters.


with_list

with_list is directly replaced by loop.


with_items

with_items is replaced by loop and the flatten filter.


with_indexed_items

with_indexed_items is replaced by loop, the flatten filter and loop_control.index_var.


with_flattened

with_flattened is replaced by loop and the flatten filter.


with_together

with_together is replaced by loop and the zip filter.

Another example with complex data


with_dict

with_dict can be substituted by loop and either the dictsort or dict.items filters.


with_sequence

with_sequence is replaced by loop and the range function, and potentially the format filter.


with_subelements

with_subelements is replaced by loop and the subelements filter.


with_nested/with_cartesian

with_nested and with_cartesian are replaced by loop and the product filter.


with_random_choice

with_random_choice is replaced by just use of the random filter, without need of loop.


See also

Next Previous