Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation on dict2items #38375

Merged
merged 1 commit into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/docsite/rst/user_guide/playbooks_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ To get the symmetric difference of 2 lists (items exclusive to each list)::
{{ list1 | symmetric_difference(list2) }}


.. _dict_filter:

Dict Filter
```````````

.. versionadded:: 2.6


To turn a dictionary into a list of items, suitable for looping, use `dict2items`::

{{ dict | dict2items }}

Which turns::

tags:
Application: payment
Environment: dev

into::

- key: Application
value: payment
- key: Environment
value: dev

.. _random_filter:

Random Number Filter
Expand Down
17 changes: 16 additions & 1 deletion docs/docsite/rst/user_guide/playbooks_loops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ If you have a list of hashes, you can reference subkeys using things like::
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }

Also be aware that when combining :ref:`when: playbooks_conditionals` with a loop, the ``when:`` statement is processed separately for each item.
Also be aware that when combining :doc:`playbooks_conditionals` with a loop, the ``when:`` statement is processed separately for each item.
See :ref:`the_when_statement` for an example.

To loop over a dict, use the ``dict2items`` :ref:`dict_filter`::

- name: create a tag dictionary of non-empty tags
set_fact:
tags_dict: "{{ (tags_dict|default({}))|combine({item.key: item.value}) }}"
with_items: "{{ tags|dict2items }}"
vars:
tags:
Environment: dev
Application: payment
Another: "{{ doesnotexist|default() }}"
when: item.value != ""

Here, we don't want to set empty tags, so we create a dictionary containing only non-empty tags.


.. _complex_loops:

Expand Down