Using Loops and Items
Loops are used to iterate over a list of items. This is useful if a module does not provide this function.
Using loops
The yum module provides a list option, but the service module doesn’t:
A loop is defined at the same level as the module. And items in the loop can be accessed by using the system internal variable item.
The module is activated again for each item in the loop.
Loops and variables
For flexibility, you can define a variable as a list and use that variable in a loop instead:
Loops and multivalued variables
Say you have a list variable with multiple values in each item:
You can call the variable in each item like this:
This does not work with dictionaries. For that you’ll need to use the dict2items filter. See the docs.
with_items
This is the old way to iterate over a list of items. Uses with_keyword* statement. Keyword is replaced with the name of an Ansible look-up plug-in.
with_items is used like loop. with_file is used to iterate over a list of filenames on the control node. with_sequence is used to generate a list of values based on a numeric sequence.