Merge pull request #17833 from resmo/docs/fix_loops

docsite: various fixes in loops doc
pull/4420/head
scottb 2016-09-30 11:26:46 -07:00 committed by GitHub
commit d88025f0cb
1 changed files with 29 additions and 28 deletions

View File

@ -116,11 +116,11 @@ Assuming that ``first_example_file`` contained the text "hello" and ``second_exa
TASK [debug msg={{ item }}] ****************************************************** TASK [debug msg={{ item }}] ******************************************************
ok: [localhost] => (item=hello) => { ok: [localhost] => (item=hello) => {
"item": "hello", "item": "hello",
"msg": "hello" "msg": "hello"
} }
ok: [localhost] => (item=world) => { ok: [localhost] => (item=world) => {
"item": "world", "item": "world",
"msg": "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" - "press the red button"
- "do nothing" - "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. 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 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:: Sometimes you would want to retry a task until a certain condition is met. Here's an example::
- action: shell /usr/bin/foo - action: shell /usr/bin/foo
register: result register: result
until: result.stdout.find("all systems go") != -1 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. .. 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. 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:: It's uncommonly used::
@ -382,7 +382,7 @@ It's uncommonly used::
Using ini file with a loop 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:: 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:: And here is the returned value::
{ {
"changed": false, "changed": false,
"msg": "All items completed", "msg": "All items completed",
"results": [ "results": [
{ {
"invocation": { "invocation": {
"module_args": "msg=\"section1/value1\"", "module_args": "msg=\"section1/value1\"",
"module_name": "debug" "module_name": "debug"
}, },
"item": "section1/value1", "item": "section1/value1",
"msg": "section1/value1", "msg": "section1/value1",
"verbose_always": true "verbose_always": true
}, },
{ {
"invocation": { "invocation": {
"module_args": "msg=\"section1/value2\"", "module_args": "msg=\"section1/value2\"",
"module_name": "debug" "module_name": "debug"
}, },
"item": "section1/value2", "item": "section1/value2",
"msg": "section1/value2", "msg": "section1/value2",
"verbose_always": true "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?:: 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 - name: flattened loop demo
yum: name={{ item }} state=installed yum: name={{ item }} state=installed
with_flattened: with_flattened:
- "{{ packages_base }}" - "{{ packages_base }}"
- "{{ packages_apps }}" - "{{ packages_apps }}"
@ -551,7 +551,7 @@ More information on the patterns can be found on :doc:`intro_patterns`
Loop Control 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. 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. 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. .. 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 .. 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::
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 - name: create servers
digital_ocean: name={{item.name}} state=present .... digital_ocean: name={{item.name}} state=present ....
with_items: with_items:
- name: server1 - name: server1
disks: 3gb disks: 3gb
ram: 15Gb 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. 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 .. 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.::
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 # main.yml
- name: create servers, pause 3s before creating next - name: create servers, pause 3s before creating next
digital_ocean: name={{item}} state=present .... digital_ocean: name={{item}} state=present ....
with_items: with_items:
- server1 - server1
- server2 - server2
loop_control: loop_control:
pause: 3 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>`_ `irc.freenode.net <http://irc.freenode.net>`_
#ansible IRC chat channel #ansible IRC chat channel