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.
ansible/docs/docsite/rst/user_guide/shared_snippets/with2loop.txt

206 lines
5.1 KiB
Plaintext

[docs][backport]Backportapalooza 10 (#71621) * Feature freeze date has been merged with Ansible-2.10.0beta1 (#71494) (cherry picked from commit c586d436fabc8033055d5195038ca3341d7f0192) * Add --allow-disabled to sanity docs (#71524) (cherry picked from commit bc6461432ef4d0d9323d9b4355efd09e79c0a7b9) * Update intro_patterns.rst (#71542) Call out the trailing comma when specifying a single host. Small snag that took me a while to notice. (cherry picked from commit ec3920cef1692d54c08f1932cf0724239628aff1) * ansible-vault: Fix typo in help message (#71485) (cherry picked from commit 215eb730e1e69f6f7e1566c610ac1b614c50854e) * update install for 2.10 (#71543) * update install for 2.10 (cherry picked from commit f75223d2c6d38ae936a4b813190ff6324d361d7f) * User guide overhaul, Table of Contents (#71553) (cherry picked from commit b694dbadfe0774e1c0a0ad6c971ed8951c60326a) * update backport instructions for 2.11 (#71567) * update backport instructions in docs/docsite/rst/community/development_process.rst Co-authored-by: Matt Martz <matt@sivel.net> (cherry picked from commit 7f9258b024cdd134d2a26a4879f24ac04dfbdb07) * More docs updates to reflect collections ecosystem (#71597) (cherry picked from commit 96aee766f4c2ae2fe769d408a010834ef95dff71) * DOCS: Mentions ansible-base, adds collections pointers to Community and Dev Guides (#71480) (cherry picked from commit 29b20bd1b135d18e9e7942555154ad24e2b1578b) Co-authored-by: Toshio Kuratomi <a.badger@gmail.com> Co-authored-by: Amin Vakil <info@aminvakil.com> Co-authored-by: Matt Deacalion <matt@dirtymonkey.co.uk> Co-authored-by: Fabien Malfoy <fabien.malfoy@laposte.net> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
4 years ago
In most cases, loops work best with the ``loop`` keyword instead of ``with_X`` style loops. The ``loop`` syntax is usually best expressed using filters instead of more complex use of ``query`` or ``lookup``.
[docs][backport]Backportapalooza 10 (#71621) * Feature freeze date has been merged with Ansible-2.10.0beta1 (#71494) (cherry picked from commit c586d436fabc8033055d5195038ca3341d7f0192) * Add --allow-disabled to sanity docs (#71524) (cherry picked from commit bc6461432ef4d0d9323d9b4355efd09e79c0a7b9) * Update intro_patterns.rst (#71542) Call out the trailing comma when specifying a single host. Small snag that took me a while to notice. (cherry picked from commit ec3920cef1692d54c08f1932cf0724239628aff1) * ansible-vault: Fix typo in help message (#71485) (cherry picked from commit 215eb730e1e69f6f7e1566c610ac1b614c50854e) * update install for 2.10 (#71543) * update install for 2.10 (cherry picked from commit f75223d2c6d38ae936a4b813190ff6324d361d7f) * User guide overhaul, Table of Contents (#71553) (cherry picked from commit b694dbadfe0774e1c0a0ad6c971ed8951c60326a) * update backport instructions for 2.11 (#71567) * update backport instructions in docs/docsite/rst/community/development_process.rst Co-authored-by: Matt Martz <matt@sivel.net> (cherry picked from commit 7f9258b024cdd134d2a26a4879f24ac04dfbdb07) * More docs updates to reflect collections ecosystem (#71597) (cherry picked from commit 96aee766f4c2ae2fe769d408a010834ef95dff71) * DOCS: Mentions ansible-base, adds collections pointers to Community and Dev Guides (#71480) (cherry picked from commit 29b20bd1b135d18e9e7942555154ad24e2b1578b) Co-authored-by: Toshio Kuratomi <a.badger@gmail.com> Co-authored-by: Amin Vakil <info@aminvakil.com> Co-authored-by: Matt Deacalion <matt@dirtymonkey.co.uk> Co-authored-by: Fabien Malfoy <fabien.malfoy@laposte.net> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
4 years ago
These examples show how to convert many common ``with_`` style loops to ``loop`` and filters.
with_list
---------
``with_list`` is directly replaced by ``loop``.
.. code-block:: yaml+jinja
- name: with_list
ansible.builtin.debug:
msg: "{{ item }}"
with_list:
- one
- two
- name: with_list -> loop
ansible.builtin.debug:
msg: "{{ item }}"
loop:
- one
- two
with_items
----------
``with_items`` is replaced by ``loop`` and the ``flatten`` filter.
.. code-block:: yaml+jinja
- name: with_items
ansible.builtin.debug:
msg: "{{ item }}"
with_items: "{{ items }}"
- name: with_items -> loop
ansible.builtin.debug:
msg: "{{ item }}"
loop: "{{ items|flatten(levels=1) }}"
with_indexed_items
------------------
``with_indexed_items`` is replaced by ``loop``, the ``flatten`` filter and ``loop_control.index_var``.
.. code-block:: yaml+jinja
- name: with_indexed_items
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
with_indexed_items: "{{ items }}"
- name: with_indexed_items -> loop
ansible.builtin.debug:
msg: "{{ index }} - {{ item }}"
loop: "{{ items|flatten(levels=1) }}"
loop_control:
index_var: index
with_flattened
--------------
``with_flattened`` is replaced by ``loop`` and the ``flatten`` filter.
.. code-block:: yaml+jinja
- name: with_flattened
ansible.builtin.debug:
msg: "{{ item }}"
with_flattened: "{{ items }}"
- name: with_flattened -> loop
ansible.builtin.debug:
msg: "{{ item }}"
loop: "{{ items|flatten }}"
with_together
-------------
``with_together`` is replaced by ``loop`` and the ``zip`` filter.
.. code-block:: yaml+jinja
- name: with_together
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
with_together:
- "{{ list_one }}"
- "{{ list_two }}"
- name: with_together -> loop
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
loop: "{{ list_one|zip(list_two)|list }}"
[docs][2.10] Backportapalooza 9 (#71493) * Explain duplicate checks includes tags and when (#68183) ##### SUMMARY Per #67913, when comparing dependencies, Ansible takes into account parameters, tags and the when clause in determining whether a role is a duplicate or not. ##### ISSUE TYPE - Docs Pull Request +label: docsite_pr (cherry picked from commit 3e4377300b05a283a874e7b4510ce599cc0bc9b4) * Docs: ansible_host can contain FQDN (#71186) (cherry picked from commit 13ab73cd89f9a300b0becf0a1d6911c57de27bc8) * clarify inventory plugin user documentation (#71387) (cherry picked from commit fb035da3b26476c028ae76937192739bd6cb30f7) * Keep caution tape for older versions (#71400) (cherry picked from commit 156b1c524546615500a4e7b54fbf2e2c9d4d017a) * document securing editor for vault (#71404) (cherry picked from commit 6c48c62f935d711889e4b301656df0309fbe5bb2) * galaxy: Add examples for galaxy section in ansible.cfg (#70931) Add example section for galaxy section in ansible.cfg Fixes: #68402 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 3f3bcbf05e46db08a0f5f88ec1eb4c72b82d9fd5) * docs: Remove examples using the `ec2.py` script (#69107) This script is mostly unmaintained and relies on the deprecated and unmaintained `boto` library. Featuring it prominently in the docs leads to many new users using it instead of the supported `aws_ec2` inventory plugin. (cherry picked from commit 66e38bf49928d1cfa5302cee846fe26afa8d4c74) * Update uri.py (#67688) Adds an example of creating workspaces in Log analytics Azure Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com> (cherry picked from commit 4317c2c80c7af0e368e082259fa2f716c03a5f66) * docs: Update Kubernetes Guide (#71372) Fixes: #61681 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 59b80b9146765382f7fbbeefe401fe33b0df033b) * fix broken links due to master -> main branch rename (#71426) (cherry picked from commit 2b7461eb52af7eca1a6459f595b7272ccaa0a364) * Modify wording to specify two ctl-d to end stdin input in ansible-vault (#69436) * 51860 - Modify wording to specify two ctl-d to end stdin input in ansible-vault * removes space to make line 160 chars (cherry picked from commit a6537b59abc9c69777130d0af3d34fac50535a20) * user_guide: Add an example for loop (#71441) Explain how to use complex data in loop while converting from with_together Fixes: #47906 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 5c1594916a297efcd28f47d6d78ac74ba6d00108) * Add link to Matt's blog (#71436) nitzmahone's blog nicely explained why Windows is not supported as Ansible controller. Link that in documentation so users can read about it. Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 3c8744f0c157b867cb5808b3a9efae3f22f26735) * user_guide: Fix reuse role examples (#71440) Fixes: #53919 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 0b16c0a8c714d2e25cc269efdb0a2bea62a72b3c) * service: Add a note about ignored parameters (#71455) Some parameters for systemd are ignored, add a note about such parameters in documentation. Fixes: #23144 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 1257b0a184c94ed405f6e5e36557c1327ad55ff6) * updates network plugin docs pages for 2.10 (#71467) Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com> (cherry picked from commit f82a1e06d7cca73466180c1b11c9f201f865a8bc) * Remove "mode: preserve" option from documentation (#71486) Remove "mode: preserve" option from documentation in doc fragments "FILE_COMMON_ARGUMENTS", as it was incorrectly included in the documentation for the `lineinfile`, `unarchive` and other file-related modules. The `copy` and `templates` modules documentation remains untouched and still contain "mode: preserve", as intended. (cherry picked from commit 7127d374665ca6ff4565d966c0edde669d2dba23) * quick update to changelog instructins (#71492) (cherry picked from commit addee0699e25f4e3bcc9cbef9c797c08e71765fe) * update Network Advanced Topics for FQCN (#71325) * update Network Advanced Topics for FQCN (cherry picked from commit b6f10b9b52153499b2f19bd1b9a4fbf0328de7b2) * fix shippable error Co-authored-by: David M. Lee <leedm777@yahoo.com> Co-authored-by: Eric G <e+github1690@linuxw.info> Co-authored-by: Sloane Hertel <shertel@redhat.com> Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: flowerysong <paul.arthur@flowerysong.com> Co-authored-by: Jose l. Azagra <azagramac@gmail.com> Co-authored-by: Patrick Reader <pxeger@protonmail.com> Co-authored-by: John Westcott IV <32551173+john-westcott-iv@users.noreply.github.com> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com> Co-authored-by: Timothy Visser <team@sacrome.com>
4 years ago
Another example with complex data
.. code-block:: yaml+jinja
- name: with_together -> loop
ansible.builtin.debug:
[docs][2.10] Backportapalooza 9 (#71493) * Explain duplicate checks includes tags and when (#68183) ##### SUMMARY Per #67913, when comparing dependencies, Ansible takes into account parameters, tags and the when clause in determining whether a role is a duplicate or not. ##### ISSUE TYPE - Docs Pull Request +label: docsite_pr (cherry picked from commit 3e4377300b05a283a874e7b4510ce599cc0bc9b4) * Docs: ansible_host can contain FQDN (#71186) (cherry picked from commit 13ab73cd89f9a300b0becf0a1d6911c57de27bc8) * clarify inventory plugin user documentation (#71387) (cherry picked from commit fb035da3b26476c028ae76937192739bd6cb30f7) * Keep caution tape for older versions (#71400) (cherry picked from commit 156b1c524546615500a4e7b54fbf2e2c9d4d017a) * document securing editor for vault (#71404) (cherry picked from commit 6c48c62f935d711889e4b301656df0309fbe5bb2) * galaxy: Add examples for galaxy section in ansible.cfg (#70931) Add example section for galaxy section in ansible.cfg Fixes: #68402 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 3f3bcbf05e46db08a0f5f88ec1eb4c72b82d9fd5) * docs: Remove examples using the `ec2.py` script (#69107) This script is mostly unmaintained and relies on the deprecated and unmaintained `boto` library. Featuring it prominently in the docs leads to many new users using it instead of the supported `aws_ec2` inventory plugin. (cherry picked from commit 66e38bf49928d1cfa5302cee846fe26afa8d4c74) * Update uri.py (#67688) Adds an example of creating workspaces in Log analytics Azure Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com> (cherry picked from commit 4317c2c80c7af0e368e082259fa2f716c03a5f66) * docs: Update Kubernetes Guide (#71372) Fixes: #61681 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 59b80b9146765382f7fbbeefe401fe33b0df033b) * fix broken links due to master -> main branch rename (#71426) (cherry picked from commit 2b7461eb52af7eca1a6459f595b7272ccaa0a364) * Modify wording to specify two ctl-d to end stdin input in ansible-vault (#69436) * 51860 - Modify wording to specify two ctl-d to end stdin input in ansible-vault * removes space to make line 160 chars (cherry picked from commit a6537b59abc9c69777130d0af3d34fac50535a20) * user_guide: Add an example for loop (#71441) Explain how to use complex data in loop while converting from with_together Fixes: #47906 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 5c1594916a297efcd28f47d6d78ac74ba6d00108) * Add link to Matt's blog (#71436) nitzmahone's blog nicely explained why Windows is not supported as Ansible controller. Link that in documentation so users can read about it. Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 3c8744f0c157b867cb5808b3a9efae3f22f26735) * user_guide: Fix reuse role examples (#71440) Fixes: #53919 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 0b16c0a8c714d2e25cc269efdb0a2bea62a72b3c) * service: Add a note about ignored parameters (#71455) Some parameters for systemd are ignored, add a note about such parameters in documentation. Fixes: #23144 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 1257b0a184c94ed405f6e5e36557c1327ad55ff6) * updates network plugin docs pages for 2.10 (#71467) Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com> (cherry picked from commit f82a1e06d7cca73466180c1b11c9f201f865a8bc) * Remove "mode: preserve" option from documentation (#71486) Remove "mode: preserve" option from documentation in doc fragments "FILE_COMMON_ARGUMENTS", as it was incorrectly included in the documentation for the `lineinfile`, `unarchive` and other file-related modules. The `copy` and `templates` modules documentation remains untouched and still contain "mode: preserve", as intended. (cherry picked from commit 7127d374665ca6ff4565d966c0edde669d2dba23) * quick update to changelog instructins (#71492) (cherry picked from commit addee0699e25f4e3bcc9cbef9c797c08e71765fe) * update Network Advanced Topics for FQCN (#71325) * update Network Advanced Topics for FQCN (cherry picked from commit b6f10b9b52153499b2f19bd1b9a4fbf0328de7b2) * fix shippable error Co-authored-by: David M. Lee <leedm777@yahoo.com> Co-authored-by: Eric G <e+github1690@linuxw.info> Co-authored-by: Sloane Hertel <shertel@redhat.com> Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: flowerysong <paul.arthur@flowerysong.com> Co-authored-by: Jose l. Azagra <azagramac@gmail.com> Co-authored-by: Patrick Reader <pxeger@protonmail.com> Co-authored-by: John Westcott IV <32551173+john-westcott-iv@users.noreply.github.com> Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com> Co-authored-by: Timothy Visser <team@sacrome.com>
4 years ago
msg: "{{ item.0 }} - {{ item.1 }} - {{ item.2 }}"
loop: "{{ data[0]|zip(*data[1:])|list }}"
vars:
data:
- ['a', 'b', 'c']
- ['d', 'e', 'f']
- ['g', 'h', 'i']
with_dict
---------
``with_dict`` can be substituted by ``loop`` and either the ``dictsort`` or ``dict2items`` filters.
.. code-block:: yaml+jinja
- name: with_dict
ansible.builtin.debug:
msg: "{{ item.key }} - {{ item.value }}"
with_dict: "{{ dictionary }}"
- name: with_dict -> loop (option 1)
ansible.builtin.debug:
msg: "{{ item.key }} - {{ item.value }}"
loop: "{{ dictionary|dict2items }}"
- name: with_dict -> loop (option 2)
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
loop: "{{ dictionary|dictsort }}"
with_sequence
-------------
``with_sequence`` is replaced by ``loop`` and the ``range`` function, and potentially the ``format`` filter.
.. code-block:: yaml+jinja
- name: with_sequence
ansible.builtin.debug:
msg: "{{ item }}"
with_sequence: start=0 end=4 stride=2 format=testuser%02x
- name: with_sequence -> loop
ansible.builtin.debug:
msg: "{{ 'testuser%02x' | format(item) }}"
# range is exclusive of the end point
loop: "{{ range(0, 4 + 1, 2)|list }}"
with_subelements
----------------
``with_subelements`` is replaced by ``loop`` and the ``subelements`` filter.
.. code-block:: yaml+jinja
- name: with_subelements
ansible.builtin.debug:
msg: "{{ item.0.name }} - {{ item.1 }}"
with_subelements:
- "{{ users }}"
- mysql.hosts
- name: with_subelements -> loop
ansible.builtin.debug:
msg: "{{ item.0.name }} - {{ item.1 }}"
loop: "{{ users|subelements('mysql.hosts') }}"
with_nested/with_cartesian
--------------------------
``with_nested`` and ``with_cartesian`` are replaced by loop and the ``product`` filter.
.. code-block:: yaml+jinja
- name: with_nested
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
with_nested:
- "{{ list_one }}"
- "{{ list_two }}"
- name: with_nested -> loop
ansible.builtin.debug:
msg: "{{ item.0 }} - {{ item.1 }}"
loop: "{{ list_one|product(list_two)|list }}"
with_random_choice
------------------
``with_random_choice`` is replaced by just use of the ``random`` filter, without need of ``loop``.
.. code-block:: yaml+jinja
- name: with_random_choice
ansible.builtin.debug:
msg: "{{ item }}"
with_random_choice: "{{ my_list }}"
- name: with_random_choice -> loop (No loop is needed here)
ansible.builtin.debug:
msg: "{{ my_list|random }}"
tags: random