From 56d7f3889d5550ab6dc105bdb48471d29f71446e Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 9 Sep 2015 12:21:07 -0400 Subject: [PATCH] Add new level of vars to deal with set_fact/register precedence rules --- lib/ansible/plugins/strategies/__init__.py | 5 +++-- lib/ansible/vars/__init__.py | 23 +++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/ansible/plugins/strategies/__init__.py b/lib/ansible/plugins/strategies/__init__.py index 8658640ab54..f45e2e7d65e 100644 --- a/lib/ansible/plugins/strategies/__init__.py +++ b/lib/ansible/plugins/strategies/__init__.py @@ -238,11 +238,12 @@ class StrategyBase: elif result[0] == 'register_host_var': # essentially the same as 'set_host_var' below, however we - # never follow the delegate_to value for registered vars + # never follow the delegate_to value for registered vars and + # the variable goes in the fact_cache host = result[1] var_name = result[2] var_value = result[3] - self._variable_manager.set_host_variable(host, var_name, var_value) + self._variable_manager.set_host_facts(host, {var_name: var_value}) elif result[0] in ('set_host_var', 'set_host_facts'): host = result[1] diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 6fa3c90ccdb..0fe4b3fdb39 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -48,14 +48,14 @@ class VariableManager: def __init__(self): - self._fact_cache = FactCache() - self._vars_cache = defaultdict(dict) - self._extra_vars = defaultdict(dict) - self._host_vars_files = defaultdict(dict) + self._fact_cache = FactCache() + self._nonpersistent_fact_cache = defaultdict(dict) + self._vars_cache = defaultdict(dict) + self._extra_vars = defaultdict(dict) + self._host_vars_files = defaultdict(dict) self._group_vars_files = defaultdict(dict) - self._inventory = None - - self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest() + self._inventory = None + self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest() def _get_cache_entry(self, play=None, host=None, task=None): play_id = "NONE" @@ -179,13 +179,14 @@ class VariableManager: for item in data: all_vars = combine_vars(all_vars, item) - # finally, the facts cache for this host, if it exists + # finally, the facts caches for this host, if it exists try: host_facts = self._fact_cache.get(host.name, dict()) for k in host_facts.keys(): if host_facts[k] is not None and not isinstance(host_facts[k], UnsafeProxy): host_facts[k] = UnsafeProxy(host_facts[k]) all_vars = combine_vars(all_vars, host_facts) + all_vars = combine_vars(all_vars, self._nonpersistent_fact_cache.get(host.name, dict())) except KeyError: pass @@ -370,12 +371,12 @@ class VariableManager: assert isinstance(facts, dict) if host.name not in self._fact_cache: - self._fact_cache[host.name] = facts + self._nonpersistent_fact_cache[host.name] = facts else: try: - self._fact_cache[host.name].update(facts) + self._nonpersistent_fact_cache[host.name].update(facts) except KeyError: - self._fact_cache[host.name] = facts + self._nonpersistent_fact_cache[host.name] = facts def set_host_variable(self, host, varname, value): '''