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>pull/369/head
parent
e594aa6090
commit
aa44958bd2
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- keep_keys - Fixes issue where all keys are removed when data is passed in as a dict.
|
|
@ -35,13 +35,12 @@ def keep_keys_from_dict_n_list(data, target, matching_parameter):
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
keep = {}
|
keep = {}
|
||||||
for k, val in data.items():
|
for k, val in data.items():
|
||||||
|
match = False
|
||||||
for key in target:
|
for key in target:
|
||||||
match = None
|
|
||||||
if not isinstance(val, (list, dict)):
|
if not isinstance(val, (list, dict)):
|
||||||
if matching_parameter == "regex":
|
if matching_parameter == "regex":
|
||||||
match = re.match(key, k)
|
if re.match(key, k):
|
||||||
if match:
|
keep[k], match = val, True
|
||||||
keep[k] = val
|
|
||||||
elif matching_parameter == "starts_with":
|
elif matching_parameter == "starts_with":
|
||||||
if k.startswith(key):
|
if k.startswith(key):
|
||||||
keep[k], match = val, True
|
keep[k], match = val, True
|
||||||
|
@ -53,13 +52,14 @@ def keep_keys_from_dict_n_list(data, target, matching_parameter):
|
||||||
keep[k], match = val, True
|
keep[k], match = val, True
|
||||||
else:
|
else:
|
||||||
list_data = keep_keys_from_dict_n_list(val, target, matching_parameter)
|
list_data = keep_keys_from_dict_n_list(val, target, matching_parameter)
|
||||||
if isinstance(list_data, list) and not match:
|
if isinstance(list_data, list):
|
||||||
list_data = [r for r in list_data if r not in ([], {})]
|
list_data = [r for r in list_data if r not in ([], {})]
|
||||||
if all(isinstance(s, str) for s in list_data):
|
if list_data not in ([], {}):
|
||||||
continue
|
keep[k], match = list_data, True
|
||||||
if list_data in ([], {}):
|
if not match and isinstance(val, (list, dict)):
|
||||||
continue
|
nested_keep = keep_keys_from_dict_n_list(val, target, matching_parameter)
|
||||||
keep[k] = list_data
|
if nested_keep not in ([], {}):
|
||||||
|
keep[k] = nested_keep
|
||||||
return keep
|
return keep
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
- name: Assert result dicts
|
- name: Assert result dicts
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that:
|
that:
|
||||||
- keep['starts_with'] == result['msg']
|
- keep_values['starts_with'] == result['msg']
|
||||||
|
|
||||||
- name: Setup data as facts for equivalent
|
- name: Setup data as facts for equivalent
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
|
@ -70,3 +70,27 @@
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that:
|
that:
|
||||||
- keep_default['default'] == result['msg']
|
- keep_default['default'] == result['msg']
|
||||||
|
|
||||||
|
- name: Setup data for multiple keys in dict
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
tomcat_data:
|
||||||
|
tomcat:
|
||||||
|
tomcat1:
|
||||||
|
name: tomcat1
|
||||||
|
tomcat2:
|
||||||
|
name: tomcat2
|
||||||
|
tomcat3:
|
||||||
|
name: tomcat3
|
||||||
|
tomcats_block:
|
||||||
|
- tomcat1
|
||||||
|
- tomcat2
|
||||||
|
|
||||||
|
- name: Debug
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: "{{ tomcat_data | ansible.utils.keep_keys(target=['tomcats_block']) }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: Assert result dicts
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- keep_tomcat['tomcat'] == result['msg']
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
keep:
|
keep_values:
|
||||||
starts_with:
|
starts_with:
|
||||||
- interface_name: eth0
|
- interface_name: eth0
|
||||||
- interface_name: eth1
|
- interface_name: eth1
|
||||||
|
@ -22,3 +22,9 @@ keep_default:
|
||||||
is_enabled: true
|
is_enabled: true
|
||||||
- interface_name: eth2
|
- interface_name: eth2
|
||||||
is_enabled: false
|
is_enabled: false
|
||||||
|
|
||||||
|
keep_tomcat:
|
||||||
|
tomcat:
|
||||||
|
tomcats_block:
|
||||||
|
- tomcat1
|
||||||
|
- tomcat2
|
||||||
|
|
Loading…
Reference in New Issue