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
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

@ -1,79 +0,0 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
tags: ansible_version
- name: Test keep keys equal (default)
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ list1 | community.general.keep_keys(target=tt) }}"
tt: [k0_x0, k1_x1]
tags: equal_default
- name: Test keep keys regex string
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ list1 | community.general.keep_keys(target=tt, matching_parameter=mp) }}"
mp: regex
tt: '^.*[01]_x.*$'
tags: regex_string
- name: Test keep keys targets1
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets1 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list1 | community.general.keep_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets1
- name: Test keep keys targets2
ansible.builtin.assert:
that:
- (rr | difference(result2) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets2 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list2 | community.general.keep_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets2

View file

@ -3,5 +3,5 @@
# 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
- name: Test keep_keys
import_tasks: keep_keys.yml
- name: Tests
import_tasks: tests.yml

View file

@ -0,0 +1,31 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
tags: ansible_version
- name: Tests
ansible.builtin.assert:
that:
- (result | difference(i.0.result) | length) == 0
success_msg: |
[OK] result:
{{ result | to_yaml }}
fail_msg: |
[ERR] result:
{{ result | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ tests | subelements('group') }}"
loop_control:
loop_var: i
label: "{{ i.1.mp | d('default') }}: {{ i.1.tt }}"
vars:
input: "{{ i.0.input }}"
target: "{{ i.1.tt }}"
mp: "{{ i.1.mp | d('default') }}"
result: "{{ lookup('template', i.0.template) }}"

View file

@ -0,0 +1 @@
{{ input | community.general.keep_keys(target=target) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -0,0 +1 @@
{{ input | community.general.keep_keys(target=target, matching_parameter=mp) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -1,33 +0,0 @@
---
# 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
targets1:
- {mp: equal, tt: [k0_x0, k1_x1]}
- {mp: starts_with, tt: [k0, k1]}
- {mp: ends_with, tt: [x0, x1]}
- {mp: regex, tt: ['^.*[01]_x.*$']}
- {mp: regex, tt: '^.*[01]_x.*$'}
list1:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
result1:
- {k0_x0: A0, k1_x1: B0}
- {k0_x0: A1, k1_x1: B1}
targets2:
- {mp: equal, tt: k0_x0}
- {mp: starts_with, tt: k0}
- {mp: ends_with, tt: x0}
- {mp: regex, tt: '^.*0_x.*$'}
list2:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
result2:
- {k0_x0: A0}
- {k0_x0: A1}

View file

@ -0,0 +1,40 @@
---
# 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
tests:
- template: default.j2
group:
- {tt: [k0_x0, k1_x1], d: 'By default, match keys that equal any of the items in the target.'}
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}
result:
- {k0_x0: A0, k1_x1: B0}
- {k0_x0: A1, k1_x1: B1}
- template: mp.j2
group:
- {mp: equal, tt: [k0_x0, k1_x1], d: Match keys that equal any of the items in the target.}
- {mp: starts_with, tt: [k0, k1], d: Match keys that start with any of the items in the target.}
- {mp: ends_with, tt: [x0, x1], d: Match keys that end with any of the items in target.}
- {mp: regex, tt: ['^.*[01]_x.*$'], d: Match keys by the regex.}
- {mp: regex, tt: '^.*[01]_x.*$', d: Match keys by the regex.}
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}
result:
- {k0_x0: A0, k1_x1: B0}
- {k0_x0: A1, k1_x1: B1}
- template: mp.j2
group:
- {mp: equal, tt: k0_x0, d: Match keys that equal the target.}
- {mp: starts_with, tt: k0, d: Match keys that start with the target.}
- {mp: ends_with, tt: x0, d: Match keys that end with the target.}
- {mp: regex, tt: '^.*0_x.*$', d: Match keys by the regex.}
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}
result:
- {k0_x0: A0}
- {k0_x0: A1}

View file

@ -3,5 +3,5 @@
# 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
- name: Test remove_keys
import_tasks: remove_keys.yml
- name: Tests
import_tasks: tests.yml

View file

@ -1,79 +0,0 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
tags: ansible_version
- name: Test remove keys equal (default)
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ list1 | community.general.remove_keys(target=tt) }}"
tt: [k0_x0, k1_x1]
tags: equal_default
- name: Test remove keys regex string
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ list1 | community.general.remove_keys(target=tt, matching_parameter=mp) }}"
mp: regex
tt: '^.*[01]_x.*$'
tags: regex_string
- name: Test remove keys targets1
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets1 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list1 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets1
- name: Test remove keys targets2
ansible.builtin.assert:
that:
- (rr | difference(result2) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets2 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list2 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets1

View file

@ -0,0 +1,31 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
tags: ansible_version
- name: Tests
ansible.builtin.assert:
that:
- (result | difference(i.0.result) | length) == 0
success_msg: |
[OK] result:
{{ result | to_yaml }}
fail_msg: |
[ERR] result:
{{ result | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ tests | subelements('group') }}"
loop_control:
loop_var: i
label: "{{ i.1.mp | d('default') }}: {{ i.1.tt }}"
vars:
input: "{{ i.0.input }}"
target: "{{ i.1.tt }}"
mp: "{{ i.1.mp | d('default') }}"
result: "{{ lookup('template', i.0.template) }}"

View file

@ -0,0 +1 @@
{{ input | community.general.remove_keys(target=target) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -0,0 +1 @@
{{ input | community.general.remove_keys(target=target, matching_parameter=mp) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -1,33 +0,0 @@
---
# 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
targets1:
- {mp: equal, tt: [k0_x0, k1_x1]}
- {mp: starts_with, tt: [k0, k1]}
- {mp: ends_with, tt: [x0, x1]}
- {mp: regex, tt: ['^.*[01]_x.*$']}
- {mp: regex, tt: '^.*[01]_x.*$'}
list1:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
result1:
- {k2_x2: [C0], k3_x3: foo}
- {k2_x2: [C1], k3_x3: bar}
targets2:
- {mp: equal, tt: k0_x0}
- {mp: starts_with, tt: k0}
- {mp: ends_with, tt: x0}
- {mp: regex, tt: '^.*0_x.*$'}
list2:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
result2:
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}

View file

@ -0,0 +1,40 @@
---
# 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
tests:
- template: default.j2
group:
- {tt: [k0_x0, k1_x1], d: 'By default, match keys that equal any of the items in the target.'}
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}
result:
- {k2_x2: [C0], k3_x3: foo}
- {k2_x2: [C1], k3_x3: bar}
- template: mp.j2
group:
- {mp: equal, tt: [k0_x0, k1_x1], d: Match keys that equal any of the items in the target.}
- {mp: starts_with, tt: [k0, k1], d: Match keys that start with any of the items in the target.}
- {mp: ends_with, tt: [x0, x1], d: Match keys that end with any of the items in target.}
- {mp: regex, tt: ['^.*[01]_x.*$'], d: Match keys by the regex.}
- {mp: regex, tt: '^.*[01]_x.*$', d: Match keys by the regex.}
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}
result:
- {k2_x2: [C0], k3_x3: foo}
- {k2_x2: [C1], k3_x3: bar}
- template: mp.j2
group:
- {mp: equal, tt: k0_x0, d: Match keys that equal the target.}
- {mp: starts_with, tt: k0, d: Match keys that start with the target.}
- {mp: ends_with, tt: x0, d: Match keys that end with the target.}
- {mp: regex, tt: '^.*0_x.*$', d: Match keys by the regex.}
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}
result:
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}

View file

@ -1,21 +0,0 @@
---
# 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
- name: Test replace keys
ansible.builtin.assert:
that:
- (rr | difference(item.result) | length) == 0
success_msg: |
[OK] {{ item.label }}
result:
{{ rr | to_nice_yaml(indent=2) | indent(2) }}
fail_msg: |
[ERR] {{ item.label }}
result:
{{ rr | to_nice_yaml(indent=2) | indent(2) }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ item.data |
community.general.replace_keys(target=item.target, matching_parameter=item.match) }}"

View file

@ -3,5 +3,5 @@
# 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
- name: Test replace_keys
import_tasks: replace_keys.yml
- name: Tests
import_tasks: tests.yml

View file

@ -1,56 +0,0 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quiet_test | d(true) | bool
tags: ansible_version
- name: Test replace keys equal (default)
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
vars:
rr: "{{ list1 | community.general.replace_keys(target=tt) }}"
tt:
- {before: k0_x0, after: a0}
- {before: k1_x1, after: a1}
tags: equal_default
- name: Test replace keys targets1
ansible.builtin.assert:
that:
- (rr | difference(result1) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets1 | dict2items }}"
loop_control:
label: "{{ item.key }}"
vars:
rr: "{{ list1 | community.general.replace_keys(target=item.value, matching_parameter=item.key) }}"
tags: targets1
- name: Test replace keys targets2
include_tasks:
file: fn-test-replace_keys.yml
apply:
tags: targets2
loop: "{{ targets2 }}"
loop_control:
label: "{{ item.label }}"
tags: targets2

View file

@ -0,0 +1,31 @@
---
# 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
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
tags: ansible_version
- name: Tests
ansible.builtin.assert:
that:
- (result | difference(i.0.result) | length) == 0
success_msg: |
[OK] result:
{{ result | to_yaml }}
fail_msg: |
[ERR] result:
{{ result | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ tests | subelements('group') }}"
loop_control:
loop_var: i
label: "{{ i.1.mp | d('default') }}: {{ i.1.tt }}"
vars:
input: "{{ i.0.input }}"
target: "{{ i.1.tt }}"
mp: "{{ i.1.mp | d('default') }}"
result: "{{ lookup('template', i.0.template) }}"

View file

@ -0,0 +1 @@
{{ input | community.general.replace_keys(target=target) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -0,0 +1 @@
{{ input | community.general.replace_keys(target=target, matching_parameter=mp) }}

View file

@ -0,0 +1,3 @@
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

View file

@ -1,58 +0,0 @@
---
# 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
list1:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
result1:
- {a0: A0, a1: B0, k2_x2: [C0], k3_x3: foo}
- {a0: A1, a1: B1, k2_x2: [C1], k3_x3: bar}
targets1:
equal:
- {before: k0_x0, after: a0}
- {before: k1_x1, after: a1}
starts_with:
- {before: k0, after: a0}
- {before: k1, after: a1}
ends_with:
- {before: x0, after: a0}
- {before: x1, after: a1}
regex:
- {before: "^.*0_x.*$", after: a0}
- {before: "^.*1_x.*$", after: a1}
list2:
- {aaa1: A, bbb1: B, ccc1: C}
- {aaa2: D, bbb2: E, ccc2: F}
targets2:
- label: If more keys match the same attribute before the last one will be used.
match: regex
target:
- {before: "^.*_x.*$", after: X}
data: "{{ list1 }}"
result:
- X: foo
- X: bar
- label: If there are items with equal attribute before the first one will be used.
match: regex
target:
- {before: "^.*_x.*$", after: X}
- {before: "^.*_x.*$", after: Y}
data: "{{ list1 }}"
result:
- X: foo
- X: bar
- label: If there are more matches for a key the first one will be used.
match: starts_with
target:
- {before: a, after: X}
- {before: aa, after: Y}
data: "{{ list2 }}"
result:
- {X: A, bbb1: B, ccc1: C}
- {X: D, bbb2: E, ccc2: F}

View file

@ -0,0 +1,71 @@
---
# 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
tests:
- template: default.j2
group:
- d: By default, match keys that equal any of the attributes before.
tt:
- {before: k0_x0, after: a0}
- {before: k1_x1, after: a1}
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}
result:
- {a0: A0, a1: B0, k2_x2: [C0], k3_x3: foo}
- {a0: A1, a1: B1, k2_x2: [C1], k3_x3: bar}
- template: mp.j2
group:
- d: Replace keys that starts with any of the attributes before.
mp: starts_with
tt:
- {before: k0, after: a0}
- {before: k1, after: a1}
- d: Replace keys that ends with any of the attributes before.
mp: ends_with
tt:
- {before: x0, after: a0}
- {before: x1, after: a1}
- d: Replace keys that match any regex of the attributes before.
mp: regex
tt:
- {before: "^.*0_x.*$", after: a0}
- {before: "^.*1_x.*$", after: a1}
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}
result:
- {a0: A0, a1: B0, k2_x2: [C0], k3_x3: foo}
- {a0: A1, a1: B1, k2_x2: [C1], k3_x3: bar}
- template: mp.j2
group:
- d: If more keys match the same attribute before the last one will be used.
mp: regex
tt:
- {before: "^.*_x.*$", after: X}
- d: If there are items with equal attribute before the first one will be used.
mp: regex
tt:
- {before: "^.*_x.*$", after: X}
- {before: "^.*_x.*$", after: Y}
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}
result:
- X: foo
- X: bar
- template: mp.j2
group:
- d: If there are more matches for a key the first one will be used.
mp: starts_with
tt:
- {before: a, after: X}
- {before: aa, after: Y}
input:
- {aaa1: A, bbb1: B, ccc1: C}
- {aaa2: D, bbb2: E, ccc2: F}
result:
- {X: A, bbb1: B, ccc1: C}
- {X: D, bbb2: E, ccc2: F}