mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Performance improvements for HostVars and some bugfixes
This commit is contained in:
parent
9d23acf302
commit
5cbeab5a3c
6 changed files with 85 additions and 84 deletions
|
@ -359,15 +359,15 @@ class VariableManager:
|
|||
for (group_name, group) in iteritems(self._inventory.groups):
|
||||
variables['groups'][group_name] = [h.name for h in group.get_hosts()]
|
||||
|
||||
if include_hostvars:
|
||||
hostvars_cache_entry = self._get_cache_entry(play=play)
|
||||
if hostvars_cache_entry in HOSTVARS_CACHE:
|
||||
hostvars = HOSTVARS_CACHE[hostvars_cache_entry]
|
||||
else:
|
||||
hostvars = HostVars(play=play, inventory=self._inventory, loader=loader, variable_manager=self)
|
||||
HOSTVARS_CACHE[hostvars_cache_entry] = hostvars
|
||||
variables['hostvars'] = hostvars
|
||||
variables['vars'] = hostvars[host.get_name()]
|
||||
#if include_hostvars:
|
||||
# hostvars_cache_entry = self._get_cache_entry(play=play)
|
||||
# if hostvars_cache_entry in HOSTVARS_CACHE:
|
||||
# hostvars = HOSTVARS_CACHE[hostvars_cache_entry]
|
||||
# else:
|
||||
# hostvars = HostVars(play=play, inventory=self._inventory, loader=loader, variable_manager=self)
|
||||
# HOSTVARS_CACHE[hostvars_cache_entry] = hostvars
|
||||
# variables['hostvars'] = hostvars
|
||||
# variables['vars'] = hostvars[host.get_name()]
|
||||
|
||||
if play:
|
||||
variables['role_names'] = [r._role_name for r in play.roles]
|
||||
|
|
|
@ -48,74 +48,50 @@ class HostVars(collections.Mapping):
|
|||
|
||||
def __init__(self, play, inventory, variable_manager, loader):
|
||||
self._lookup = dict()
|
||||
self._inventory = inventory
|
||||
self._loader = loader
|
||||
self._play = play
|
||||
self._variable_manager = variable_manager
|
||||
self._cached_result = dict()
|
||||
|
||||
hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)
|
||||
def set_variable_manager(self, variable_manager):
|
||||
self._variable_manager = variable_manager
|
||||
|
||||
# check to see if localhost is in the hosts list, as we
|
||||
# may have it referenced via hostvars but if created implicitly
|
||||
# it doesn't sow up in the hosts list
|
||||
has_localhost = False
|
||||
for host in hosts:
|
||||
if host.name in C.LOCALHOST:
|
||||
has_localhost = True
|
||||
break
|
||||
|
||||
if not has_localhost:
|
||||
new_host = Host(name='localhost')
|
||||
new_host.set_variable("ansible_python_interpreter", sys.executable)
|
||||
new_host.set_variable("ansible_connection", "local")
|
||||
new_host.address = '127.0.0.1'
|
||||
hosts.append(new_host)
|
||||
|
||||
for host in hosts:
|
||||
self._lookup[host.name] = host
|
||||
def _find_host(self, host_name):
|
||||
return self._inventory.get_host(host_name)
|
||||
|
||||
def __getitem__(self, host_name):
|
||||
host = self._find_host(host_name)
|
||||
if host is None:
|
||||
return j2undefined
|
||||
|
||||
if host_name not in self._lookup:
|
||||
return j2undefined()
|
||||
|
||||
host = self._lookup.get(host_name)
|
||||
data = self._variable_manager.get_vars(loader=self._loader, host=host, play=self._play, include_hostvars=False)
|
||||
|
||||
#****************************************************
|
||||
# TESTING REMOVAL OF THIS
|
||||
#****************************************************
|
||||
# Since we template much later now in 2.0, it may be completely unrequired to do
|
||||
# a full template of the vars returned above, which is quite costly in time when
|
||||
# the result is large.
|
||||
# Using cache in order to avoid template call
|
||||
sha1_hash = sha1(str(data).encode('utf-8')).hexdigest()
|
||||
if sha1_hash in self._cached_result:
|
||||
result = self._cached_result[sha1_hash]
|
||||
else:
|
||||
templar = Templar(variables=data, loader=self._loader)
|
||||
result = templar.template(data, fail_on_undefined=False, static_vars=STATIC_VARS)
|
||||
self._cached_result[sha1_hash] = result
|
||||
return result
|
||||
#sha1_hash = sha1(str(data).encode('utf-8')).hexdigest()
|
||||
#if sha1_hash in self._cached_result:
|
||||
# result = self._cached_result[sha1_hash]
|
||||
#else:
|
||||
# templar = Templar(variables=data, loader=self._loader)
|
||||
# result = templar.template(data, fail_on_undefined=False, static_vars=STATIC_VARS)
|
||||
# self._cached_result[sha1_hash] = result
|
||||
#return result
|
||||
#****************************************************
|
||||
return data
|
||||
|
||||
def __contains__(self, host_name):
|
||||
item = self.get(host_name)
|
||||
if item and item is not j2undefined:
|
||||
return True
|
||||
return False
|
||||
return self._find_host(host_name) is not None
|
||||
|
||||
def __iter__(self):
|
||||
for host in self._lookup:
|
||||
for host in self._inventory.get_hosts(ignore_limits_and_restrictions=True):
|
||||
yield host
|
||||
|
||||
def __len__(self):
|
||||
return len(self._lookup)
|
||||
return len(self._inventory.get_hosts(ignore_limits_and_restrictions=True))
|
||||
|
||||
def __getstate__(self):
|
||||
return dict(
|
||||
loader=self._loader,
|
||||
lookup=self._lookup,
|
||||
play=self._play,
|
||||
var_manager=self._variable_manager,
|
||||
)
|
||||
|
||||
def __setstate__(self, data):
|
||||
self._play = data.get('play')
|
||||
self._loader = data.get('loader')
|
||||
self._lookup = data.get('lookup')
|
||||
self._variable_manager = data.get('var_manager')
|
||||
self._cached_result = dict()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue