Skip to content

Commit aa44958

Browse files
Fixes issue with keep_keys removing all keys when passed a dict. (#370)
* Fixes issue with keep_keys removing all keys when data is passed in as dict * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed var name * updated changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e594aa6 commit aa44958

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- keep_keys - Fixes issue where all keys are removed when data is passed in as a dict.

plugins/plugin_utils/keep_keys.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ def keep_keys_from_dict_n_list(data, target, matching_parameter):
3535
if isinstance(data, dict):
3636
keep = {}
3737
for k, val in data.items():
38+
match = False
3839
for key in target:
39-
match = None
4040
if not isinstance(val, (list, dict)):
4141
if matching_parameter == "regex":
42-
match = re.match(key, k)
43-
if match:
44-
keep[k] = val
42+
if re.match(key, k):
43+
keep[k], match = val, True
4544
elif matching_parameter == "starts_with":
4645
if k.startswith(key):
4746
keep[k], match = val, True
@@ -53,13 +52,14 @@ def keep_keys_from_dict_n_list(data, target, matching_parameter):
5352
keep[k], match = val, True
5453
else:
5554
list_data = keep_keys_from_dict_n_list(val, target, matching_parameter)
56-
if isinstance(list_data, list) and not match:
55+
if isinstance(list_data, list):
5756
list_data = [r for r in list_data if r not in ([], {})]
58-
if all(isinstance(s, str) for s in list_data):
59-
continue
60-
if list_data in ([], {}):
61-
continue
62-
keep[k] = list_data
57+
if list_data not in ([], {}):
58+
keep[k], match = list_data, True
59+
if not match and isinstance(val, (list, dict)):
60+
nested_keep = keep_keys_from_dict_n_list(val, target, matching_parameter)
61+
if nested_keep not in ([], {}):
62+
keep[k] = nested_keep
6363
return keep
6464
return data
6565

tests/integration/targets/utils_keep_keys/tasks/simple.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
- name: Assert result dicts
3434
ansible.builtin.assert:
3535
that:
36-
- keep['starts_with'] == result['msg']
36+
- keep_values['starts_with'] == result['msg']
3737

3838
- name: Setup data as facts for equivalent
3939
ansible.builtin.set_fact:
@@ -70,3 +70,27 @@
7070
ansible.builtin.assert:
7171
that:
7272
- keep_default['default'] == result['msg']
73+
74+
- name: Setup data for multiple keys in dict
75+
ansible.builtin.set_fact:
76+
tomcat_data:
77+
tomcat:
78+
tomcat1:
79+
name: tomcat1
80+
tomcat2:
81+
name: tomcat2
82+
tomcat3:
83+
name: tomcat3
84+
tomcats_block:
85+
- tomcat1
86+
- tomcat2
87+
88+
- name: Debug
89+
ansible.builtin.debug:
90+
msg: "{{ tomcat_data | ansible.utils.keep_keys(target=['tomcats_block']) }}"
91+
register: result
92+
93+
- name: Assert result dicts
94+
ansible.builtin.assert:
95+
that:
96+
- keep_tomcat['tomcat'] == result['msg']

tests/integration/targets/utils_keep_keys/vars/main.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
keep:
2+
keep_values:
33
starts_with:
44
- interface_name: eth0
55
- interface_name: eth1
@@ -22,3 +22,9 @@ keep_default:
2222
is_enabled: true
2323
- interface_name: eth2
2424
is_enabled: false
25+
26+
keep_tomcat:
27+
tomcat:
28+
tomcats_block:
29+
- tomcat1
30+
- tomcat2

0 commit comments

Comments
 (0)