From 0f2b1244d26d35cd25ef43ebaa451e5a7c3313d5 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 5 Apr 2016 11:31:00 -0400 Subject: [PATCH] Make sure VariableManager has a view of HostVars Fixes #15261 --- lib/ansible/executor/process/result.py | 2 +- lib/ansible/plugins/strategy/__init__.py | 1 - lib/ansible/vars/__init__.py | 25 ++++++++++++------------ lib/ansible/vars/hostvars.py | 2 ++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/ansible/executor/process/result.py b/lib/ansible/executor/process/result.py index aef8f801a2..1fbcbeb896 100644 --- a/lib/ansible/executor/process/result.py +++ b/lib/ansible/executor/process/result.py @@ -143,7 +143,7 @@ class ResultProcess(multiprocessing.Process): if result._task.loop: # this task had a loop, and has more than one result, so # loop over all of them instead of a single result - result_items = result._result['results'] + result_items = result._result.get('results', []) else: result_items = [ result._result ] diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 2c24e802ba..7b2a40a71a 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -141,7 +141,6 @@ class StrategyBase: display.debug("entering _queue_task() for %s/%s" % (host, task)) - task_vars['hostvars'] = self._tqm.hostvars # and then queue the new task display.debug("%s - putting task (%s) in queue" % (host, task)) try: diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index edaedbbd63..b87514cdf6 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -40,7 +40,6 @@ from ansible.inventory.host import Host from ansible.plugins import lookup_loader from ansible.plugins.cache import FactCache from ansible.template import Templar -from ansible.utils.debug import debug from ansible.utils.listify import listify_lookup_plugin_terms from ansible.utils.vars import combine_vars from ansible.vars.unsafe_proxy import wrap_var @@ -98,6 +97,7 @@ class VariableManager: self._host_vars_files = defaultdict(dict) self._group_vars_files = defaultdict(dict) self._inventory = None + self._hostvars = None self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest() def __getstate__(self): @@ -172,8 +172,6 @@ class VariableManager: return data - # FIXME: include_hostvars is no longer used, and should be removed, but - # all other areas of code calling get_vars need to be fixed too def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=True, include_delegate_to=True, use_cache=True): ''' Returns the variables, with optional "context" given via the parameters @@ -194,10 +192,10 @@ class VariableManager: - extra vars ''' - debug("in VariableManager get_vars()") + display.debug("in VariableManager get_vars()") cache_entry = self._get_cache_entry(play=play, host=host, task=task) if cache_entry in VARIABLE_CACHE and use_cache: - debug("vars are cached, returning them now") + display.debug("vars are cached, returning them now") return VARIABLE_CACHE[cache_entry] all_vars = dict() @@ -346,7 +344,7 @@ class VariableManager: if task or play: all_vars['vars'] = all_vars.copy() - debug("done with get_vars()") + display.debug("done with get_vars()") return all_vars def invalidate_hostvars_cache(self, play): @@ -395,6 +393,9 @@ class VariableManager: variables['omit'] = self._omit_token variables['ansible_version'] = CLI.version_info(gitinfo=False) + if self._hostvars is not None and include_hostvars: + variables['hostvars'] = self._hostvars + return variables def _get_delegated_vars(self, loader, play, task, existing_variables): @@ -407,16 +408,14 @@ class VariableManager: items = [] if task.loop is not None: if task.loop in lookup_loader: - #TODO: remove convert_bare true and deprecate this in with_ try: + #TODO: remove convert_bare true and deprecate this in with_ loop_terms = listify_lookup_plugin_terms(terms=task.loop_args, templar=templar, loader=loader, fail_on_undefined=True, convert_bare=True) + items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy) except AnsibleUndefinedVariable as e: - if 'has no attribute' in str(e): - loop_terms = [] - display.deprecated("Skipping task due to undefined attribute, in the future this will be a fatal error.") - else: - raise - items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy) + # This task will be skipped later due to this, so we just setup + # a dummy array for the later code so it doesn't fail + items = [None] else: raise AnsibleError("Unexpected failure in finding the lookup named '%s' in the available lookup plugins" % task.loop) else: diff --git a/lib/ansible/vars/hostvars.py b/lib/ansible/vars/hostvars.py index afa00ec8a4..2370b8f8a8 100644 --- a/lib/ansible/vars/hostvars.py +++ b/lib/ansible/vars/hostvars.py @@ -51,10 +51,12 @@ class HostVars(collections.Mapping): self._inventory = inventory self._loader = loader self._variable_manager = variable_manager + variable_manager._hostvars = self self._cached_result = dict() def set_variable_manager(self, variable_manager): self._variable_manager = variable_manager + variable_manager._hostvars = self def set_inventory(self, inventory): self._inventory = inventory