mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
663 lines
21 KiB
ReStructuredText
663 lines
21 KiB
ReStructuredText
11 years ago
|
Loops
|
||
|
=====
|
||
11 years ago
|
|
||
11 years ago
|
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.
|
||
11 years ago
|
|
||
11 years ago
|
.. contents:: Topics
|
||
|
|
||
11 years ago
|
.. _standard_loops:
|
||
11 years ago
|
|
||
11 years ago
|
Standard Loops
|
||
|
``````````````
|
||
13 years ago
|
|
||
|
To save some typing, repeated tasks can be written in short-hand like so::
|
||
|
|
||
12 years ago
|
- name: add several users
|
||
11 years ago
|
user: name={{ item }} state=present groups=wheel
|
||
13 years ago
|
with_items:
|
||
|
- testuser1
|
||
|
- testuser2
|
||
|
|
||
13 years ago
|
If you have defined a YAML list in a variables file, or the 'vars' section, you can also do::
|
||
|
|
||
9 years ago
|
with_items: "{{ somelist }}"
|
||
13 years ago
|
|
||
13 years ago
|
The above would be the equivalent of::
|
||
|
|
||
|
- name: add user testuser1
|
||
11 years ago
|
user: name=testuser1 state=present groups=wheel
|
||
13 years ago
|
- name: add user testuser2
|
||
11 years ago
|
user: name=testuser2 state=present groups=wheel
|
||
13 years ago
|
|
||
12 years ago
|
The yum and apt modules use with_items to execute fewer package manager transactions.
|
||
13 years ago
|
|
||
12 years ago
|
Note that the types of items you iterate over with 'with_items' do not have to be simple lists of strings.
|
||
12 years ago
|
If you have a list of hashes, you can reference subkeys using things like::
|
||
|
|
||
12 years ago
|
- name: add several users
|
||
11 years ago
|
user: name={{ item.name }} state=present groups={{ item.groups }}
|
||
12 years ago
|
with_items:
|
||
|
- { name: 'testuser1', groups: 'wheel' }
|
||
|
- { name: 'testuser2', groups: 'root' }
|
||
12 years ago
|
|
||
10 years ago
|
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.
|
||
|
|
||
8 years ago
|
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.
|
||
|
|
||
11 years ago
|
.. _nested_loops:
|
||
|
|
||
12 years ago
|
Nested Loops
|
||
|
````````````
|
||
|
|
||
12 years ago
|
Loops can be nested as well::
|
||
12 years ago
|
|
||
12 years ago
|
- name: give users access to multiple databases
|
||
11 years ago
|
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
|
||
12 years ago
|
with_nested:
|
||
11 years ago
|
- [ 'alice', 'bob' ]
|
||
12 years ago
|
- [ 'clientdb', 'employeedb', 'providerdb' ]
|
||
|
|
||
10 years ago
|
As with the case of 'with_items' above, you can use previously defined variables.::
|
||
12 years ago
|
|
||
|
- name: here, 'users' contains the above list of employees
|
||
11 years ago
|
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
|
||
12 years ago
|
with_nested:
|
||
9 years ago
|
- "{{ users }}"
|
||
12 years ago
|
- [ 'clientdb', 'employeedb', 'providerdb' ]
|
||
|
|
||
11 years ago
|
.. _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
|
||
|
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
|
||
9 years ago
|
with_dict: "{{ users }}"
|
||
11 years ago
|
|
||
11 years ago
|
.. _looping_over_fileglobs:
|
||
|
|
||
9 years ago
|
Looping over Files
|
||
|
``````````````````
|
||
|
|
||
9 years ago
|
``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::
|
||
9 years ago
|
|
||
|
---
|
||
|
- hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
# emit a debug message containing the content of each file.
|
||
|
- debug:
|
||
9 years ago
|
msg: "{{ item }}"
|
||
9 years ago
|
with_file:
|
||
|
- first_example_file
|
||
|
- second_example_file
|
||
|
|
||
|
Assuming that ``first_example_file`` contained the text "hello" and ``second_example_file`` contained the text "world", this would result in::
|
||
|
|
||
9 years ago
|
TASK [debug msg={{ item }}] ******************************************************
|
||
9 years ago
|
ok: [localhost] => (item=hello) => {
|
||
8 years ago
|
"item": "hello",
|
||
9 years ago
|
"msg": "hello"
|
||
|
}
|
||
|
ok: [localhost] => (item=world) => {
|
||
8 years ago
|
"item": "world",
|
||
9 years ago
|
"msg": "world"
|
||
|
}
|
||
|
|
||
11 years ago
|
Looping over Fileglobs
|
||
|
``````````````````````
|
||
12 years ago
|
|
||
8 years ago
|
``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::
|
||
12 years ago
|
|
||
12 years ago
|
---
|
||
12 years ago
|
- hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
# first ensure our target directory exists
|
||
12 years ago
|
- file: dest=/etc/fooapp state=directory
|
||
12 years ago
|
|
||
|
# copy each file over that matches the given pattern
|
||
12 years ago
|
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
|
||
12 years ago
|
with_fileglob:
|
||
12 years ago
|
- /playbooks/files/fooapp/*
|
||
10 years ago
|
|
||
11 years ago
|
.. note:: When using a relative path with ``with_fileglob`` in a role, Ansible resolves the path relative to the `roles/<rolename>/files` directory.
|
||
12 years ago
|
|
||
11 years ago
|
Looping over Parallel Sets of Data
|
||
|
``````````````````````````````````
|
||
12 years ago
|
|
||
11 years ago
|
.. 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.
|
||
11 years ago
|
|
||
|
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:
|
||
|
- debug: msg="{{ item.0 }} and {{ item.1 }}"
|
||
|
with_together:
|
||
9 years ago
|
- "{{ alpha }}"
|
||
|
- "{{ numbers }}"
|
||
12 years ago
|
|
||
11 years ago
|
Looping over Subelements
|
||
|
````````````````````````
|
||
12 years ago
|
|
||
11 years ago
|
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
|
||
10 years ago
|
SSH keys.
|
||
11 years ago
|
|
||
|
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
|
||
10 years ago
|
authorized:
|
||
11 years ago
|
- /tmp/alice/onekey.pub
|
||
|
- /tmp/alice/twokey.pub
|
||
10 years ago
|
mysql:
|
||
|
password: mysql-password
|
||
|
hosts:
|
||
|
- "%"
|
||
|
- "127.0.0.1"
|
||
|
- "::1"
|
||
|
- "localhost"
|
||
|
privs:
|
||
|
- "*.*:SELECT"
|
||
|
- "DB1.*:ALL"
|
||
11 years ago
|
- name: bob
|
||
|
authorized:
|
||
|
- /tmp/bob/id_rsa.pub
|
||
10 years ago
|
mysql:
|
||
|
password: other-mysql-password
|
||
|
hosts:
|
||
|
- "db1"
|
||
|
privs:
|
||
|
- "*.*:SELECT"
|
||
|
- "DB2.*:ALL"
|
||
11 years ago
|
|
||
|
It might happen like so::
|
||
|
|
||
|
- user: name={{ item.name }} state=present generate_ssh_key=yes
|
||
9 years ago
|
with_items: "{{ users }}"
|
||
12 years ago
|
|
||
11 years ago
|
- authorized_key: "user={{ item.0.name }} key='{{ lookup('file', item.1) }}'"
|
||
11 years ago
|
with_subelements:
|
||
9 years ago
|
- "{{ users }}"
|
||
11 years ago
|
- authorized
|
||
|
|
||
10 years ago
|
Given the mysql hosts and privs subkey lists, you can also iterate over a list in a nested subkey::
|
||
|
|
||
|
- name: Setup MySQL users
|
||
9 years ago
|
mysql_user: name={{ item.0.name }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join('/') }}
|
||
10 years ago
|
with_subelements:
|
||
9 years ago
|
- "{{ users }}"
|
||
10 years ago
|
- mysql.hosts
|
||
|
|
||
|
Subelements walks a list of hashes (aka dictionaries) and then traverses a list with a given (nested sub-)key inside of those
|
||
11 years ago
|
records.
|
||
|
|
||
10 years ago
|
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.
|
||
|
|
||
11 years ago
|
The authorized_key pattern is exactly where it comes up most.
|
||
|
|
||
|
.. _looping_over_integer_sequences:
|
||
12 years ago
|
|
||
11 years ago
|
Looping over Integer Sequences
|
||
|
``````````````````````````````
|
||
12 years ago
|
|
||
12 years ago
|
``with_sequence`` generates a sequence of items in ascending numerical order. You
|
||
12 years ago
|
can specify a start, end, and an optional step value.
|
||
12 years ago
|
|
||
12 years ago
|
Arguments should be specified in key=value pairs. If supplied, the 'format' is a printf style string.
|
||
12 years ago
|
|
||
12 years ago
|
Numerical values can be specified in decimal, hexadecimal (0x3f8) or octal (0600).
|
||
12 years ago
|
Negative numbers are not supported. This works as follows::
|
||
|
|
||
|
---
|
||
12 years ago
|
- hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
# create groups
|
||
|
- group: name=evens state=present
|
||
|
- group: name=odds state=present
|
||
|
|
||
12 years ago
|
# create some test users
|
||
11 years ago
|
- user: name={{ item }} state=present groups=evens
|
||
12 years ago
|
with_sequence: start=0 end=32 format=testuser%02x
|
||
12 years ago
|
|
||
12 years ago
|
# create a series of directories with even numbers for some reason
|
||
|
- file: dest=/var/stuff/{{ item }} state=directory
|
||
|
with_sequence: start=4 end=16 stride=2
|
||
12 years ago
|
|
||
12 years ago
|
# a simpler way to use the sequence plugin
|
||
12 years ago
|
# create 4 groups
|
||
12 years ago
|
- group: name=group{{ item }} state=present
|
||
12 years ago
|
with_sequence: count=4
|
||
|
|
||
11 years ago
|
.. _random_choice:
|
||
|
|
||
|
Random Choices
|
||
|
``````````````
|
||
|
|
||
11 years ago
|
The 'random_choice' feature can be used to pick something at random. While it's not a load balancer (there are modules
|
||
8 years ago
|
for those), it can somewhat be used as a poor man's load balancer in a MacGyver like situation::
|
||
11 years ago
|
|
||
|
- debug: msg={{ item }}
|
||
|
with_random_choice:
|
||
|
- "go through the door"
|
||
|
- "drink from the goblet"
|
||
|
- "press the red button"
|
||
|
- "do nothing"
|
||
|
|
||
8 years ago
|
One of the provided strings will be selected at random.
|
||
11 years ago
|
|
||
|
At a more basic level, they can be used to add chaos and excitement to otherwise predictable automation environments.
|
||
|
|
||
|
.. _do_until_loops:
|
||
|
|
||
11 years ago
|
Do-Until Loops
|
||
|
``````````````
|
||
11 years ago
|
|
||
8 years ago
|
.. versionadded:: 1.4
|
||
11 years ago
|
|
||
11 years ago
|
Sometimes you would want to retry a task until a certain condition is met. Here's an example::
|
||
8 years ago
|
|
||
11 years ago
|
- action: shell /usr/bin/foo
|
||
|
register: result
|
||
11 years ago
|
until: result.stdout.find("all systems go") != -1
|
||
11 years ago
|
retries: 5
|
||
|
delay: 10
|
||
11 years ago
|
|
||
11 years ago
|
The above example run the shell module recursively till the module's result has "all systems go" in its stdout or the task has
|
||
11 years ago
|
been retried for 5 times with a delay of 10 seconds. The default value for "retries" is 3 and "delay" is 5.
|
||
13 years ago
|
|
||
11 years ago
|
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.
|
||
13 years ago
|
|
||
11 years ago
|
.. _with_first_found:
|
||
|
|
||
|
Finding First Matched Files
|
||
|
```````````````````````````
|
||
|
|
||
11 years ago
|
.. 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.
|
||
|
|
||
11 years ago
|
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
|
||
|
template: src={{ item }} dest=/etc/foo.conf
|
||
|
with_first_found:
|
||
9 years ago
|
- "{{ ansible_virtualization_type }}_foo.conf"
|
||
11 years ago
|
- "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
|
||
|
template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root
|
||
|
with_first_found:
|
||
|
- files:
|
||
9 years ago
|
- "{{ inventory_hostname }}/etc/file.cfg"
|
||
11 years ago
|
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
|
||
|
`````````````````````````````````````````````````
|
||
|
|
||
11 years ago
|
.. 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.
|
||
|
|
||
11 years ago
|
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.
|
||
9 years ago
|
Ansible provides a neat way to do that, though you should remember, this is always executed on the control machine, not the remote
|
||
11 years ago
|
machine::
|
||
|
|
||
|
- name: Example of looping over a command result
|
||
|
shell: /usr/bin/frobnicate {{ item }}
|
||
|
with_lines: /usr/bin/frobnications_per_host --param {{ inventory_hostname }}
|
||
|
|
||
|
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
|
||
|
shell: /usr/bin/something
|
||
|
register: command_result
|
||
|
|
||
|
- name: Do something with each result
|
||
|
shell: /usr/bin/something_else --param {{ item }}
|
||
9 years ago
|
with_items: "{{ command_result.stdout_lines }}"
|
||
11 years ago
|
|
||
|
.. _indexed_lists:
|
||
|
|
||
|
Looping Over A List With An Index
|
||
|
`````````````````````````````````
|
||
|
|
||
11 years ago
|
.. 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.
|
||
|
|
||
8 years ago
|
.. versionadded:: 1.3
|
||
11 years ago
|
|
||
|
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
|
||
|
debug: msg="at array position {{ item.0 }} there is a value {{ item.1 }}"
|
||
9 years ago
|
with_indexed_items: "{{ some_list }}"
|
||
11 years ago
|
|
||
9 years ago
|
.. _using_ini_with_a_loop:
|
||
|
|
||
|
Using ini file with a loop
|
||
|
``````````````````````````
|
||
8 years ago
|
.. versionadded:: 2.0
|
||
9 years ago
|
|
||
|
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::
|
||
|
|
||
|
[section1]
|
||
|
value1=section1/value1
|
||
|
value2=section1/value2
|
||
|
|
||
|
[section2]
|
||
|
value1=section2/value1
|
||
|
value2=section2/value2
|
||
|
|
||
|
Here is an example of using ``with_ini``::
|
||
|
|
||
9 years ago
|
- debug: msg="{{ item }}"
|
||
9 years ago
|
with_ini: value[1-2] section=section1 file=lookup.ini re=true
|
||
|
|
||
|
And here is the returned value::
|
||
|
|
||
|
{
|
||
8 years ago
|
"changed": false,
|
||
|
"msg": "All items completed",
|
||
9 years ago
|
"results": [
|
||
|
{
|
||
|
"invocation": {
|
||
8 years ago
|
"module_args": "msg=\"section1/value1\"",
|
||
9 years ago
|
"module_name": "debug"
|
||
8 years ago
|
},
|
||
|
"item": "section1/value1",
|
||
|
"msg": "section1/value1",
|
||
9 years ago
|
"verbose_always": true
|
||
8 years ago
|
},
|
||
9 years ago
|
{
|
||
|
"invocation": {
|
||
8 years ago
|
"module_args": "msg=\"section1/value2\"",
|
||
9 years ago
|
"module_name": "debug"
|
||
8 years ago
|
},
|
||
|
"item": "section1/value2",
|
||
|
"msg": "section1/value2",
|
||
9 years ago
|
"verbose_always": true
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
|
||
11 years ago
|
.. _flattening_a_list:
|
||
|
|
||
|
Flattening A List
|
||
|
`````````````````
|
||
|
|
||
11 years ago
|
.. 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.
|
||
|
|
||
11 years ago
|
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
|
||
8 years ago
|
yum: name={{ item }} state=installed
|
||
11 years ago
|
with_flattened:
|
||
9 years ago
|
- "{{ packages_base }}"
|
||
|
- "{{ packages_apps }}"
|
||
11 years ago
|
|
||
|
That's how!
|
||
|
|
||
11 years ago
|
.. _using_register_with_a_loop:
|
||
11 years ago
|
|
||
11 years ago
|
Using register with a loop
|
||
|
``````````````````````````
|
||
|
|
||
8 years ago
|
When 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.
|
||
11 years ago
|
|
||
11 years ago
|
Here is an example of using ``register`` with ``with_items``::
|
||
11 years ago
|
|
||
|
- shell: echo "{{ item }}"
|
||
|
with_items:
|
||
|
- one
|
||
|
- two
|
||
|
register: echo
|
||
|
|
||
11 years ago
|
This differs from the data structure returned when using ``register`` without a loop::
|
||
11 years ago
|
|
||
|
{
|
||
|
"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
|
||
9 years ago
|
with_items: "{{ echo.results }}"
|
||
11 years ago
|
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
.. _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
|
||
|
- debug: msg={{ item }}
|
||
9 years ago
|
with_items: "{{ groups['all'] }}"
|
||
9 years ago
|
|
||
|
# show all the hosts in the current play
|
||
|
- debug: msg={{ item }}
|
||
8 years ago
|
with_items: "{{ play_hosts }}"
|
||
9 years ago
|
|
||
9 years ago
|
There is also a specific lookup plugin ``inventory_hostnames`` that can be used like this::
|
||
9 years ago
|
|
||
|
# show all the hosts in the inventory
|
||
|
- debug: msg={{ item }}
|
||
9 years ago
|
with_inventory_hostnames: all
|
||
9 years ago
|
|
||
|
# show all the hosts matching the pattern, ie all but the group www
|
||
|
- debug: msg={{ item }}
|
||
9 years ago
|
with_inventory_hostnames: all:!www
|
||
9 years ago
|
|
||
|
More information on the patterns can be found on :doc:`intro_patterns`
|
||
|
|
||
9 years ago
|
.. _loop_control:
|
||
9 years ago
|
|
||
9 years ago
|
Loop Control
|
||
|
````````````
|
||
9 years ago
|
|
||
8 years ago
|
.. versionadded:: 2.1
|
||
9 years ago
|
|
||
9 years ago
|
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::
|
||
9 years ago
|
|
||
9 years ago
|
# main.yml
|
||
9 years ago
|
- include: inner.yml
|
||
9 years ago
|
with_items:
|
||
|
- 1
|
||
|
- 2
|
||
|
- 3
|
||
9 years ago
|
loop_control:
|
||
|
loop_var: outer_item
|
||
|
|
||
|
# inner.yml
|
||
9 years ago
|
- debug: msg="outer item={{ outer_item }} inner item={{ item }}"
|
||
9 years ago
|
with_items:
|
||
|
- a
|
||
|
- b
|
||
|
- c
|
||
9 years ago
|
|
||
9 years ago
|
.. 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.
|
||
9 years ago
|
|
||
8 years ago
|
.. 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::
|
||
8 years ago
|
|
||
|
- name: create servers
|
||
|
digital_ocean: name={{item.name}} state=present ....
|
||
8 years ago
|
with_items:
|
||
8 years ago
|
- name: server1
|
||
|
disks: 3gb
|
||
|
ram: 15Gb
|
||
8 years ago
|
network:
|
||
8 years ago
|
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.
|
||
|
|
||
8 years ago
|
.. 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.::
|
||
8 years ago
|
|
||
|
# main.yml
|
||
|
- name: create servers, pause 3s before creating next
|
||
|
digital_ocean: name={{item}} state=present ....
|
||
8 years ago
|
with_items:
|
||
8 years ago
|
- server1
|
||
|
- server2
|
||
8 years ago
|
loop_control:
|
||
8 years ago
|
pause: 3
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
.. _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
|
||
9 years ago
|
- include: inner.yml
|
||
9 years ago
|
with_items:
|
||
|
- 1
|
||
|
- 2
|
||
|
- 3
|
||
|
|
||
|
# inner.yml
|
||
|
- set_fact:
|
||
9 years ago
|
outer_item: "{{ item }}"
|
||
9 years ago
|
|
||
|
- debug:
|
||
9 years ago
|
msg: "outer item={{ outer_item }} inner item={{ item }}"
|
||
9 years ago
|
with_items:
|
||
|
- a
|
||
|
- b
|
||
|
- c
|
||
|
|
||
|
|
||
11 years ago
|
.. _writing_your_own_iterators:
|
||
|
|
||
11 years ago
|
Writing Your Own Iterators
|
||
|
``````````````````````````
|
||
|
|
||
8 years ago
|
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:`developing_plugins` for some starter
|
||
11 years ago
|
information. Each of the above features are implemented as plugins in ansible, so there are many implementations to reference.
|
||
|
|
||
11 years ago
|
.. seealso::
|
||
|
|
||
|
:doc:`playbooks`
|
||
|
An introduction to playbooks
|
||
|
:doc:`playbooks_roles`
|
||
|
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
|
||
|
|