diff --git a/changelogs/fragments/keep_keys_greedy.yaml b/changelogs/fragments/keep_keys_greedy.yaml new file mode 100644 index 0000000..b0c35ec --- /dev/null +++ b/changelogs/fragments/keep_keys_greedy.yaml @@ -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. diff --git a/plugins/plugin_utils/keep_keys.py b/plugins/plugin_utils/keep_keys.py index 2390a56..de77ade 100644 --- a/plugins/plugin_utils/keep_keys.py +++ b/plugins/plugin_utils/keep_keys.py @@ -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 diff --git a/tests/integration/inventory b/tests/integration/inventory new file mode 100644 index 0000000..59c74c0 --- /dev/null +++ b/tests/integration/inventory @@ -0,0 +1,2 @@ +[testgroup] +testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/home/roverflow/workspace/virtualvenvs/a216p312/bin/python" diff --git a/tests/integration/targets/utils_keep_keys/tasks/simple.yaml b/tests/integration/targets/utils_keep_keys/tasks/simple.yaml index 822e590..6ef0506 100644 --- a/tests/integration/targets/utils_keep_keys/tasks/simple.yaml +++ b/tests/integration/targets/utils_keep_keys/tasks/simple.yaml @@ -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'] diff --git a/tests/integration/targets/utils_keep_keys/vars/main.yaml b/tests/integration/targets/utils_keep_keys/vars/main.yaml index 1814789..f520fe0 100644 --- a/tests/integration/targets/utils_keep_keys/vars/main.yaml +++ b/tests/integration/targets/utils_keep_keys/vars/main.yaml @@ -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