mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Add docs detailing how to convert many with_X style loops to use loop and filters (#40964)
* Add docs detailing how to convert many with_X style loops to use loop and filters. Fixes #40727 * Switch lookup used in query vs lookup comparison, to not recommend use of nested lookup * Improve docs based on feedback
This commit is contained in:
parent
5228b939fe
commit
1cec3c8daf
3 changed files with 206 additions and 3 deletions
|
@ -105,7 +105,7 @@ For example, using the 'nested' lookup, you can combine lists::
|
|||
priv: "{{ item[1] }}.*:ALL"
|
||||
append_privs: yes
|
||||
password: "foo"
|
||||
loop: "{{ query('nested', [ 'alice', 'bob' ], [ 'clientdb', 'employeedb', 'providerdb' ]) }}"
|
||||
loop: "{{ ['alice', 'bob'] |product(['clientdb', 'employeedb', 'providerdb'])|list }}"
|
||||
|
||||
.. note:: ``with_`` loops are actually a combination of things ``with_`` + ``lookup()``, even ``items`` is a lookup. ``loop`` can be used in the same way as shown above.
|
||||
|
||||
|
@ -121,9 +121,9 @@ In certain situations the ``lookup`` function may not return a list which ``loop
|
|||
|
||||
The following invocations are equivalent, using ``wantlist=True`` with ``lookup`` to ensure a return type of a list::
|
||||
|
||||
loop: "{{ query('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb']) }}"
|
||||
loop: "{{ query('inventory_hostnames', 'all') }}"
|
||||
|
||||
loop: "{{ lookup('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb'], wantlist=True) }}"
|
||||
loop: "{{ lookup('inventory_hostnames', 'all', wantlist=True) }}"
|
||||
|
||||
|
||||
.. _do_until_loops:
|
||||
|
@ -330,6 +330,11 @@ If you need to keep track of where you are in a loop, you can use the ``index_va
|
|||
loop_control:
|
||||
index_var: my_idx
|
||||
|
||||
Migrating from with_X to loop
|
||||
`````````````````````````````
|
||||
|
||||
.. include:: shared_snippets/with2loop.txt
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue