mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Merge branch 'devel_cache_for_do_template_call' of https://github.com/Yannig/ansible into Yannig-devel_cache_for_do_template_call
This commit is contained in:
commit
5040abaaf2
3 changed files with 67 additions and 16 deletions
|
@ -28,6 +28,18 @@ from ansible import constants as C
|
|||
from ansible.inventory.host import Host
|
||||
from ansible.template import Templar
|
||||
|
||||
STATIC_VARS = [
|
||||
'inventory_hostname', 'inventory_hostname_short',
|
||||
'inventory_file', 'inventory_dir', 'playbook_dir',
|
||||
'ansible_play_hosts', 'play_hosts', 'groups', 'ungrouped', 'group_names',
|
||||
'ansible_version', 'omit', 'role_names'
|
||||
]
|
||||
|
||||
try:
|
||||
from hashlib import sha1
|
||||
except ImportError:
|
||||
from sha import sha as sha1
|
||||
|
||||
__all__ = ['HostVars']
|
||||
|
||||
# Note -- this is a Mapping, not a MutableMapping
|
||||
|
@ -39,6 +51,7 @@ class HostVars(collections.Mapping):
|
|||
self._loader = loader
|
||||
self._play = play
|
||||
self._variable_manager = variable_manager
|
||||
self._cached_result = {}
|
||||
|
||||
hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)
|
||||
|
||||
|
@ -68,8 +81,16 @@ class HostVars(collections.Mapping):
|
|||
|
||||
host = self._lookup.get(host_name)
|
||||
data = self._variable_manager.get_vars(loader=self._loader, host=host, play=self._play, include_hostvars=False)
|
||||
templar = Templar(variables=data, loader=self._loader)
|
||||
return templar.template(data, fail_on_undefined=False)
|
||||
|
||||
# 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
|
||||
|
||||
def __contains__(self, host_name):
|
||||
item = self.get(host_name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue