Add inventory variables to hostvars.

Look up only when requested and cache the result.
This commit is contained in:
Jeroen Hoekx 2012-10-08 20:26:58 +02:00
parent f32778aa93
commit 46a4ae74a7

View file

@ -69,6 +69,22 @@ def _executor_hook(job_queue, result_queue):
except:
traceback.print_exc()
class HostVars(dict):
''' A special view of setup_cache that adds values from the inventory
when needed. '''
def __init__(self, setup_cache, inventory):
self.setup_cache = setup_cache
self.inventory = inventory
self.lookup = {}
self.update(setup_cache)
def __getitem__(self, host):
if not host in self.lookup:
self.lookup[host] = self.inventory.get_variables(host)
self.setup_cache[host].update(self.lookup[host])
return self.setup_cache[host]
class Runner(object):
''' core API interface to ansible '''
@ -252,7 +268,7 @@ class Runner(object):
inject.update(host_variables)
inject.update(self.module_vars)
inject.update(self.setup_cache[host])
inject['hostvars'] = self.setup_cache
inject['hostvars'] = HostVars(self.setup_cache, self.inventory)
inject['group_names'] = host_variables.get('group_names', [])
inject['groups'] = self.inventory.groups_list()