updated conditional docs
- added missing anchors - clarified when/with interactions - added examples for skipping loops on undefined varspull/4420/head
parent
ba74f5f3e5
commit
7376405358
|
@ -95,13 +95,32 @@ If a required variable has not been set, you can skip or fail using Jinja2's
|
||||||
This is especially useful in combination with the conditional import of vars
|
This is especially useful in combination with the conditional import of vars
|
||||||
files (see below).
|
files (see below).
|
||||||
|
|
||||||
Note that when combining `when` with `with_items` (see :doc:`playbooks_loops`), be aware that the `when` statement is processed separately for each item. This is by design::
|
.. _loops_and_conditionals:
|
||||||
|
|
||||||
|
Loops and Conditionals
|
||||||
|
``````````````````````
|
||||||
|
Combining `when` with `with_items` (see :doc:`playbooks_loops`), be aware that the `when` statement is processed separately for each item. This is by design::
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- command: echo {{ item }}
|
- command: echo {{ item }}
|
||||||
with_items: [ 0, 2, 4, 6, 8, 10 ]
|
with_items: [ 0, 2, 4, 6, 8, 10 ]
|
||||||
when: item > 5
|
when: item > 5
|
||||||
|
|
||||||
|
If you need to skip the whole task depending on the loop variable being defined, used the `|default` filter to provide an empty iterator::
|
||||||
|
|
||||||
|
- command: echo {{ item }}
|
||||||
|
with_items: "{{ mylist|default([]) }}"
|
||||||
|
when: item > 5
|
||||||
|
|
||||||
|
|
||||||
|
If using `with_dict` which does not take a list::
|
||||||
|
|
||||||
|
- command: echo {{ item.key }}
|
||||||
|
with_dict: "{{ mydict|default({}) }}"
|
||||||
|
when: item.value > 5
|
||||||
|
|
||||||
|
.. _loading_in_custom_facts:
|
||||||
|
|
||||||
Loading in Custom Facts
|
Loading in Custom Facts
|
||||||
```````````````````````
|
```````````````````````
|
||||||
|
|
||||||
|
@ -114,7 +133,9 @@ there will be accessible to future tasks::
|
||||||
action: site_facts
|
action: site_facts
|
||||||
- command: /usr/bin/thingy
|
- command: /usr/bin/thingy
|
||||||
when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'
|
when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'
|
||||||
|
|
||||||
|
.. _when_roles_and_includes:
|
||||||
|
|
||||||
Applying 'when' to roles and includes
|
Applying 'when' to roles and includes
|
||||||
`````````````````````````````````````
|
`````````````````````````````````````
|
||||||
|
|
||||||
|
@ -135,6 +156,8 @@ Or with a role::
|
||||||
You will note a lot of 'skipped' output by default in Ansible when using this approach on systems that don't match the criteria.
|
You will note a lot of 'skipped' output by default in Ansible when using this approach on systems that don't match the criteria.
|
||||||
Read up on the 'group_by' module in the :doc:`modules` docs for a more streamlined way to accomplish the same thing.
|
Read up on the 'group_by' module in the :doc:`modules` docs for a more streamlined way to accomplish the same thing.
|
||||||
|
|
||||||
|
.. _conditional_imports:
|
||||||
|
|
||||||
Conditional Imports
|
Conditional Imports
|
||||||
```````````````````
|
```````````````````
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue