Feature. Add chapter 'Lists of dictionaries' to docsite (#8482)

* Feature. Add chapter 'Lists of dictionaries'

* Fix copyright and licensing.

* Add maintainers for docsite chapter 'Lists of dictionaries'.

* Generate docs keep_keys and remove_keys

* Update integration tests of keep_keys and remove_keys
* Update docs helpers of keep_keys and remove_keys

* Fix copyright and licensing.

* Fix remove license from templates. Cleanup.

* Add docs helper replace_keys

* Update integration test filter_replace_keys
* Generate and update:
  filter_guide-abstract_informations-lists_of_dictionaries-replace_keys.rst

* Formatting improved.

* Fix results Jinja quotation marks.

* Update docs/docsite/helper/keep_keys/filter_guide-abstract_informations-lists_of_dictionaries-keep_keys.rst.j2

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update docs/docsite/helper/keep_keys/filter_guide-abstract_informations-lists_of_dictionaries-keep_keys.rst.j2

Co-authored-by: Felix Fontein <felix@fontein.de>

* Fix references.

* Updated helpers.

* Fix licenses. Simplified templates.

* Fix licenses.

* Fix README.

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Vladimir Botka 2024-07-05 08:42:35 +02:00 committed by GitHub
parent 5259caacae
commit caecb2297f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
55 changed files with 1500 additions and 365 deletions

View file

@ -0,0 +1,151 @@
..
Copyright (c) Ansible Project
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
keep_keys
"""""""""
Use the filter :ansplugin:`community.general.keep_keys#filter` if you have a list of dictionaries and want to keep certain keys only.
.. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See :ansplugin:`the documentation for the community.general.yaml callback plugin <community.general.yaml#callback>`.
Let us use the below list in the following examples:
.. code-block:: yaml
input:
- k0_x0: A0
k1_x1: B0
k2_x2: [C0]
k3_x3: foo
- k0_x0: A1
k1_x1: B1
k2_x2: [C1]
k3_x3: bar
* By default, match keys that equal any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1
target: ['k0_x0', 'k1_x1']
result: "{{ input | community.general.keep_keys(target=target) }}"
gives
.. code-block:: yaml
:emphasize-lines: 1-
result:
- {k0_x0: A0, k1_x1: B0}
- {k0_x0: A1, k1_x1: B1}
.. versionadded:: 9.1.0
* The results of the below examples 1-5 are all the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- {k0_x0: A0, k1_x1: B0}
- {k0_x0: A1, k1_x1: B1}
1. Match keys that equal any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: equal
target: ['k0_x0', 'k1_x1']
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
2. Match keys that start with any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: starts_with
target: ['k0', 'k1']
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
3. Match keys that end with any of the items in target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: ends_with
target: ['x0', 'x1']
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
4. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ['^.*[01]_x.*$']
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
5. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ^.*[01]_x.*$
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
* The results of the below examples 6-9 are all the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- {k0_x0: A0}
- {k0_x0: A1}
6. Match keys that equal the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: equal
target: k0_x0
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
7. Match keys that start with the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: starts_with
target: k0
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
8. Match keys that end with the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: ends_with
target: x0
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"
9. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ^.*0_x.*$
result: "{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}"

View file

@ -0,0 +1,159 @@
..
Copyright (c) Ansible Project
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
remove_keys
"""""""""""
Use the filter :ansplugin:`community.general.remove_keys#filter` if you have a list of dictionaries and want to remove certain keys.
.. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See See :ansplugin:`the documentation for the community.general.yaml callback plugin <community.general.yaml#callback>`.
Let us use the below list in the following examples:
.. code-block:: yaml
input:
- k0_x0: A0
k1_x1: B0
k2_x2: [C0]
k3_x3: foo
- k0_x0: A1
k1_x1: B1
k2_x2: [C1]
k3_x3: bar
* By default, match keys that equal any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1
target: ['k0_x0', 'k1_x1']
result: "{{ input | community.general.remove_keys(target=target) }}"
gives
.. code-block:: yaml
:emphasize-lines: 1-
result:
- k2_x2: [C0]
k3_x3: foo
- k2_x2: [C1]
k3_x3: bar
.. versionadded:: 9.1.0
* The results of the below examples 1-5 are all the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- k2_x2: [C0]
k3_x3: foo
- k2_x2: [C1]
k3_x3: bar
1. Match keys that equal any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: equal
target: ['k0_x0', 'k1_x1']
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
2. Match keys that start with any of the items in the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: starts_with
target: ['k0', 'k1']
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
3. Match keys that end with any of the items in target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: ends_with
target: ['x0', 'x1']
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
4. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ['^.*[01]_x.*$']
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
5. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ^.*[01]_x.*$
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
* The results of the below examples 6-9 are all the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- k1_x1: B0
k2_x2: [C0]
k3_x3: foo
- k1_x1: B1
k2_x2: [C1]
k3_x3: bar
6. Match keys that equal the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: equal
target: k0_x0
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
7. Match keys that start with the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: starts_with
target: k0
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
8. Match keys that end with the target.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: ends_with
target: x0
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"
9. Match keys by the regex.
.. code-block:: yaml+jinja
:emphasize-lines: 1,2
mp: regex
target: ^.*0_x.*$
result: "{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}"

View file

@ -0,0 +1,175 @@
..
Copyright (c) Ansible Project
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
replace_keys
""""""""""""
Use the filter :ansplugin:`community.general.replace_keys#filter` if you have a list of dictionaries and want to replace certain keys.
.. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See :ansplugin:`the documentation for the community.general.yaml callback plugin <community.general.yaml#callback>`.
Let us use the below list in the following examples:
.. code-block:: yaml
input:
- k0_x0: A0
k1_x1: B0
k2_x2: [C0]
k3_x3: foo
- k0_x0: A1
k1_x1: B1
k2_x2: [C1]
k3_x3: bar
* By default, match keys that equal any of the attributes before.
.. code-block:: yaml+jinja
:emphasize-lines: 1-3
target:
- {after: a0, before: k0_x0}
- {after: a1, before: k1_x1}
result: "{{ input | community.general.replace_keys(target=target) }}"
gives
.. code-block:: yaml
:emphasize-lines: 1-
result:
- a0: A0
a1: B0
k2_x2: [C0]
k3_x3: foo
- a0: A1
a1: B1
k2_x2: [C1]
k3_x3: bar
.. versionadded:: 9.1.0
* The results of the below examples 1-3 are all the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- a0: A0
a1: B0
k2_x2: [C0]
k3_x3: foo
- a0: A1
a1: B1
k2_x2: [C1]
k3_x3: bar
1. Replace keys that starts with any of the attributes before.
.. code-block:: yaml+jinja
:emphasize-lines: 1-4
mp: starts_with
target:
- {after: a0, before: k0}
- {after: a1, before: k1}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
2. Replace keys that ends with any of the attributes before.
.. code-block:: yaml+jinja
:emphasize-lines: 1-4
mp: ends_with
target:
- {after: a0, before: x0}
- {after: a1, before: x1}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
3. Replace keys that match any regex of the attributes before.
.. code-block:: yaml+jinja
:emphasize-lines: 1-4
mp: regex
target:
- {after: a0, before: ^.*0_x.*$}
- {after: a1, before: ^.*1_x.*$}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
* The results of the below examples 4-5 are the same:
.. code-block:: yaml
:emphasize-lines: 1-
result:
- {X: foo}
- {X: bar}
4. If more keys match the same attribute before the last one will be used.
.. code-block:: yaml+jinja
:emphasize-lines: 1-3
mp: regex
target:
- {after: X, before: ^.*_x.*$}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
5. If there are items with equal attribute before the first one will be used.
.. code-block:: yaml+jinja
:emphasize-lines: 1-3
mp: regex
target:
- {after: X, before: ^.*_x.*$}
- {after: Y, before: ^.*_x.*$}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
6. If there are more matches for a key the first one will be used.
.. code-block:: yaml
:emphasize-lines: 1-
input:
- {aaa1: A, bbb1: B, ccc1: C}
- {aaa2: D, bbb2: E, ccc2: F}
.. code-block:: yaml+jinja
:emphasize-lines: 1-4
mp: starts_with
target:
- {after: X, before: a}
- {after: Y, before: aa}
result: "{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}"
gives
.. code-block:: yaml
:emphasize-lines: 1-
result:
- {X: A, bbb1: B, ccc1: C}
- {X: D, bbb2: E, ccc2: F}

View file

@ -0,0 +1,18 @@
..
Copyright (c) Ansible Project
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
.. _ansible_collections.community.general.docsite.filter_guide.filter_guide_abstract_informations.lists_of_dicts:
Lists of dictionaries
^^^^^^^^^^^^^^^^^^^^^
Filters to manage keys in a list of dictionaries:
.. toctree::
:maxdepth: 1
filter_guide-abstract_informations-lists_of_dictionaries-keep_keys
filter_guide-abstract_informations-lists_of_dictionaries-remove_keys
filter_guide-abstract_informations-lists_of_dictionaries-replace_keys

View file

@ -11,6 +11,7 @@ Abstract transformations
filter_guide_abstract_informations_dictionaries
filter_guide_abstract_informations_grouping
filter_guide-abstract_informations-lists_of_dictionaries
filter_guide_abstract_informations_merging_lists_of_dictionaries
filter_guide_abstract_informations_lists_helper
filter_guide_abstract_informations_counting_elements_in_sequence