Fixes keep_keys filter to retain the entire node when a key match occurs, rather than just the leaf node values. (#377)

* Fix keep_keys not keeping the values if they are not a string

* added changelog

* remove inventory

* add tests

* added more test
pull/378/head
Vinay M 2024-09-27 19:53:27 +05:30 committed by GitHub
parent c4ace98702
commit d902c0f4f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,3 @@
---
bugfixes:
- keep_keys - Fixes keep_keys filter to retain the entire node when a key match occurs, rather than just the leaf node values.

View File

@ -37,7 +37,9 @@ def keep_keys_from_dict_n_list(data, target, matching_parameter):
for k, val in data.items():
match = False
for key in target:
if not isinstance(val, (list, dict)):
if k == key:
keep[k], match = val, True
elif not isinstance(val, (list, dict)):
if matching_parameter == "regex":
if re.match(key, k):
keep[k], match = val, True

View File

@ -0,0 +1,2 @@
[testgroup]
testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/home/roverflow/workspace/virtualvenvs/a216p312/bin/python"

View File

@ -79,12 +79,19 @@
name: tomcat1
tomcat2:
name: tomcat2
tomcat3:
name: tomcat3
tomcat3:
name: tomcat3
tomcat4:
name: tomcat3
tomcat3:
tomcat3:
name: tomcattest
checktomcat3: stringinput
tomcats_block:
- tomcat1
- tomcat2
- name: Debug
ansible.builtin.debug:
msg: "{{ tomcat_data | ansible.utils.keep_keys(target=['tomcats_block']) }}"
@ -94,3 +101,23 @@
ansible.builtin.assert:
that:
- keep_tomcat['tomcat'] == result['msg']
- name: Test to keep keys when initial node is matched
ansible.builtin.debug:
msg: "{{ tomcat_data['tomcat'] | ansible.utils.keep_keys(target=['tomcat1', 'tomcat2']) }}"
register: result
- name: Assert result dicts
ansible.builtin.assert:
that:
- keep_tomcat['greedy_values'] == result['msg']
- name: Test to keep keys to check if nested dict is matched
ansible.builtin.debug:
msg: "{{ tomcat_data['tomcat'] | ansible.utils.keep_keys(target=['tomcat3']) }}"
register: result
- name: Assert result dicts
ansible.builtin.assert:
that:
- keep_tomcat['nested_values'] == result['msg']

View File

@ -28,3 +28,21 @@ keep_tomcat:
tomcats_block:
- tomcat1
- tomcat2
greedy_values:
tomcat1:
name: tomcat1
tomcat2:
name: tomcat2
tomcat3:
name: tomcat3
nested_values:
tomcat2:
tomcat3:
name: tomcat3
tomcat3:
name: tomcat3
tomcat4:
tomcat3:
tomcat3:
name: tomcattest
checktomcat3: stringinput