Browse code

Make sure VariableManager has a view of HostVars

Fixes #15261

James Cammarata authored on 2016/04/06 00:31:00
Showing 4 changed files
... ...
@@ -141,7 +141,7 @@ class ResultProcess(multiprocessing.Process):
141 141
                     if result._task.loop:
142 142
                         # this task had a loop, and has more than one result, so
143 143
                         # loop over all of them instead of a single result
144
-                        result_items = result._result['results']
144
+                        result_items = result._result.get('results', [])
145 145
                     else:
146 146
                         result_items = [ result._result ]
147 147
 
... ...
@@ -141,7 +141,6 @@ class StrategyBase:
141 141
 
142 142
         display.debug("entering _queue_task() for %s/%s" % (host, task))
143 143
 
144
-        task_vars['hostvars'] = self._tqm.hostvars
145 144
         # and then queue the new task
146 145
         display.debug("%s - putting task (%s) in queue" % (host, task))
147 146
         try:
... ...
@@ -40,7 +40,6 @@ from ansible.inventory.host import Host
40 40
 from ansible.plugins import lookup_loader
41 41
 from ansible.plugins.cache import FactCache
42 42
 from ansible.template import Templar
43
-from ansible.utils.debug import debug
44 43
 from ansible.utils.listify import listify_lookup_plugin_terms
45 44
 from ansible.utils.vars import combine_vars
46 45
 from ansible.vars.unsafe_proxy import wrap_var
... ...
@@ -98,6 +97,7 @@ class VariableManager:
98 98
         self._host_vars_files = defaultdict(dict)
99 99
         self._group_vars_files = defaultdict(dict)
100 100
         self._inventory = None
101
+        self._hostvars = None
101 102
         self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest()
102 103
 
103 104
     def __getstate__(self):
... ...
@@ -172,8 +172,6 @@ class VariableManager:
172 172
 
173 173
         return data
174 174
 
175
-    # FIXME: include_hostvars is no longer used, and should be removed, but
176
-    #        all other areas of code calling get_vars need to be fixed too
177 175
     def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=True, include_delegate_to=True, use_cache=True):
178 176
         '''
179 177
         Returns the variables, with optional "context" given via the parameters
... ...
@@ -194,10 +192,10 @@ class VariableManager:
194 194
         - extra vars
195 195
         '''
196 196
 
197
-        debug("in VariableManager get_vars()")
197
+        display.debug("in VariableManager get_vars()")
198 198
         cache_entry = self._get_cache_entry(play=play, host=host, task=task)
199 199
         if cache_entry in VARIABLE_CACHE and use_cache:
200
-            debug("vars are cached, returning them now")
200
+            display.debug("vars are cached, returning them now")
201 201
             return VARIABLE_CACHE[cache_entry]
202 202
 
203 203
         all_vars = dict()
... ...
@@ -346,7 +344,7 @@ class VariableManager:
346 346
         if task or play:
347 347
             all_vars['vars'] = all_vars.copy()
348 348
 
349
-        debug("done with get_vars()")
349
+        display.debug("done with get_vars()")
350 350
         return all_vars
351 351
 
352 352
     def invalidate_hostvars_cache(self, play):
... ...
@@ -395,6 +393,9 @@ class VariableManager:
395 395
         variables['omit'] = self._omit_token
396 396
         variables['ansible_version'] = CLI.version_info(gitinfo=False)
397 397
 
398
+        if self._hostvars is not None and include_hostvars:
399
+            variables['hostvars'] = self._hostvars
400
+
398 401
         return variables
399 402
 
400 403
     def _get_delegated_vars(self, loader, play, task, existing_variables):
... ...
@@ -407,16 +408,14 @@ class VariableManager:
407 407
         items = []
408 408
         if task.loop is not None:
409 409
             if task.loop in lookup_loader:
410
-                #TODO: remove convert_bare true and deprecate this in with_
411 410
                 try:
411
+                    #TODO: remove convert_bare true and deprecate this in with_
412 412
                     loop_terms = listify_lookup_plugin_terms(terms=task.loop_args, templar=templar, loader=loader, fail_on_undefined=True, convert_bare=True)
413
+                    items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy)
413 414
                 except AnsibleUndefinedVariable as e:
414
-                    if 'has no attribute' in str(e):
415
-                        loop_terms = []
416
-                        display.deprecated("Skipping task due to undefined attribute, in the future this will be a fatal error.")
417
-                    else:
418
-                        raise
419
-                items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy)
415
+                    # This task will be skipped later due to this, so we just setup
416
+                    # a dummy array for the later code so it doesn't fail
417
+                    items = [None]
420 418
             else:
421 419
                 raise AnsibleError("Unexpected failure in finding the lookup named '%s' in the available lookup plugins" % task.loop)
422 420
         else:
... ...
@@ -51,10 +51,12 @@ class HostVars(collections.Mapping):
51 51
         self._inventory = inventory
52 52
         self._loader = loader
53 53
         self._variable_manager = variable_manager
54
+        variable_manager._hostvars = self
54 55
         self._cached_result = dict()
55 56
 
56 57
     def set_variable_manager(self, variable_manager):
57 58
         self._variable_manager = variable_manager
59
+        variable_manager._hostvars = self
58 60
 
59 61
     def set_inventory(self, inventory):
60 62
         self._inventory = inventory