mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add inventory variables to hostvars.
Look up only when requested and cache the result.
This commit is contained in:
parent
f32778aa93
commit
46a4ae74a7
1 changed files with 17 additions and 1 deletions
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue