Skip to content

Commit a18cc3d

Browse files
authored
Update playbooks_filters.rst (#2404)
Add more example output
1 parent 02282b8 commit a18cc3d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

docs/docsite/rst/playbook_guide/playbooks_filters.rst

+23-10
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,12 @@ To get permutations of a list:
812812
- name: Give me the largest permutations (order matters)
813813
ansible.builtin.debug:
814814
msg: "{{ [1,2,3,4,5] | ansible.builtin.permutations | list }}"
815+
# => [(1, 2, 3, 4, 5), (1, 2, 3, 5, 4), (1, 2, 4, 3, 5), (1, 2, 4, 5, 3), (1, 2, 5, 3, 4), (1, 2, 5, 4, 3), (1, 3, 2, 4, 5), (1, 3, 2, 5, 4), (1, 3, 4, 2, 5), (1, 3, 4, 5, 2), (1, 3, 5, 2, 4), (1, 3, 5, 4, 2), (1, 4, 2, 3, 5), (1, 4, 2, 5, 3), (1, 4, 3, 2, 5), (1, 4, 3, 5, 2), (1, 4, 5, 2, 3), (1, 4, 5, 3, 2), (1, 5, 2, 3, 4), (1, 5, 2, 4, 3), (1, 5, 3, 2, 4), (1, 5, 3, 4, 2), (1, 5, 4, 2, 3), (1, 5, 4, 3, 2), (2, 1, 3, 4, 5), (2, 1, 3, 5, 4), (2, 1, 4, 3, 5), (2, 1, 4, 5, 3), (2, 1, 5, 3, 4), (2, 1, 5, 4, 3), (2, 3, 1, 4, 5), (2, 3, 1, 5, 4), (2, 3, 4, 1, 5), (2, 3, 4, 5, 1), (2, 3, 5, 1, 4), (2, 3, 5, 4, 1), (2, 4, 1, 3, 5), (2, 4, 1, 5, 3), (2, 4, 3, 1, 5), (2, 4, 3, 5, 1), (2, 4, 5, 1, 3), (2, 4, 5, 3, 1), (2, 5, 1, 3, 4), (2, 5, 1, 4, 3), (2, 5, 3, 1, 4), (2, 5, 3, 4, 1), (2, 5, 4, 1, 3), (2, 5, 4, 3, 1), (3, 1, 2, 4, 5), (3, 1, 2, 5, 4), (3, 1, 4, 2, 5), (3, 1, 4, 5, 2), (3, 1, 5, 2, 4), (3, 1, 5, 4, 2), (3, 2, 1, 4, 5), (3, 2, 1, 5, 4), (3, 2, 4, 1, 5), (3, 2, 4, 5, 1), (3, 2, 5, 1, 4), (3, 2, 5, 4, 1), (3, 4, 1, 2, 5), (3, 4, 1, 5, 2), (3, 4, 2, 1, 5), (3, 4, 2, 5, 1), (3, 4, 5, 1, 2), (3, 4, 5, 2, 1), (3, 5, 1, 2, 4), (3, 5, 1, 4, 2), (3, 5, 2, 1, 4), (3, 5, 2, 4, 1), (3, 5, 4, 1, 2), (3, 5, 4, 2, 1), (4, 1, 2, 3, 5), (4, 1, 2, 5, 3), (4, 1, 3, 2, 5), (4, 1, 3, 5, 2), (4, 1, 5, 2, 3), (4, 1, 5, 3, 2), (4, 2, 1, 3, 5), (4, 2, 1, 5, 3), (4, 2, 3, 1, 5), (4, 2, 3, 5, 1), (4, 2, 5, 1, 3), (4, 2, 5, 3, 1), (4, 3, 1, 2, 5), (4, 3, 1, 5, 2), (4, 3, 2, 1, 5), (4, 3, 2, 5, 1), (4, 3, 5, 1, 2), (4, 3, 5, 2, 1), (4, 5, 1, 2, 3), (4, 5, 1, 3, 2), (4, 5, 2, 1, 3), (4, 5, 2, 3, 1), (4, 5, 3, 1, 2), (4, 5, 3, 2, 1), (5, 1, 2, 3, 4), (5, 1, 2, 4, 3), (5, 1, 3, 2, 4), (5, 1, 3, 4, 2), (5, 1, 4, 2, 3), (5, 1, 4, 3, 2), (5, 2, 1, 3, 4), (5, 2, 1, 4, 3), (5, 2, 3, 1, 4), (5, 2, 3, 4, 1), (5, 2, 4, 1, 3), (5, 2, 4, 3, 1), (5, 3, 1, 2, 4), (5, 3, 1, 4, 2), (5, 3, 2, 1, 4), (5, 3, 2, 4, 1), (5, 3, 4, 1, 2), (5, 3, 4, 2, 1), (5, 4, 1, 2, 3), (5, 4, 1, 3, 2), (5, 4, 2, 1, 3), (5, 4, 2, 3, 1), (5, 4, 3, 1, 2), (5, 4, 3, 2, 1)]
815816

816817
- name: Give me permutations of sets of three
817818
ansible.builtin.debug:
818819
msg: "{{ [1,2,3,4,5] | ansible.builtin.permutations(3) | list }}"
820+
# => [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 5, 2), (1, 5, 3), (1, 5, 4), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 5, 1), (2, 5, 3), (2, 5, 4), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 5, 1), (3, 5, 2), (3, 5, 4), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 5, 1), (4, 5, 2), (4, 5, 3), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 4, 1), (5, 4, 2), (5, 4, 3)]
819821

820822
combinations
821823
^^^^^^^^^^^^
@@ -826,26 +828,25 @@ Combinations always require a set size:
826828
- name: Give me combinations for sets of two
827829
ansible.builtin.debug:
828830
msg: "{{ [1,2,3,4,5] | ansible.builtin.combinations(2) | list }}"
831+
# => [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
829832

830833
Also see the :ref:`zip_filter`
831834

832835
products
833836
^^^^^^^^
834837
The product filter returns the `cartesian product <https://docs.python.org/3/library/itertools.html#itertools.product>`_ of the input iterables. This is roughly equivalent to nested for-loops in a generator expression.
835838

836-
For example:
837-
838839
.. code-block:: yaml+jinja
839840

840-
- name: Generate multiple hostnames
841-
ansible.builtin.debug:
842-
msg: "{{ ['foo', 'bar'] | product(['com']) | map('join', '.') | join(',') }}"
841+
{{ ['foo', 'bar'] | product(['com']) }}
842+
# => [('foo', 'com'), ('bar', 'com')]
843843

844-
This would result in:
844+
For example Generate multiple hostnames:
845845

846-
.. code-block:: json
846+
.. code-block:: yaml+jinja
847847

848-
{ "msg": "foo.com,bar.com" }
848+
{{ ['foo', 'bar'] | product(['com']) | map('join', '.') }}
849+
# => ['foo.com', 'bar.com']
849850

850851
.. json_query_filter:
851852
@@ -1111,7 +1112,8 @@ To get the minimum value from the list of numbers:
11111112

11121113
.. code-block:: jinja
11131114
1114-
{{ list1 | min }}
1115+
{{ [3, 4, 2] | min }}
1116+
# => 2
11151117
11161118
.. versionadded:: 2.11
11171119

@@ -1120,12 +1122,14 @@ To get the minimum value in a list of objects:
11201122
.. code-block:: jinja
11211123
11221124
{{ [{'val': 1}, {'val': 2}] | min(attribute='val') }}
1125+
# => {'val': 1}
11231126
11241127
To get the maximum value from a list of numbers:
11251128

11261129
.. code-block:: jinja
11271130
11281131
{{ [3, 4, 2] | max }}
1132+
# => 4
11291133
11301134
.. versionadded:: 2.11
11311135

@@ -1134,6 +1138,7 @@ To get the maximum value in a list of objects:
11341138
.. code-block:: jinja
11351139
11361140
{{ [{'val': 1}, {'val': 2}] | max(attribute='val') }}
1141+
# => {'val': 2}
11371142
11381143
.. versionadded:: 2.5
11391144

@@ -1154,13 +1159,21 @@ Flatten only the first level of a list (akin to the `items` lookup):
11541159
11551160
.. versionadded:: 2.11
11561161

1157-
Preserve nulls in a list, by default flatten removes them. :
1162+
Preserve nulls in a list (flatten removes them by default):
11581163

11591164
.. code-block:: jinja
11601165
11611166
{{ [3, None, [4, [2]] ] | flatten(levels=1, skip_nulls=False) }}
11621167
# => [3, None, 4, [2]]
11631168
1169+
Create a list from a list repeated N times:
1170+
1171+
.. code-block:: yaml+jinja
1172+
1173+
{{ 3*[1, 2, 3, "foo"] }}
1174+
# => [1, 2, 3, 'foo', 1, 2, 3, 'foo', 1, 2, 3, 'foo']
1175+
1176+
11641177

11651178
.. _set_theory_filters:
11661179

0 commit comments

Comments
 (0)