|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2024 Vladimir Botka <[email protected]> |
| 3 | +# Copyright (c) 2024 Felix Fontein <[email protected]> |
| 4 | +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + |
| 7 | +from __future__ import (absolute_import, division, print_function) |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +DOCUMENTATION = ''' |
| 11 | + name: remove_keys |
| 12 | + short_description: Remove specific keys from dictionaries in a list |
| 13 | + version_added: "9.1.0" |
| 14 | + author: |
| 15 | + - Vladimir Botka (@vbotka) |
| 16 | + - Felix Fontein (@felixfontein) |
| 17 | + description: This filter removes only specified keys from a provided list of dictionaries. |
| 18 | + options: |
| 19 | + _input: |
| 20 | + description: |
| 21 | + - A list of dictionaries. |
| 22 | + - Top level keys must be strings. |
| 23 | + type: list |
| 24 | + elements: dictionary |
| 25 | + required: true |
| 26 | + target: |
| 27 | + description: |
| 28 | + - A single key or key pattern to remove, or a list of keys or keys patterns to remove. |
| 29 | + - If O(matching_parameter=regex) there must be exactly one pattern provided. |
| 30 | + type: raw |
| 31 | + required: true |
| 32 | + matching_parameter: |
| 33 | + description: Specify the matching option of target keys. |
| 34 | + type: str |
| 35 | + default: equal |
| 36 | + choices: |
| 37 | + equal: Matches keys of exactly one of the O(target) items. |
| 38 | + starts_with: Matches keys that start with one of the O(target) items. |
| 39 | + ends_with: Matches keys that end with one of the O(target) items. |
| 40 | + regex: |
| 41 | + - Matches keys that match the regular expresion provided in O(target). |
| 42 | + - In this case, O(target) must be a regex string or a list with single regex string. |
| 43 | +''' |
| 44 | + |
| 45 | +EXAMPLES = ''' |
| 46 | + l: |
| 47 | + - {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo} |
| 48 | + - {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar} |
| 49 | +
|
| 50 | + # 1) By default match keys that equal any of the items in the target. |
| 51 | + t: [k0_x0, k1_x1] |
| 52 | + r: "{{ l | community.general.remove_keys(target=t) }}" |
| 53 | +
|
| 54 | + # 2) Match keys that start with any of the items in the target. |
| 55 | + t: [k0, k1] |
| 56 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}" |
| 57 | +
|
| 58 | + # 3) Match keys that end with any of the items in target. |
| 59 | + t: [x0, x1] |
| 60 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}" |
| 61 | +
|
| 62 | + # 4) Match keys by the regex. |
| 63 | + t: ['^.*[01]_x.*$'] |
| 64 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}" |
| 65 | +
|
| 66 | + # 5) Match keys by the regex. |
| 67 | + t: '^.*[01]_x.*$' |
| 68 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}" |
| 69 | +
|
| 70 | + # The results of above examples 1-5 are all the same. |
| 71 | + r: |
| 72 | + - {k2_x2: [C0], k3_x3: foo} |
| 73 | + - {k2_x2: [C1], k3_x3: bar} |
| 74 | +
|
| 75 | + # 6) By default match keys that equal the target. |
| 76 | + t: k0_x0 |
| 77 | + r: "{{ l | community.general.remove_keys(target=t) }}" |
| 78 | +
|
| 79 | + # 7) Match keys that start with the target. |
| 80 | + t: k0 |
| 81 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}" |
| 82 | +
|
| 83 | + # 8) Match keys that end with the target. |
| 84 | + t: x0 |
| 85 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}" |
| 86 | +
|
| 87 | + # 9) Match keys by the regex. |
| 88 | + t: '^.*0_x.*$' |
| 89 | + r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}" |
| 90 | +
|
| 91 | + # The results of above examples 6-9 are all the same. |
| 92 | + r: |
| 93 | + - {k1_x1: B0, k2_x2: [C0], k3_x3: foo} |
| 94 | + - {k1_x1: B1, k2_x2: [C1], k3_x3: bar} |
| 95 | +''' |
| 96 | + |
| 97 | +RETURN = ''' |
| 98 | + _value: |
| 99 | + description: The list of dictionaries with selected keys removed. |
| 100 | + type: list |
| 101 | + elements: dictionary |
| 102 | +''' |
| 103 | + |
| 104 | +from ansible_collections.community.general.plugins.plugin_utils.keys_filter import ( |
| 105 | + _keys_filter_params, |
| 106 | + _keys_filter_target_str) |
| 107 | + |
| 108 | + |
| 109 | +def remove_keys(data, target=None, matching_parameter='equal'): |
| 110 | + """remove specific keys from dictionaries in a list""" |
| 111 | + |
| 112 | + # test parameters |
| 113 | + _keys_filter_params(data, target, matching_parameter) |
| 114 | + # test and transform target |
| 115 | + tt = _keys_filter_target_str(target, matching_parameter) |
| 116 | + |
| 117 | + if matching_parameter == 'equal': |
| 118 | + def keep_key(key): |
| 119 | + return key not in tt |
| 120 | + elif matching_parameter == 'starts_with': |
| 121 | + def keep_key(key): |
| 122 | + return not key.startswith(tt) |
| 123 | + elif matching_parameter == 'ends_with': |
| 124 | + def keep_key(key): |
| 125 | + return not key.endswith(tt) |
| 126 | + elif matching_parameter == 'regex': |
| 127 | + def keep_key(key): |
| 128 | + return tt.match(key) is None |
| 129 | + |
| 130 | + return [dict((k, v) for k, v in d.items() if keep_key(k)) for d in data] |
| 131 | + |
| 132 | + |
| 133 | +class FilterModule(object): |
| 134 | + |
| 135 | + def filters(self): |
| 136 | + return { |
| 137 | + 'remove_keys': remove_keys, |
| 138 | + } |
0 commit comments