Fix fact cleaning (#42595) (#43956)

* Fix fact cleaning (#42595)

* fix fact cleanup

fixes #41684

(cherry picked from commit 006f08da99)

* fix misspelled key name

* update changelog text
pull/44321/head
Matt Davis 6 years ago committed by GitHub
parent 8d2c129944
commit e107da11e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- fact names that conflict with well-known connection vars are now filtered (https://github.com/ansible/ansible/issues/41684)

@ -22,7 +22,7 @@ def _warning(msg):
try: try:
from __main__ import display from __main__ import display
display.warning(msg) display.warning(msg)
except: except Exception:
import sys import sys
sys.stderr.write(' [WARNING] %s\n' % (msg)) sys.stderr.write(' [WARNING] %s\n' % (msg))
@ -32,7 +32,7 @@ def _deprecated(msg, version='2.8'):
try: try:
from __main__ import display from __main__ import display
display.deprecated(msg, version=version) display.deprecated(msg, version=version)
except: except Exception:
import sys import sys
sys.stderr.write(' [DEPRECATED] %s, to be removed in %s\n' % (msg, version)) sys.stderr.write(' [DEPRECATED] %s, to be removed in %s\n' % (msg, version))
@ -53,7 +53,7 @@ def get_config(parser, section, key, env_var, default_value, value_type=None, ex
if value is None: if value is None:
try: try:
value = get_ini_config_value(parser, {'key': key, 'section': section}) value = get_ini_config_value(parser, {'key': key, 'section': section})
except: except Exception:
pass pass
if value is None: if value is None:
value = default_value value = default_value
@ -121,6 +121,10 @@ VAULT_VERSION_MAX = 1.0
# object. The dictionary values are tuples, to account for aliases # object. The dictionary values are tuples, to account for aliases
# in variable names. # in variable names.
COMMON_CONNECTION_VARS = frozenset(set(('ansible_connection', 'ansible_host', 'ansible_user', 'ansible_shell_executable',
'ansible_port', 'ansible_pipelining', 'ansible_password', 'ansible_timeout',
'ansible_shell_type', 'ansible_module_compression', 'ansible_private_key_file')))
MAGIC_VARIABLE_MAPPING = dict( MAGIC_VARIABLE_MAPPING = dict(
# base # base
@ -191,7 +195,7 @@ for setting in config.data.get_settings():
value = literal_eval(value) value = literal_eval(value)
except ValueError: except ValueError:
pass # not a python data structure pass # not a python data structure
except: except Exception:
pass # not templatable pass # not templatable
value = ensure_type(value, setting.name) value = ensure_type(value, setting.name)

@ -63,8 +63,13 @@ def clean_facts(facts):
fact_keys = set(data.keys()) fact_keys = set(data.keys())
# first we add all of our magic variable names to the set of # first we add all of our magic variable names to the set of
# keys we want to remove from facts # keys we want to remove from facts
# NOTE: these will eventually disappear in favor of others below
for magic_var in C.MAGIC_VARIABLE_MAPPING: for magic_var in C.MAGIC_VARIABLE_MAPPING:
remove_keys.update(fact_keys.intersection(C.MAGIC_VARIABLE_MAPPING[magic_var])) remove_keys.update(fact_keys.intersection(C.MAGIC_VARIABLE_MAPPING[magic_var]))
# remove common connection vars
remove_keys.update(fact_keys.intersection(C.COMMON_CONNECTION_VARS))
# next we remove any connection plugin specific vars # next we remove any connection plugin specific vars
for conn_path in connection_loader.all(path_only=True): for conn_path in connection_loader.all(path_only=True):
try: try:
@ -72,7 +77,7 @@ def clean_facts(facts):
re_key = re.compile('^ansible_%s_' % conn_name) re_key = re.compile('^ansible_%s_' % conn_name)
for fact_key in fact_keys: for fact_key in fact_keys:
# most lightweight VM or container tech creates devices with this pattern, this avoids filtering them out # most lightweight VM or container tech creates devices with this pattern, this avoids filtering them out
if re_key.match(fact_key) and not fact_key.endswith(('_bridge', '_gwbridge')): if (re_key.match(fact_key) and not fact_key.endswith(('_bridge', '_gwbridge'))) or re_key.startswith('ansible_become_'):
remove_keys.add(fact_key) remove_keys.add(fact_key)
except AttributeError: except AttributeError:
pass pass

@ -43,7 +43,7 @@ from ansible.template import Templar
from ansible.utils.listify import listify_lookup_plugin_terms from ansible.utils.listify import listify_lookup_plugin_terms
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars
from ansible.utils.unsafe_proxy import wrap_var from ansible.utils.unsafe_proxy import wrap_var
from ansible.vars.clean import namespace_facts from ansible.vars.clean import namespace_facts, clean_facts
try: try:
from __main__ import display from __main__ import display
@ -313,7 +313,7 @@ class VariableManager:
# push facts to main namespace # push facts to main namespace
if C.INJECT_FACTS_AS_VARS: if C.INJECT_FACTS_AS_VARS:
all_vars = combine_vars(all_vars, wrap_var(facts)) all_vars = combine_vars(all_vars, wrap_var(clean_facts(facts)))
else: else:
# always 'promote' ansible_local # always 'promote' ansible_local
all_vars = combine_vars(all_vars, wrap_var({'ansible_local': facts.get('ansible_local', {})})) all_vars = combine_vars(all_vars, wrap_var({'ansible_local': facts.get('ansible_local', {})}))

Loading…
Cancel
Save