New patch against hostvars.py. With this patch, Ansible run lose 50% of time.

Little rewrite of previous patch to use sha1 signature.
Use fail_on_undefined to compute sha1 signature.
This commit is contained in:
Yannig Perré 2015-11-02 21:04:20 +01:00
commit 4a8d1703d4
2 changed files with 29 additions and 7 deletions

View file

@ -28,6 +28,11 @@ from ansible import constants as C
from ansible.inventory.host import Host
from ansible.template import Templar
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 +44,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 +74,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)).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)
self._cached_result[sha1_hash] = result
return result
def __contains__(self, host_name):
item = self.get(host_name)