mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
Only template each hostvars var on-demand (fixes #33259)
This commit is contained in:
committed by
Brian Coca
parent
0f893027c4
commit
dae737c8b7
@@ -47,7 +47,7 @@ try:
|
||||
except ImportError:
|
||||
from sha import sha as sha1
|
||||
|
||||
__all__ = ['HostVars']
|
||||
__all__ = ['HostVars', 'HostVarsVars']
|
||||
|
||||
|
||||
# Note -- this is a Mapping, not a MutableMapping
|
||||
@@ -86,11 +86,9 @@ class HostVars(collections.Mapping):
|
||||
|
||||
def __getitem__(self, host_name):
|
||||
data = self.raw_get(host_name)
|
||||
sha1_hash = sha1(to_bytes(data)).hexdigest()
|
||||
if sha1_hash not in self._cached_result:
|
||||
templar = Templar(variables=data, loader=self._loader)
|
||||
self._cached_result[sha1_hash] = templar.template(data, fail_on_undefined=False, static_vars=STATIC_VARS)
|
||||
return self._cached_result[sha1_hash]
|
||||
if isinstance(data, Undefined):
|
||||
return data
|
||||
return HostVarsVars(data, loader=self._loader)
|
||||
|
||||
def set_host_variable(self, host, varname, value):
|
||||
self._variable_manager.set_host_variable(host, varname, value)
|
||||
@@ -117,3 +115,28 @@ class HostVars(collections.Mapping):
|
||||
for host in self._inventory.hosts:
|
||||
out[host] = self.get(host)
|
||||
return repr(out)
|
||||
|
||||
|
||||
class HostVarsVars(collections.Mapping):
|
||||
|
||||
def __init__(self, variables, loader):
|
||||
self._vars = variables
|
||||
self._loader = loader
|
||||
|
||||
def __getitem__(self, var):
|
||||
templar = Templar(variables=self._vars, loader=self._loader)
|
||||
foo = templar.template(self._vars[var], fail_on_undefined=False, static_vars=STATIC_VARS)
|
||||
return foo
|
||||
|
||||
def __contains__(self, var):
|
||||
return (var in self._vars)
|
||||
|
||||
def __iter__(self):
|
||||
for var in self._vars.keys():
|
||||
yield var
|
||||
|
||||
def __len__(self):
|
||||
return len(self._vars.keys())
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self._vars)
|
||||
|
||||
Reference in New Issue
Block a user