mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #17833 from resmo/docs/fix_loops
docsite: various fixes in loops doc
This commit is contained in:
commit
d88025f0cb
1 changed files with 29 additions and 28 deletions
|
@ -116,11 +116,11 @@ Assuming that ``first_example_file`` contained the text "hello" and ``second_exa
|
|||
|
||||
TASK [debug msg={{ item }}] ******************************************************
|
||||
ok: [localhost] => (item=hello) => {
|
||||
"item": "hello",
|
||||
"item": "hello",
|
||||
"msg": "hello"
|
||||
}
|
||||
ok: [localhost] => (item=world) => {
|
||||
"item": "world",
|
||||
"item": "world",
|
||||
"msg": "world"
|
||||
}
|
||||
|
||||
|
@ -278,7 +278,7 @@ for those), it can somewhat be used as a poor man's loadbalancer in a MacGyver l
|
|||
- "press the red button"
|
||||
- "do nothing"
|
||||
|
||||
One of the provided strings will be selected at random.
|
||||
One of the provided strings will be selected at random.
|
||||
|
||||
At a more basic level, they can be used to add chaos and excitement to otherwise predictable automation environments.
|
||||
|
||||
|
@ -287,10 +287,10 @@ At a more basic level, they can be used to add chaos and excitement to otherwise
|
|||
Do-Until Loops
|
||||
``````````````
|
||||
|
||||
.. versionadded: 1.4
|
||||
.. versionadded:: 1.4
|
||||
|
||||
Sometimes you would want to retry a task until a certain condition is met. Here's an example::
|
||||
|
||||
|
||||
- action: shell /usr/bin/foo
|
||||
register: result
|
||||
until: result.stdout.find("all systems go") != -1
|
||||
|
@ -369,7 +369,7 @@ Looping Over A List With An Index
|
|||
|
||||
.. 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.
|
||||
|
||||
.. versionadded: 1.3
|
||||
.. versionadded:: 1.3
|
||||
|
||||
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::
|
||||
|
@ -382,7 +382,7 @@ It's uncommonly used::
|
|||
|
||||
Using ini file with a loop
|
||||
``````````````````````````
|
||||
.. versionadded: 2.0
|
||||
.. versionadded:: 2.0
|
||||
|
||||
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::
|
||||
|
||||
|
@ -402,25 +402,25 @@ Here is an example of using ``with_ini``::
|
|||
And here is the returned value::
|
||||
|
||||
{
|
||||
"changed": false,
|
||||
"msg": "All items completed",
|
||||
"changed": false,
|
||||
"msg": "All items completed",
|
||||
"results": [
|
||||
{
|
||||
"invocation": {
|
||||
"module_args": "msg=\"section1/value1\"",
|
||||
"module_args": "msg=\"section1/value1\"",
|
||||
"module_name": "debug"
|
||||
},
|
||||
"item": "section1/value1",
|
||||
"msg": "section1/value1",
|
||||
},
|
||||
"item": "section1/value1",
|
||||
"msg": "section1/value1",
|
||||
"verbose_always": true
|
||||
},
|
||||
},
|
||||
{
|
||||
"invocation": {
|
||||
"module_args": "msg=\"section1/value2\"",
|
||||
"module_args": "msg=\"section1/value2\"",
|
||||
"module_name": "debug"
|
||||
},
|
||||
"item": "section1/value2",
|
||||
"msg": "section1/value2",
|
||||
},
|
||||
"item": "section1/value2",
|
||||
"msg": "section1/value2",
|
||||
"verbose_always": true
|
||||
}
|
||||
]
|
||||
|
@ -447,7 +447,7 @@ a really crazy hypothetical datastructure::
|
|||
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
|
||||
yum: name={{ item }} state=installed
|
||||
yum: name={{ item }} state=installed
|
||||
with_flattened:
|
||||
- "{{ packages_base }}"
|
||||
- "{{ packages_apps }}"
|
||||
|
@ -551,7 +551,7 @@ More information on the patterns can be found on :doc:`intro_patterns`
|
|||
Loop Control
|
||||
````````````
|
||||
|
||||
.. versionadded: 2.1
|
||||
.. versionadded:: 2.1
|
||||
|
||||
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.
|
||||
|
@ -575,12 +575,13 @@ As of Ansible 2.1, the `loop_control` option can be used to specify the name of
|
|||
|
||||
.. 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.
|
||||
|
||||
.. 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::
|
||||
.. 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::
|
||||
|
||||
- name: create servers
|
||||
digital_ocean: name={{item.name}} state=present ....
|
||||
with_items:
|
||||
with_items:
|
||||
- name: server1
|
||||
disks: 3gb
|
||||
ram: 15Gb
|
||||
|
@ -593,16 +594,17 @@ When using complex data structures for looping the display might get a bit too "
|
|||
|
||||
This will now display just the 'label' field instead of the whole structure per 'item', it defaults to '"{{item}}"' to display things as usual.
|
||||
|
||||
.. 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.::
|
||||
.. 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.::
|
||||
|
||||
# main.yml
|
||||
- name: create servers, pause 3s before creating next
|
||||
digital_ocean: name={{item}} state=present ....
|
||||
with_items:
|
||||
with_items:
|
||||
- server1
|
||||
- server2
|
||||
loop_control:
|
||||
loop_control:
|
||||
pause: 3
|
||||
|
||||
|
||||
|
@ -658,4 +660,3 @@ information. Each of the above features are implemented as plugins in ansible,
|
|||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue