Skip to content

Commit 3a3bbfb

Browse files
vbotkafelixfontein
authored andcommitted
Feature filter remove_keys (ansible-collections#8443)
* Add filter remove_keys. * Add filter remove_keys integration test, fragment, and maintainer. * Update with plugins/plugin_utils/keys_filter.py * Update according PR ansible-collections#8456 * Update maintainers. * Fix typo in return doc. * Remove local keys_filter.py. Then rebase. * Add local keys_filter.py * Update plugins/filter/remove_keys.py Co-authored-by: Felix Fontein <[email protected]> * Update plugins/filter/remove_keys.py Co-authored-by: Felix Fontein <[email protected]> --------- Co-authored-by: Felix Fontein <[email protected]>
1 parent ec3b509 commit 3a3bbfb

File tree

6 files changed

+264
-0
lines changed

6 files changed

+264
-0
lines changed

.github/BOTMETA.yml

+2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ files:
172172
$filters/lists_union.yml:
173173
maintainers: cfiehe
174174
$filters/random_mac.py: {}
175+
$filters/remove_keys.py:
176+
maintainers: vbotka
175177
$filters/time.py:
176178
maintainers: resmo
177179
$filters/to_days.yml:

plugins/filter/remove_keys.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2024 Vladimir Botka <[email protected]>
3+
# Copyright (c) 2024 Felix Fontein <[email protected]>
4+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
7+
from __future__ import (absolute_import, division, print_function)
8+
__metaclass__ = type
9+
10+
DOCUMENTATION = '''
11+
name: remove_keys
12+
short_description: Remove specific keys from dictionaries in a list
13+
version_added: "9.1.0"
14+
author:
15+
- Vladimir Botka (@vbotka)
16+
- Felix Fontein (@felixfontein)
17+
description: This filter removes only specified keys from a provided list of dictionaries.
18+
options:
19+
_input:
20+
description:
21+
- A list of dictionaries.
22+
- Top level keys must be strings.
23+
type: list
24+
elements: dictionary
25+
required: true
26+
target:
27+
description:
28+
- A single key or key pattern to remove, or a list of keys or keys patterns to remove.
29+
- If O(matching_parameter=regex) there must be exactly one pattern provided.
30+
type: raw
31+
required: true
32+
matching_parameter:
33+
description: Specify the matching option of target keys.
34+
type: str
35+
default: equal
36+
choices:
37+
equal: Matches keys of exactly one of the O(target) items.
38+
starts_with: Matches keys that start with one of the O(target) items.
39+
ends_with: Matches keys that end with one of the O(target) items.
40+
regex:
41+
- Matches keys that match the regular expresion provided in O(target).
42+
- In this case, O(target) must be a regex string or a list with single regex string.
43+
'''
44+
45+
EXAMPLES = '''
46+
l:
47+
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
48+
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
49+
50+
# 1) By default match keys that equal any of the items in the target.
51+
t: [k0_x0, k1_x1]
52+
r: "{{ l | community.general.remove_keys(target=t) }}"
53+
54+
# 2) Match keys that start with any of the items in the target.
55+
t: [k0, k1]
56+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}"
57+
58+
# 3) Match keys that end with any of the items in target.
59+
t: [x0, x1]
60+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}"
61+
62+
# 4) Match keys by the regex.
63+
t: ['^.*[01]_x.*$']
64+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
65+
66+
# 5) Match keys by the regex.
67+
t: '^.*[01]_x.*$'
68+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
69+
70+
# The results of above examples 1-5 are all the same.
71+
r:
72+
- {k2_x2: [C0], k3_x3: foo}
73+
- {k2_x2: [C1], k3_x3: bar}
74+
75+
# 6) By default match keys that equal the target.
76+
t: k0_x0
77+
r: "{{ l | community.general.remove_keys(target=t) }}"
78+
79+
# 7) Match keys that start with the target.
80+
t: k0
81+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}"
82+
83+
# 8) Match keys that end with the target.
84+
t: x0
85+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}"
86+
87+
# 9) Match keys by the regex.
88+
t: '^.*0_x.*$'
89+
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
90+
91+
# The results of above examples 6-9 are all the same.
92+
r:
93+
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
94+
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}
95+
'''
96+
97+
RETURN = '''
98+
_value:
99+
description: The list of dictionaries with selected keys removed.
100+
type: list
101+
elements: dictionary
102+
'''
103+
104+
from ansible_collections.community.general.plugins.plugin_utils.keys_filter import (
105+
_keys_filter_params,
106+
_keys_filter_target_str)
107+
108+
109+
def remove_keys(data, target=None, matching_parameter='equal'):
110+
"""remove specific keys from dictionaries in a list"""
111+
112+
# test parameters
113+
_keys_filter_params(data, target, matching_parameter)
114+
# test and transform target
115+
tt = _keys_filter_target_str(target, matching_parameter)
116+
117+
if matching_parameter == 'equal':
118+
def keep_key(key):
119+
return key not in tt
120+
elif matching_parameter == 'starts_with':
121+
def keep_key(key):
122+
return not key.startswith(tt)
123+
elif matching_parameter == 'ends_with':
124+
def keep_key(key):
125+
return not key.endswith(tt)
126+
elif matching_parameter == 'regex':
127+
def keep_key(key):
128+
return tt.match(key) is None
129+
130+
return [dict((k, v) for k, v in d.items() if keep_key(k)) for d in data]
131+
132+
133+
class FilterModule(object):
134+
135+
def filters(self):
136+
return {
137+
'remove_keys': remove_keys,
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) Ansible Project
2+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
azp/posix/2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# Copyright (c) Ansible Project
3+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
6+
- name: Test remove_keys
7+
import_tasks: remove_keys.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
# Copyright (c) Ansible Project
3+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
6+
- name: Debug ansible_version
7+
ansible.builtin.debug:
8+
var: ansible_version
9+
when: not quite_test | d(true) | bool
10+
tags: ansible_version
11+
12+
- name: Test remove keys equal (default)
13+
ansible.builtin.assert:
14+
that:
15+
- (rr | difference(result1) | length) == 0
16+
success_msg: |
17+
[OK] result:
18+
{{ rr | to_yaml }}
19+
fail_msg: |
20+
[ERR] result:
21+
{{ rr | to_yaml }}
22+
quiet: "{{ quiet_test | d(true) | bool }}"
23+
vars:
24+
rr: "{{ list1 | community.general.remove_keys(target=tt) }}"
25+
tt: [k0_x0, k1_x1]
26+
tags: equal_default
27+
28+
- name: Test remove keys regex string
29+
ansible.builtin.assert:
30+
that:
31+
- (rr | difference(result1) | length) == 0
32+
success_msg: |
33+
[OK] result:
34+
{{ rr | to_yaml }}
35+
fail_msg: |
36+
[ERR] result:
37+
{{ rr | to_yaml }}
38+
quiet: "{{ quiet_test | d(true) | bool }}"
39+
vars:
40+
rr: "{{ list1 | community.general.remove_keys(target=tt, matching_parameter=mp) }}"
41+
mp: regex
42+
tt: '^.*[01]_x.*$'
43+
tags: regex_string
44+
45+
- name: Test remove keys targets1
46+
ansible.builtin.assert:
47+
that:
48+
- (rr | difference(result1) | length) == 0
49+
success_msg: |
50+
[OK] result:
51+
{{ rr | to_yaml }}
52+
fail_msg: |
53+
[ERR] result:
54+
{{ rr | to_yaml }}
55+
quiet: "{{ quiet_test | d(true) | bool }}"
56+
loop: "{{ targets1 }}"
57+
loop_control:
58+
label: "{{ item.mp }}: {{ item.tt }}"
59+
vars:
60+
rr: "{{ list1 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
61+
tags: targets1
62+
63+
- name: Test remove keys targets2
64+
ansible.builtin.assert:
65+
that:
66+
- (rr | difference(result2) | length) == 0
67+
success_msg: |
68+
[OK] result:
69+
{{ rr | to_yaml }}
70+
fail_msg: |
71+
[ERR] result:
72+
{{ rr | to_yaml }}
73+
quiet: "{{ quiet_test | d(true) | bool }}"
74+
loop: "{{ targets2 }}"
75+
loop_control:
76+
label: "{{ item.mp }}: {{ item.tt }}"
77+
vars:
78+
rr: "{{ list2 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
79+
tags: targets1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# Copyright (c) Ansible Project
3+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
6+
targets1:
7+
- {mp: equal, tt: [k0_x0, k1_x1]}
8+
- {mp: starts_with, tt: [k0, k1]}
9+
- {mp: ends_with, tt: [x0, x1]}
10+
- {mp: regex, tt: ['^.*[01]_x.*$']}
11+
- {mp: regex, tt: '^.*[01]_x.*$'}
12+
13+
list1:
14+
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
15+
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
16+
17+
result1:
18+
- {k2_x2: [C0], k3_x3: foo}
19+
- {k2_x2: [C1], k3_x3: bar}
20+
21+
targets2:
22+
- {mp: equal, tt: k0_x0}
23+
- {mp: starts_with, tt: k0}
24+
- {mp: ends_with, tt: x0}
25+
- {mp: regex, tt: '^.*0_x.*$'}
26+
27+
list2:
28+
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
29+
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
30+
31+
result2:
32+
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
33+
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}

0 commit comments

Comments
 (0)