mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
namespace facts
updated action plugins to use new guranteed facts updated tests to new data clean added cases for ansible_local and some docstrings
This commit is contained in:
@@ -36,13 +36,14 @@ from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVar
|
||||
from ansible.inventory.host import Host
|
||||
from ansible.inventory.helpers import sort_groups, get_group_vars
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.six import iteritems, string_types, text_type
|
||||
from ansible.module_utils.six import iteritems, text_type
|
||||
from ansible.plugins.loader import lookup_loader, vars_loader
|
||||
from ansible.plugins.cache import FactCache
|
||||
from ansible.template import Templar
|
||||
from ansible.utils.listify import listify_lookup_plugin_terms
|
||||
from ansible.utils.vars import combine_vars
|
||||
from ansible.utils.unsafe_proxy import wrap_var
|
||||
from ansible.vars.clean import namespace_facts, clean_facts
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
@@ -72,39 +73,6 @@ def preprocess_vars(a):
|
||||
return data
|
||||
|
||||
|
||||
def strip_internal_keys(dirty, exceptions=None):
|
||||
'''
|
||||
All keys stating with _ansible_ are internal, so create a copy of the 'dirty' dict
|
||||
and remove them from the clean one before returning it
|
||||
'''
|
||||
|
||||
if exceptions is None:
|
||||
exceptions = ()
|
||||
clean = dirty.copy()
|
||||
for k in dirty.keys():
|
||||
if isinstance(k, 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
|
||||
|
||||
|
||||
def remove_internal_keys(data):
|
||||
'''
|
||||
More nuanced version of strip_internal_keys
|
||||
'''
|
||||
for key in list(data.keys()):
|
||||
if (key.startswith('_ansible_') and key != '_ansible_parsed') or key in C.INTERNAL_RESULT_KEYS:
|
||||
display.warning("Removed unexpected internal key in module return: %s = %s" % (key, data[key]))
|
||||
del data[key]
|
||||
|
||||
# remove bad/empty internal keys
|
||||
for key in ['warnings', 'deprecations']:
|
||||
if key in data and not data[key]:
|
||||
del data[key]
|
||||
|
||||
|
||||
class VariableManager:
|
||||
|
||||
_ALLOWED = frozenset(['plugins_by_group', 'groups_plugins_play', 'groups_plugins_inventory', 'groups_inventory',
|
||||
@@ -351,10 +319,15 @@ class VariableManager:
|
||||
|
||||
# finally, the facts caches for this host, if it exists
|
||||
try:
|
||||
host_facts = wrap_var(self._fact_cache.get(host.name, {}))
|
||||
facts = self._fact_cache.get(host.name, {})
|
||||
all_vars.update(namespace_facts(facts))
|
||||
|
||||
# push facts to main namespace
|
||||
all_vars = combine_vars(all_vars, host_facts)
|
||||
if C.INJECT_FACTS_AS_VARS:
|
||||
all_vars = combine_vars(all_vars, wrap_var(facts))
|
||||
else:
|
||||
# always 'promote' ansible_local
|
||||
all_vars = combine_vars(all_vars, wrap_var({'ansible_local': facts.get('ansible_local', {})}))
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
@@ -431,7 +404,9 @@ class VariableManager:
|
||||
# next, we merge in the vars cache (include vars) and nonpersistent
|
||||
# facts cache (set_fact/register), in that order
|
||||
if host:
|
||||
# include_vars non-persistent cache
|
||||
all_vars = combine_vars(all_vars, self._vars_cache.get(host.get_name(), dict()))
|
||||
# fact non-persistent cache
|
||||
all_vars = combine_vars(all_vars, self._nonpersistent_fact_cache.get(host.name, dict()))
|
||||
|
||||
# next, we merge in role params and task include params
|
||||
|
||||
Reference in New Issue
Block a user