mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-09 09:54:02 -07:00
fixes for stripping (#52930)
function changed to do in place replacement, should be less expensive even with copy as it avoids 'sub copies', can compose with module_args_copy to create replacement for old behavior attempt to fix #52910 * handle lists and subdicts correctly * added missing exception case, which was not noticed since 'cleaning' was not working * added comments to clarify exceptions
This commit is contained in:
parent
7a387e216e
commit
b793f08a92
11 changed files with 83 additions and 47 deletions
|
@ -9,11 +9,14 @@ import os
|
|||
import re
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils import six
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.common._collections_compat import MutableMapping, MutableSequence
|
||||
from ansible.plugins.loader import connection_loader
|
||||
from ansible.utils.display import Display
|
||||
|
||||
|
||||
display = Display()
|
||||
|
||||
|
||||
|
@ -65,21 +68,32 @@ def module_response_deepcopy(v):
|
|||
|
||||
|
||||
def strip_internal_keys(dirty, exceptions=None):
|
||||
'''
|
||||
All keys starting with _ansible_ are internal, so create a copy of the 'dirty' dict
|
||||
and remove them from the clean one before returning it
|
||||
'''
|
||||
# All keys starting with _ansible_ are internal, so change the 'dirty' mapping and remove them.
|
||||
|
||||
if exceptions is None:
|
||||
exceptions = ()
|
||||
clean = dirty.copy()
|
||||
for k in dirty.keys():
|
||||
if isinstance(k, six.string_types) and k.startswith('_ansible_'):
|
||||
if k not in exceptions:
|
||||
del clean[k]
|
||||
elif isinstance(dirty[k], dict):
|
||||
clean[k] = strip_internal_keys(dirty[k])
|
||||
return clean
|
||||
exceptions = tuple()
|
||||
|
||||
if isinstance(dirty, MutableSequence):
|
||||
|
||||
for element in dirty:
|
||||
if isinstance(element, (MutableMapping, MutableSequence)):
|
||||
strip_internal_keys(element, exceptions=exceptions)
|
||||
|
||||
elif isinstance(dirty, MutableMapping):
|
||||
|
||||
# listify to avoid updating dict while iterating over it
|
||||
for k in list(dirty.keys()):
|
||||
if isinstance(k, six.string_types):
|
||||
if k.startswith('_ansible_') and k not in exceptions:
|
||||
del dirty[k]
|
||||
continue
|
||||
|
||||
if isinstance(dirty[k], (MutableMapping, MutableSequence)):
|
||||
strip_internal_keys(dirty[k], exceptions=exceptions)
|
||||
else:
|
||||
raise AnsibleError("Cannot strip invalid keys from %s" % type(dirty))
|
||||
|
||||
return dirty
|
||||
|
||||
|
||||
def remove_internal_keys(data):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue