This commit is contained in:
Lorenzo Tanganelli 2025-05-15 15:39:27 +00:00
commit 7bf066116b
2 changed files with 51 additions and 49 deletions

1
ansible Submodule

@ -0,0 +1 @@
Subproject commit fe2d9e316a38c96b798e830acc0a1314c35b8ef4

View file

@ -36,16 +36,16 @@ options:
default: true default: true
required: false required: false
matching_parameter: matching_parameter:
description: Specify the matching option of target keys. description: Specify the matching option of values.
type: str type: str
default: equal default: equal
choices: choices:
equal: Matches keys of equally one of the O(target[].before) items. equal: Matches values of equally one of the O(values[]) items.
regex: Matches keys that match one of the regular expressions provided in O(target[].before).: regex: Matches values that match one of the regular expressions provided in O(values[]).
""" """
EXAMPLES = r""" EXAMPLES = r"""
- name: Remove empty keys from a list of dictionaries - name: Remove empty values from a list of dictionaries
set_fact: set_fact:
my_list: my_list:
- a: foo - a: foo
@ -57,16 +57,16 @@ EXAMPLES = r"""
- a: ok - a: ok
b: {} b: {}
c: None c: None
- debug: - debug:
msg: "{{ my_list | remove_empty_keys }}" msg: "{{ my_list | remove_empty_keys }}"
- debug: - debug:
msg: "{{ my_list | remove_empty_keys(values='') }}" msg: "{{ my_list | remove_empty_keys(values='') }}"
- debug: - debug:
msg: "{{ my_list | remove_empty_keys(values=['', [], {}, None]) }}" msg: "{{ my_list | remove_empty_keys(values=['', [], {}, None]) }}"
- debug: - debug:
msg: "{{ my_list | remove_empty_keys(values=['', [], {}, None], recursive=False) }}" msg: "{{ my_list | remove_empty_keys(values=['', [], {}, None], recursive=False) }}"
- debug: - debug:
msg: "{{ my_list | remove_empty_keys(values=['foo', 'bar']) }}" msg: "{{ my_list | remove_empty_keys(values=['foo', 'bar']) }}"
- name: Remove keys from a dictionary - name: Remove keys from a dictionary
set_fact: set_fact:
@ -81,42 +81,42 @@ EXAMPLES = r"""
- a: bar - a: bar
b: {} b: {}
c: None c: None
- debug: - debug:
msg: "{{ my_dict | remove_empty_keys }}" msg: "{{ my_dict | remove_empty_keys }}"
# returns # returns
# a: foo # a: foo
# d: # d:
# - a: foo # - a: foo
# - a: bar # - a: bar
- debug: - debug:
msg: "{{ my_dict | remove_empty_keys(values='') }}" msg: "{{ my_dict | remove_empty_keys(values='') }}"
# returns # returns
# a: foo # a: foo
# d: # d:
# - a: foo # - a: foo
# c: [] # c: []
# - a: bar # - a: bar
# b: {} # b: {}
# c: None # c: None
- debug: - debug:
msg: "{{ my_dict | remove_empty_keys(values=['', [], {}, None], recursive=False) }}" msg: "{{ my_dict | remove_empty_keys(values=['', [], {}, None], recursive=False) }}"
# return # return
# a: foo # a: foo
# d: # d:
# - a: foo # - a: foo
# - a: bar # - a: bar
- debug: - debug:
msg: "{{ my_dict | remove_empty_keys(values=['foo', 'bar']) }}" msg: "{{ my_dict | remove_empty_keys(values=['foo', 'bar']) }}"
# returns # returns
# b: '' # b: ''
# c: [] # c: []
# d: # d:
# - b: '' # - b: ''
# c: [] # c: []
# - b: {} # - b: {}
# c: None # c: None
""" """
RETURN = r""" RETURN = r"""
@ -169,7 +169,7 @@ def remove_keys_from_values(data, values=None, recursive=True, matching_paramete
Removes keys from dictionaries or lists of dictionaries Removes keys from dictionaries or lists of dictionaries
if their values match the specified values or regex patterns. if their values match the specified values or regex patterns.
""" """
if not isinstance(data, (dict, list)): if not isinstance(data, (dict, list)):
raise AnsibleFilterError("Input must be a dictionary or a list.") raise AnsibleFilterError("Input must be a dictionary or a list.")
@ -205,6 +205,7 @@ def remove_keys_from_values(data, values=None, recursive=True, matching_paramete
return clean(data) return clean(data)
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {