2013-09-29 21:02:53 +00:00
Loops
=====
2013-09-25 05:18:43 +00:00
2013-10-05 17:50:53 +00:00
Often you'll want to do many things in one task, such as create a lot of users, install a lot of packages, or
repeat a polling step until a certain result is reached.
This chapter is all about how to use loops in playbooks.
2013-09-25 04:26:14 +00:00
2013-12-26 19:32:01 +00:00
.. contents :: Topics
2013-10-04 22:34:39 +00:00
.. _standard_loops:
2013-09-25 04:26:14 +00:00
2013-09-29 21:02:53 +00:00
Standard Loops
`` ` ` ` ` ` ` ` ` ` ` ``
2012-05-13 15:00:02 +00:00
To save some typing, repeated tasks can be written in short-hand like so::
2013-03-02 16:47:43 +00:00
- name: add several users
2017-03-06 22:39:07 +00:00
user:
name: "{{ item }}"
state: present
groups: "wheel"
2012-05-13 15:00:02 +00:00
with_items:
- testuser1
- testuser2
2012-08-01 02:19:04 +00:00
If you have defined a YAML list in a variables file, or the 'vars' section, you can also do::
2016-04-28 13:18:32 +00:00
with_items: "{{ somelist }}"
2012-08-01 02:19:04 +00:00
2012-05-13 15:00:02 +00:00
The above would be the equivalent of::
- name: add user testuser1
2017-03-06 22:39:07 +00:00
user:
name: "testuser1"
state: present
groups: "wheel"
2012-05-13 15:00:02 +00:00
- name: add user testuser2
2017-03-06 22:39:07 +00:00
user:
name: "testuser2"
state: present
groups: "wheel"
2012-05-13 15:00:02 +00:00
2012-10-08 11:38:21 +00:00
The yum and apt modules use with_items to execute fewer package manager transactions.
2012-05-13 15:00:02 +00:00
2012-10-16 23:12:31 +00:00
Note that the types of items you iterate over with 'with_items' do not have to be simple lists of strings.
2012-10-16 22:58:31 +00:00
If you have a list of hashes, you can reference subkeys using things like::
2013-05-29 22:05:14 +00:00
- name: add several users
2017-03-06 22:39:07 +00:00
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
2013-05-29 22:05:14 +00:00
with_items:
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }
2012-10-16 22:58:31 +00:00
2015-03-06 21:35:49 +00:00
Also be aware that when combining `when` with `with_items` (or any other loop statement), the `when` statement is processed separately for each item. See :ref: `the_when_statement` for an example.
2016-09-23 17:04:32 +00:00
Loops are actually a combination of things `with_` + `lookup()` , so any lookup plugin can be used as a source for a loop, 'items' is lookup.
2013-10-04 22:34:39 +00:00
.. _nested_loops:
2013-07-15 04:28:02 +00:00
Nested Loops
`` ` ` ` ` ` ` ` ` ``
2013-07-19 13:31:50 +00:00
Loops can be nested as well::
2013-07-15 04:28:02 +00:00
2013-07-19 13:13:30 +00:00
- name: give users access to multiple databases
2017-03-06 22:39:07 +00:00
mysql_user:
name: "{{ item[0] }}"
priv: "{{ item[1] }}.*:ALL"
append_privs: yes
password: "foo"
2013-07-15 04:28:02 +00:00
with_nested:
2014-06-04 00:45:03 +00:00
- [ 'alice', 'bob' ]
2013-07-15 04:28:02 +00:00
- [ 'clientdb', 'employeedb', 'providerdb' ]
2015-06-03 15:15:13 +00:00
As with the case of 'with_items' above, you can use previously defined variables.::
2013-07-15 04:28:02 +00:00
- name: here, 'users' contains the above list of employees
2017-03-06 22:39:07 +00:00
mysql_user:
name: "{{ item[0] }}"
priv: "{{ item[1] }}.*:ALL"
append_privs: yes
password: "foo"
2013-07-15 04:28:02 +00:00
with_nested:
2016-04-28 13:18:32 +00:00
- "{{ users }}"
2013-07-15 04:28:02 +00:00
- [ 'clientdb', 'employeedb', 'providerdb' ]
2014-02-11 22:58:51 +00:00
.. _looping_over_hashes:
Looping over Hashes
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 1.5
Suppose you have the following variable::
---
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
And you want to print every user's name and phone number. You can loop through the elements of a hash using `` with_dict `` like this::
tasks:
- name: Print phone records
2017-03-06 22:39:07 +00:00
debug:
msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
2017-03-16 14:13:22 +00:00
with_dict: "{{ users }}"
2014-02-11 22:58:51 +00:00
2013-10-04 22:34:39 +00:00
.. _looping_over_fileglobs:
2015-10-28 20:45:12 +00:00
Looping over Files
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2016-01-20 02:20:10 +00:00
`` with_file `` iterates over the content of a list of files, `item` will be set to the content of each file in sequence. It can be used like this::
2015-10-28 20:45:12 +00:00
---
- hosts: all
tasks:
# emit a debug message containing the content of each file.
- debug:
2016-04-28 13:18:32 +00:00
msg: "{{ item }}"
2015-10-28 20:45:12 +00:00
with_file:
- first_example_file
- second_example_file
2017-01-07 19:38:52 +00:00
Assuming that `` first_example_file `` contained the text "hello" and `` second_example_file `` contained the text "world", this would result in:
.. code-block :: shell-session
2015-10-28 20:45:12 +00:00
2016-04-28 13:18:32 +00:00
TASK [debug msg={{ item }}] ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *** *
2015-10-28 20:45:12 +00:00
ok: [localhost] => (item=hello) => {
2016-09-30 14:01:56 +00:00
"item": "hello",
2015-10-28 20:45:12 +00:00
"msg": "hello"
}
ok: [localhost] => (item=world) => {
2016-09-30 14:01:56 +00:00
"item": "world",
2015-10-28 20:45:12 +00:00
"msg": "world"
}
2013-09-29 21:02:53 +00:00
Looping over Fileglobs
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-10-16 22:51:10 +00:00
2016-08-15 13:38:27 +00:00
`` with_fileglob `` matches all files in a single directory, non-recursively, that match a pattern. It calls
`Python's glob library <https://docs.python.org/2/library/glob.html> `_ , and can be used like this::
2012-10-16 22:51:10 +00:00
2013-05-31 08:54:13 +00:00
---
2012-10-16 22:51:10 +00:00
- hosts: all
tasks:
# first ensure our target directory exists
2017-03-06 22:39:07 +00:00
- name: Ensure target directory exists
file:
dest: "/etc/fooapp"
state: directory
2012-10-16 22:51:10 +00:00
# copy each file over that matches the given pattern
2017-03-06 22:39:07 +00:00
- name: Copy each file over that matches the given pattern
copy:
src: "{{ item }}"
dest: "/etc/fooapp/"
owner: "root"
2017-04-27 08:17:38 +00:00
mode: 0600
2013-02-17 17:47:51 +00:00
with_fileglob:
2017-03-06 22:39:07 +00:00
- "/playbooks/files/fooapp/*"
2015-06-03 15:15:13 +00:00
2014-04-23 21:52:32 +00:00
.. note :: When using a relative path with `` with_fileglob `` in a role, Ansible resolves the path relative to the `roles/<rolename>/files` directory.
2013-02-02 16:51:25 +00:00
2013-09-29 21:02:53 +00:00
Looping over Parallel Sets of Data
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-06-14 15:13:38 +00:00
2013-10-13 12:57:10 +00:00
.. note :: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
2013-10-04 22:34:39 +00:00
Suppose you have the following variable data was loaded in via somewhere::
---
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]
And you want the set of '(a, 1)' and '(b, 2)' and so on. Use 'with_together' to get this::
tasks:
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item.0 }} and {{ item.1 }}"
2013-10-04 22:34:39 +00:00
with_together:
2016-04-28 13:18:32 +00:00
- "{{ alpha }}"
- "{{ numbers }}"
2013-02-02 16:51:25 +00:00
2013-09-29 21:02:53 +00:00
Looping over Subelements
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-02-02 17:19:21 +00:00
2013-10-04 22:34:39 +00:00
Suppose you want to do something like loop over a list of users, creating them, and allowing them to login by a certain set of
2015-06-03 15:15:13 +00:00
SSH keys.
2013-10-04 22:34:39 +00:00
How might that be accomplished? Let's assume you had the following defined and loaded in via "vars_files" or maybe a "group_vars/all" file::
---
users:
- name: alice
2015-06-03 15:15:13 +00:00
authorized:
2013-10-04 22:34:39 +00:00
- /tmp/alice/onekey.pub
- /tmp/alice/twokey.pub
2015-05-12 17:51:35 +00:00
mysql:
password: mysql-password
hosts:
- "%"
- "127.0.0.1"
- "::1"
- "localhost"
privs:
- "*.* :SELECT"
- "DB1.*:ALL"
2013-10-04 22:34:39 +00:00
- name: bob
authorized:
- /tmp/bob/id_rsa.pub
2015-05-12 17:51:35 +00:00
mysql:
password: other-mysql-password
hosts:
- "db1"
privs:
- "*.* :SELECT"
- "DB2.*:ALL"
2013-10-04 22:34:39 +00:00
It might happen like so::
2017-03-06 22:39:07 +00:00
- name: Create User
user:
name: "{{ item.name }}"
state: present
generate_ssh_key: yes
with_items:
- "{{ users }}"
2013-02-02 17:56:56 +00:00
2017-03-06 22:39:07 +00:00
- name: Set authorized ssh key
authorized_key:
user: "{{ item.0.name }}"
key: "{{ lookup('file', item.1) }}"
2013-10-04 22:34:39 +00:00
with_subelements:
2016-03-02 12:12:30 +00:00
- "{{ users }}"
2013-10-04 22:34:39 +00:00
- authorized
2015-05-12 17:51:35 +00:00
Given the mysql hosts and privs subkey lists, you can also iterate over a list in a nested subkey::
- name: Setup MySQL users
2017-03-06 22:39:07 +00:00
mysql_user:
name: "{{ item.0.name }}"
password: "{{ item.0.mysql.password }}"
host: "{{ item.1 }}"
priv: "{{ item.0.mysql.privs | join('/') }}"
2015-05-12 17:51:35 +00:00
with_subelements:
2016-03-02 12:12:30 +00:00
- "{{ users }}"
2017-03-06 22:39:07 +00:00
- "{{ mysql.hosts }}"
2015-05-12 17:51:35 +00:00
Subelements walks a list of hashes (aka dictionaries) and then traverses a list with a given (nested sub-)key inside of those
2013-10-04 22:34:39 +00:00
records.
2015-05-12 17:51:35 +00:00
Optionally, you can add a third element to the subelements list, that holds a
dictionary of flags. Currently you can add the 'skip_missing' flag. If set to
True, the lookup plugin will skip the lists items that do not contain the given
subkey. Without this flag, or if that flag is set to False, the plugin will
yield an error and complain about the missing subkey.
2013-10-04 22:34:39 +00:00
The authorized_key pattern is exactly where it comes up most.
.. _looping_over_integer_sequences:
2013-02-02 17:19:21 +00:00
2013-09-29 21:02:53 +00:00
Looping over Integer Sequences
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-01-08 20:13:25 +00:00
2017-03-29 23:11:19 +00:00
`` with_sequence `` generates a sequence of items. You
can specify a start value, an end value, an optional "stride" value that specifies the number of steps to increment the sequence, and an optional printf-style format string.
2013-01-08 20:13:25 +00:00
2017-03-29 23:11:19 +00:00
Arguments should be specified as key=value pair strings.
A simple shortcut form of the arguments string is also accepted: `` [start-]end[/stride][:format] `` .
2013-02-02 16:51:25 +00:00
2013-02-17 17:47:51 +00:00
Numerical values can be specified in decimal, hexadecimal (0x3f8) or octal (0600).
2013-02-02 16:51:25 +00:00
Negative numbers are not supported. This works as follows::
---
2013-01-08 20:13:25 +00:00
- hosts: all
tasks:
# create groups
2017-03-06 22:39:07 +00:00
- group:
name: "evens"
state: present
- group:
name: "odds"
state: present
2013-01-08 20:13:25 +00:00
2013-04-12 22:21:09 +00:00
# create some test users
2017-03-06 22:39:07 +00:00
- user:
name: "{{ item }}"
state: present
groups: "evens"
2017-03-29 23:11:19 +00:00
with_sequence: start=0 end=32 format=testuser%02x
2013-01-08 20:13:25 +00:00
2013-04-12 22:21:09 +00:00
# create a series of directories with even numbers for some reason
2017-03-06 22:39:07 +00:00
- file:
dest: "/var/stuff/{{ item }}"
state: directory
2017-03-29 23:11:19 +00:00
with_sequence: start=4 end=16 stride=2
2013-01-08 20:13:25 +00:00
2013-02-02 16:51:25 +00:00
# a simpler way to use the sequence plugin
2013-01-08 20:13:25 +00:00
# create 4 groups
2017-03-06 22:39:07 +00:00
- group:
name: "group{{ item }}"
state: present
2017-03-29 23:11:19 +00:00
with_sequence: count=4
2013-01-08 20:13:25 +00:00
2013-10-04 22:34:39 +00:00
.. _random_choice:
Random Choices
`` ` ` ` ` ` ` ` ` ` ` ``
2013-10-12 12:34:41 +00:00
The 'random_choice' feature can be used to pick something at random. While it's not a load balancer (there are modules
2016-10-21 13:58:09 +00:00
for those), it can somewhat be used as a poor man's load balancer in a MacGyver like situation::
2013-10-04 22:34:39 +00:00
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
2013-10-04 22:34:39 +00:00
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
2016-09-30 14:01:56 +00:00
One of the provided strings will be selected at random.
2013-10-04 22:34:39 +00:00
At a more basic level, they can be used to add chaos and excitement to otherwise predictable automation environments.
.. _do_until_loops:
2013-09-29 21:02:53 +00:00
Do-Until Loops
`` ` ` ` ` ` ` ` ` ` ` ``
2013-09-03 04:08:00 +00:00
2016-09-30 14:01:56 +00:00
.. versionadded :: 1.4
2013-10-14 12:24:20 +00:00
2013-09-29 21:02:53 +00:00
Sometimes you would want to retry a task until a certain condition is met. Here's an example::
2016-09-30 14:01:56 +00:00
2017-04-27 08:30:45 +00:00
- shell: /usr/bin/foo
2013-09-29 21:02:53 +00:00
register: result
2013-10-03 20:28:29 +00:00
until: result.stdout.find("all systems go") != -1
2013-09-29 21:02:53 +00:00
retries: 5
delay: 10
2013-08-12 13:49:34 +00:00
2014-05-03 15:59:50 +00:00
The above example run the shell module recursively till the module's result has "all systems go" in its stdout or the task has
2013-09-29 21:02:53 +00:00
been retried for 5 times with a delay of 10 seconds. The default value for "retries" is 3 and "delay" is 5.
2012-08-01 02:19:04 +00:00
2013-09-29 21:02:53 +00:00
The task returns the results returned by the last task run. The results of individual retries can be viewed by -vv option.
The registered variable will also have a new key "attempts" which will have the number of the retries for the task.
2012-08-01 02:19:04 +00:00
2013-10-13 12:54:36 +00:00
.. _with_first_found:
Finding First Matched Files
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-13 12:57:10 +00:00
.. note :: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
2013-10-13 12:54:36 +00:00
This isn't exactly a loop, but it's close. What if you want to use a reference to a file based on the first file found
that matches a given criteria, and some of the filenames are determined by variable names? Yes, you can do that as follows::
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
2017-03-06 22:39:07 +00:00
template:
src: "{{ item }}"
dest: "/etc/foo.conf"
2013-10-13 12:54:36 +00:00
with_first_found:
2016-04-28 13:18:32 +00:00
- "{{ ansible_virtualization_type }}_foo.conf"
2013-10-13 12:54:36 +00:00
- "default_foo.conf"
This tool also has a long form version that allows for configurable search paths. Here's an example::
- name: some configuration template
2017-03-06 22:39:07 +00:00
template:
src: "{{ item }}"
dest: "/etc/file.cfg"
mode: 0444
owner: "root"
group: "root"
2013-10-13 12:54:36 +00:00
with_first_found:
- files:
2016-04-28 13:18:32 +00:00
- "{{ inventory_hostname }}/etc/file.cfg"
2013-10-13 12:54:36 +00:00
paths:
- ../../../templates.overwrites
- ../../../templates
- files:
- etc/file.cfg
paths:
- templates
.. _looping_over_the_results_of_a_program_execution:
Iterating Over The Results of a Program Execution
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-13 12:57:10 +00:00
.. note :: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
2013-10-13 12:54:36 +00:00
Sometimes you might want to execute a program, and based on the output of that program, loop over the results of that line by line.
2016-04-25 16:16:28 +00:00
Ansible provides a neat way to do that, though you should remember, this is always executed on the control machine, not the remote
2013-10-13 12:54:36 +00:00
machine::
- name: Example of looping over a command result
2017-03-06 22:39:07 +00:00
shell: "/usr/bin/frobnicate {{ item }}"
with_lines:
- "/usr/bin/frobnications_per_host --param {{ inventory_hostname }}"
2013-10-13 12:54:36 +00:00
Ok, that was a bit arbitrary. In fact, if you're doing something that is inventory related you might just want to write a dynamic
inventory source instead (see :doc: `intro_dynamic_inventory` ), but this can be occasionally useful in quick-and-dirty implementations.
Should you ever need to execute a command remotely, you would not use the above method. Instead do this::
- name: Example of looping over a REMOTE command result
2017-03-06 22:39:07 +00:00
shell: "/usr/bin/something"
2013-10-13 12:54:36 +00:00
register: command_result
- name: Do something with each result
2017-03-06 22:39:07 +00:00
shell: "/usr/bin/something_else --param {{ item }}"
with_items:
- "{{ command_result.stdout_lines }}"
2013-10-13 12:54:36 +00:00
.. _indexed_lists:
Looping Over A List With An Index
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-13 12:57:10 +00:00
.. note :: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
2016-09-30 14:01:56 +00:00
.. versionadded :: 1.3
2013-10-13 12:54:36 +00:00
If you want to loop over an array and also get the numeric index of where you are in the array as you go, you can also do that.
It's uncommonly used::
- name: indexed loop demo
2017-03-06 22:39:07 +00:00
debug:
msg: "at array position {{ item.0 }} there is a value {{ item.1 }}"
with_indexed_items:
- "{{ some_list }}"
2013-10-13 12:54:36 +00:00
2015-08-05 09:09:40 +00:00
.. _using_ini_with_a_loop:
Using ini file with a loop
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2016-09-30 14:01:56 +00:00
.. versionadded :: 2.0
2015-08-05 09:09:40 +00:00
2017-01-07 19:38:52 +00:00
The ini plugin can use regexp to retrieve a set of keys. As a consequence, we can loop over this set. Here is the ini file we'll use:
.. code-block :: ini
2015-08-05 09:09:40 +00:00
[section1]
value1=section1/value1
value2=section1/value2
[section2]
value1=section2/value1
value2=section2/value2
Here is an example of using `` with_ini `` ::
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
with_ini:
- value[1-2]
- section: section1
- file: "lookup.ini"
- re: true
2015-08-05 09:09:40 +00:00
And here is the returned value::
{
2016-09-30 14:01:56 +00:00
"changed": false,
"msg": "All items completed",
2015-08-05 09:09:40 +00:00
"results": [
{
"invocation": {
2016-09-30 14:01:56 +00:00
"module_args": "msg=\"section1/value1\"",
2015-08-05 09:09:40 +00:00
"module_name": "debug"
2016-09-30 14:01:56 +00:00
},
"item": "section1/value1",
"msg": "section1/value1",
2015-08-05 09:09:40 +00:00
"verbose_always": true
2016-09-30 14:01:56 +00:00
},
2015-08-05 09:09:40 +00:00
{
"invocation": {
2016-09-30 14:01:56 +00:00
"module_args": "msg=\"section1/value2\"",
2015-08-05 09:09:40 +00:00
"module_name": "debug"
2016-09-30 14:01:56 +00:00
},
"item": "section1/value2",
"msg": "section1/value2",
2015-08-05 09:09:40 +00:00
"verbose_always": true
}
]
}
2013-10-13 12:54:36 +00:00
.. _flattening_a_list:
Flattening A List
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-13 12:57:10 +00:00
.. note :: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
2013-10-13 12:54:36 +00:00
In rare instances you might have several lists of lists, and you just want to iterate over every item in all of those lists. Assume
a really crazy hypothetical datastructure::
----
# file: roles/foo/vars/main.yml
packages_base:
- [ 'foo-package', 'bar-package' ]
packages_apps:
- [ ['one-package', 'two-package' ]]
- [ ['red-package'], ['blue-package']]
As you can see the formatting of packages in these lists is all over the place. How can we install all of the packages in both lists?::
- name: flattened loop demo
2017-03-06 22:39:07 +00:00
yum:
name: "{{ item }}"
state: present
2013-10-13 12:54:36 +00:00
with_flattened:
2016-04-28 13:18:32 +00:00
- "{{ packages_base }}"
- "{{ packages_apps }}"
2013-10-13 12:54:36 +00:00
That's how!
2013-12-20 17:37:38 +00:00
.. _using_register_with_a_loop:
2013-10-13 12:54:36 +00:00
2013-12-19 18:11:00 +00:00
Using register with a loop
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2017-02-02 23:02:28 +00:00
After using `` 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.
2013-12-19 18:11:00 +00:00
2013-12-20 17:37:38 +00:00
Here is an example of using `` register `` with `` with_items `` ::
2013-12-19 18:11:00 +00:00
2017-03-06 22:39:07 +00:00
- shell: "echo {{ item }}"
2013-12-19 18:11:00 +00:00
with_items:
2017-03-06 22:39:07 +00:00
- "one"
- "two"
2013-12-19 18:11:00 +00:00
register: echo
2014-01-09 00:17:36 +00:00
This differs from the data structure returned when using `` register `` without a loop::
2013-12-19 18:11:00 +00:00
{
"changed": true,
"msg": "All items completed",
"results": [
{
"changed": true,
"cmd": "echo \"one\" ",
"delta": "0:00:00.003110",
"end": "2013-12-19 12:00:05.187153",
"invocation": {
"module_args": "echo \"one\"",
"module_name": "shell"
},
"item": "one",
"rc": 0,
"start": "2013-12-19 12:00:05.184043",
"stderr": "",
"stdout": "one"
},
{
"changed": true,
"cmd": "echo \"two\" ",
"delta": "0:00:00.002920",
"end": "2013-12-19 12:00:05.245502",
"invocation": {
"module_args": "echo \"two\"",
"module_name": "shell"
},
"item": "two",
"rc": 0,
"start": "2013-12-19 12:00:05.242582",
"stderr": "",
"stdout": "two"
}
]
}
Subsequent loops over the registered variable to inspect the results may look like::
- name: Fail if return code is not 0
fail:
msg: "The command ({{ item.cmd }}) did not have a 0 return code"
when: item.rc != 0
2016-04-28 13:18:32 +00:00
with_items: "{{ echo.results }}"
2013-12-19 18:11:00 +00:00
2017-02-28 20:34:39 +00:00
During iteration, the result of the current item will be placed in the variable::
2017-02-02 23:02:28 +00:00
- shell: echo "{{ item }}"
with_items:
- one
- two
register: echo
changed_when: echo.stdout != "one"
2015-10-23 14:31:57 +00:00
2015-12-01 23:21:10 +00:00
.. _looping_over_the_inventory:
Looping over the inventory
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
If you wish to loop over the inventory, or just a subset of it, there is multiple ways.
One can use a regular `` with_items `` with the `` play_hosts `` or `` groups `` variables, like this::
# show all the hosts in the inventory
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
with_items:
- "{{ groups['all'] }}"
2015-12-01 23:21:10 +00:00
# show all the hosts in the current play
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
with_items:
- "{{ play_hosts }}"
2015-12-01 23:21:10 +00:00
2016-04-28 13:18:32 +00:00
There is also a specific lookup plugin `` inventory_hostnames `` that can be used like this::
2015-12-01 23:21:10 +00:00
# show all the hosts in the inventory
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
with_inventory_hostnames:
- all
2015-12-01 23:21:10 +00:00
# show all the hosts matching the pattern, ie all but the group www
2017-03-06 22:39:07 +00:00
- debug:
msg: "{{ item }}"
with_inventory_hostnames:
- all:!www
2015-12-01 23:21:10 +00:00
More information on the patterns can be found on :doc: `intro_patterns`
2015-10-23 07:27:09 +00:00
.. _loop_control:
2015-10-23 14:31:57 +00:00
2015-10-23 07:27:09 +00:00
Loop Control
`` ` ` ` ` ` ` ` ` ``
2015-10-23 14:31:57 +00:00
2016-09-30 14:01:56 +00:00
.. versionadded :: 2.1
2015-10-23 14:31:57 +00:00
2015-10-23 07:27:09 +00:00
In 2.0 you are again able to use `with_` loops and task includes (but not playbook includes). This adds the ability to loop over the set of tasks in one shot.
Ansible by default sets the loop variable `item` for each loop, which causes these nested loops to overwrite the value of `item` from the "outer" loops.
As of Ansible 2.1, the `loop_control` option can be used to specify the name of the variable to be used for the loop::
2015-10-23 14:31:57 +00:00
2015-10-23 07:27:09 +00:00
# main.yml
2016-05-16 13:48:54 +00:00
- include: inner.yml
2015-10-23 14:31:57 +00:00
with_items:
- 1
- 2
- 3
2015-10-23 07:27:09 +00:00
loop_control:
loop_var: outer_item
# inner.yml
2017-03-06 22:39:07 +00:00
- debug:
msg: "outer item={{ outer_item }} inner item={{ item }}"
2015-10-23 07:27:09 +00:00
with_items:
- a
- b
- c
2015-10-23 14:31:57 +00:00
2015-10-23 07:27:09 +00:00
.. note :: 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.
2015-10-23 14:31:57 +00:00
2016-09-30 14:01:56 +00:00
.. versionadded :: 2.2
When using complex data structures for looping the display might get a bit too "busy", this is where the C(label) directive comes to help::
2016-08-31 20:09:37 +00:00
- name: create servers
2017-03-06 22:39:07 +00:00
digital_ocean:
name: "{{ item.name }}"
state: present
2016-09-30 14:01:56 +00:00
with_items:
2016-08-31 20:09:37 +00:00
- name: server1
disks: 3gb
ram: 15Gb
2016-10-21 13:58:09 +00:00
network:
2016-08-31 20:09:37 +00:00
nic01: 100Gb
nic02: 10Gb
...
loop_control:
label: "{{item.name}}"
This will now display just the 'label' field instead of the whole structure per 'item', it defaults to '"{{item}}"' to display things as usual.
2016-09-30 14:01:56 +00:00
.. versionadded :: 2.2
Another option to loop control is C(pause), which allows you to control the time (in seconds) between execution of items in a task loop.::
2016-08-31 20:09:37 +00:00
# main.yml
- name: create servers, pause 3s before creating next
2017-03-06 22:39:07 +00:00
digital_ocean:
name: "{{ item }}"
state: present
2016-09-30 14:01:56 +00:00
with_items:
2016-08-31 20:09:37 +00:00
- server1
- server2
2016-09-30 14:01:56 +00:00
loop_control:
2016-08-31 20:09:37 +00:00
pause: 3
2015-10-23 14:31:57 +00:00
2015-10-23 07:27:09 +00:00
.. _loops_and_includes_2.0:
Loops and Includes in 2.0
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
Because `loop_control` is not available in Ansible 2.0, when using an include with a loop you should use `set_fact` to save the "outer" loops value
for `item` ::
# main.yml
2016-05-16 13:48:54 +00:00
- include: inner.yml
2015-10-23 07:27:09 +00:00
with_items:
- 1
- 2
- 3
# inner.yml
- set_fact:
2016-04-28 13:18:32 +00:00
outer_item: "{{ item }}"
2015-10-23 07:27:09 +00:00
- debug:
2016-04-28 13:18:32 +00:00
msg: "outer item={{ outer_item }} inner item={{ item }}"
2015-10-23 14:31:57 +00:00
with_items:
- a
- b
- c
2013-12-20 17:37:38 +00:00
.. _writing_your_own_iterators:
2013-10-13 12:54:36 +00:00
Writing Your Own Iterators
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2017-01-07 19:38:52 +00:00
While you ordinarily shouldn't have to, should you wish to write your own ways to loop over arbitrary data structures, you can read :doc: `dev_guide/developing_plugins` for some starter
2013-10-13 12:54:36 +00:00
information. Each of the above features are implemented as plugins in ansible, so there are many implementations to reference.
2013-10-05 16:31:16 +00:00
.. seealso ::
:doc: `playbooks`
An introduction to playbooks
2017-06-06 21:39:48 +00:00
:doc: `playbooks_reuse_roles`
2013-10-05 16:31:16 +00:00
Playbook organization by roles
:doc: `playbooks_best_practices`
Best practices in playbooks
:doc: `playbooks_conditionals`
Conditional statements in playbooks
:doc: `playbooks_variables`
All about variables
`User Mailing List <http://groups.google.com/group/ansible-devel> `_
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel