Added some caching logic to improve external inventory scripts, removed some debug statements.
This commit is contained in:
parent
57f12ac9e3
commit
617f9dc942
2 changed files with 24 additions and 14 deletions
|
@ -36,7 +36,7 @@ class Inventory(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = [ 'host_list', 'groups', '_restriction', '_is_script',
|
__slots__ = [ 'host_list', 'groups', '_restriction', '_is_script',
|
||||||
'parser' ]
|
'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache' ]
|
||||||
|
|
||||||
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
||||||
|
|
||||||
|
@ -44,6 +44,13 @@ class Inventory(object):
|
||||||
# if a list, inventory data will NOT be loaded
|
# if a list, inventory data will NOT be loaded
|
||||||
self.host_list = host_list
|
self.host_list = host_list
|
||||||
|
|
||||||
|
# caching to avoid repeated calculations, particularly with
|
||||||
|
# external inventory scripts.
|
||||||
|
|
||||||
|
self._vars_per_host = {}
|
||||||
|
self._vars_per_group = {}
|
||||||
|
self._hosts_cache = {}
|
||||||
|
|
||||||
# the inventory object holds a list of groups
|
# the inventory object holds a list of groups
|
||||||
self.groups = []
|
self.groups = []
|
||||||
|
|
||||||
|
@ -111,9 +118,12 @@ class Inventory(object):
|
||||||
def get_groups(self):
|
def get_groups(self):
|
||||||
return self.groups
|
return self.groups
|
||||||
|
|
||||||
# TODO: cache this logic so if called a second time the result is not recalculated
|
|
||||||
# if using inventory scripts
|
|
||||||
def get_host(self, hostname):
|
def get_host(self, hostname):
|
||||||
|
if hostname not in self._hosts_cache:
|
||||||
|
self._hosts_cache[hostname] = self._get_host(hostname)
|
||||||
|
return self._hosts_cache[hostname]
|
||||||
|
|
||||||
|
def _get_host(self, hostname):
|
||||||
for group in self.groups:
|
for group in self.groups:
|
||||||
for host in group.get_hosts():
|
for host in group.get_hosts():
|
||||||
if hostname == host.name:
|
if hostname == host.name:
|
||||||
|
@ -125,18 +135,24 @@ class Inventory(object):
|
||||||
if group.name == groupname:
|
if group.name == groupname:
|
||||||
return group
|
return group
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# TODO: cache this logic so if called a second time the result is not recalculated
|
|
||||||
# if using inventory scripts
|
|
||||||
def get_group_variables(self, groupname):
|
def get_group_variables(self, groupname):
|
||||||
|
if groupname not in self._vars_per_group:
|
||||||
|
self._vars_per_group[groupname] = self._get_group_variables(groupname)
|
||||||
|
return self._vars_per_group[groupname]
|
||||||
|
|
||||||
|
def _get_group_variables(self, groupname):
|
||||||
group = self.get_group(groupname)
|
group = self.get_group(groupname)
|
||||||
if group is None:
|
if group is None:
|
||||||
raise Exception("group not found: %s" % groupname)
|
raise Exception("group not found: %s" % groupname)
|
||||||
return group.get_variables()
|
return group.get_variables()
|
||||||
|
|
||||||
# TODO: cache this logic so if called a second time the result is not recalculated
|
|
||||||
# if using inventory scripts
|
|
||||||
def get_variables(self, hostname):
|
def get_variables(self, hostname):
|
||||||
|
if hostname not in self._vars_per_host:
|
||||||
|
self._vars_per_host[hostname] = self._get_variables(hostname)
|
||||||
|
return self._vars_per_host[hostname]
|
||||||
|
|
||||||
|
def _get_variables(self, hostname):
|
||||||
|
|
||||||
if self._is_script:
|
if self._is_script:
|
||||||
host = self.get_host(hostname)
|
host = self.get_host(hostname)
|
||||||
|
|
|
@ -209,12 +209,10 @@ class Play(object):
|
||||||
path = os.path.join(basedir, "group_vars/%s" % x)
|
path = os.path.join(basedir, "group_vars/%s" % x)
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
data = utils.parse_yaml_from_file(path)
|
data = utils.parse_yaml_from_file(path)
|
||||||
print "GROUPFILE: %s" % data
|
|
||||||
self.playbook.SETUP_CACHE[host].update(data)
|
self.playbook.SETUP_CACHE[host].update(data)
|
||||||
path = os.path.join(basedir, "host_vars/%s" % hostrec.name)
|
path = os.path.join(basedir, "host_vars/%s" % hostrec.name)
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
data = utils.parse_yaml_from_file(path)
|
data = utils.parse_yaml_from_file(path)
|
||||||
print "HOSTFILE: %s" % data
|
|
||||||
self.playbook.SETUP_CACHE[host].update(data)
|
self.playbook.SETUP_CACHE[host].update(data)
|
||||||
|
|
||||||
for filename in self.vars_files:
|
for filename in self.vars_files:
|
||||||
|
@ -238,13 +236,11 @@ class Play(object):
|
||||||
if self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
if self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
||||||
# this filename has variables in it that were fact specific
|
# this filename has variables in it that were fact specific
|
||||||
# so it needs to be loaded into the per host SETUP_CACHE
|
# so it needs to be loaded into the per host SETUP_CACHE
|
||||||
print "VF1 UPDATE: %s" % data
|
|
||||||
self.playbook.SETUP_CACHE[host].update(data)
|
self.playbook.SETUP_CACHE[host].update(data)
|
||||||
self.playbook.callbacks.on_import_for_host(host, filename4)
|
self.playbook.callbacks.on_import_for_host(host, filename4)
|
||||||
elif not self._has_vars_in(filename4):
|
elif not self._has_vars_in(filename4):
|
||||||
# found a non-host specific variable, load into vars and NOT
|
# found a non-host specific variable, load into vars and NOT
|
||||||
# the setup cache
|
# the setup cache
|
||||||
print "VF2 UPDATE: %s" % data
|
|
||||||
self.vars.update(data)
|
self.vars.update(data)
|
||||||
elif host is not None:
|
elif host is not None:
|
||||||
self.playbook.callbacks.on_not_import_for_host(host, filename4)
|
self.playbook.callbacks.on_not_import_for_host(host, filename4)
|
||||||
|
@ -273,9 +269,7 @@ class Play(object):
|
||||||
if host is not None and self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
if host is not None and self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
||||||
# running a host specific pass and has host specific variables
|
# running a host specific pass and has host specific variables
|
||||||
# load into setup cache
|
# load into setup cache
|
||||||
print "VF3 update: %s" % new_vars
|
|
||||||
self.playbook.SETUP_CACHE[host].update(new_vars)
|
self.playbook.SETUP_CACHE[host].update(new_vars)
|
||||||
elif host is None:
|
elif host is None:
|
||||||
# running a non-host specific pass and we can update the global vars instead
|
# running a non-host specific pass and we can update the global vars instead
|
||||||
print "VF4 update: %s" % new_vars
|
|
||||||
self.vars.update(new_vars)
|
self.vars.update(new_vars)
|
||||||
|
|
Loading…
Reference in a new issue