diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index e0bd1674531..05eb1f6cdb1 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -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()