Merge pull request #1260 from jhoekx/inventory-in-hostvars

Add inventory variables for hosts not in the play to hostvars.
This commit is contained in:
Michael DeHaan 2012-10-08 14:19:33 -07:00
commit 136a7d5604

View file

@ -69,6 +69,22 @@ def _executor_hook(job_queue, result_queue):
except: except:
traceback.print_exc() 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): class Runner(object):
''' core API interface to ansible ''' ''' core API interface to ansible '''
@ -252,7 +268,7 @@ class Runner(object):
inject.update(host_variables) inject.update(host_variables)
inject.update(self.module_vars) inject.update(self.module_vars)
inject.update(self.setup_cache[host]) 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['group_names'] = host_variables.get('group_names', [])
inject['groups'] = self.inventory.groups_list() inject['groups'] = self.inventory.groups_list()