mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #4429 from sergevanginderachter/cache_pattern_enumeration
Add caching to pattern enumeration
This commit is contained in:
commit
c2572b0379
1 changed files with 12 additions and 10 deletions
|
@ -38,7 +38,7 @@ class Inventory(object):
|
||||||
|
|
||||||
__slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset',
|
__slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset',
|
||||||
'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list',
|
'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list',
|
||||||
'_vars_plugins', '_playbook_basedir']
|
'_pattern_hosts', '_vars_plugins', '_playbook_basedir']
|
||||||
|
|
||||||
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
||||||
|
|
||||||
|
@ -53,6 +53,7 @@ class Inventory(object):
|
||||||
self._vars_per_group = {}
|
self._vars_per_group = {}
|
||||||
self._hosts_cache = {}
|
self._hosts_cache = {}
|
||||||
self._groups_list = {}
|
self._groups_list = {}
|
||||||
|
self._pattern_hosts = {}
|
||||||
|
|
||||||
# to be set by calling set_playbook_basedir by ansible-playbook
|
# to be set by calling set_playbook_basedir by ansible-playbook
|
||||||
self._playbook_basedir = None
|
self._playbook_basedir = None
|
||||||
|
@ -247,7 +248,6 @@ class Inventory(object):
|
||||||
else:
|
else:
|
||||||
return [ hosts[left] ]
|
return [ hosts[left] ]
|
||||||
|
|
||||||
# TODO: cache this logic so if called a second time the result is not recalculated
|
|
||||||
def _hosts_in_unenumerated_pattern(self, pattern):
|
def _hosts_in_unenumerated_pattern(self, pattern):
|
||||||
""" Get all host names matching the pattern """
|
""" Get all host names matching the pattern """
|
||||||
|
|
||||||
|
@ -255,14 +255,16 @@ class Inventory(object):
|
||||||
# ignore any negative checks here, this is handled elsewhere
|
# ignore any negative checks here, this is handled elsewhere
|
||||||
pattern = pattern.replace("!","").replace("&", "")
|
pattern = pattern.replace("!","").replace("&", "")
|
||||||
|
|
||||||
results = []
|
if pattern not in self._pattern_hosts:
|
||||||
groups = self.get_groups()
|
results = []
|
||||||
for group in groups:
|
groups = self.get_groups()
|
||||||
for host in group.get_hosts():
|
for group in groups:
|
||||||
if pattern == 'all' or self._match(group.name, pattern) or self._match(host.name, pattern):
|
for host in group.get_hosts():
|
||||||
if host not in results:
|
if pattern == 'all' or self._match(group.name, pattern) or self._match(host.name, pattern):
|
||||||
results.append(host)
|
if host not in results:
|
||||||
return results
|
results.append(host)
|
||||||
|
self._pattern_hosts[pattern] = results
|
||||||
|
return self._pattern_hosts[pattern]
|
||||||
|
|
||||||
def groups_for_host(self, host):
|
def groups_for_host(self, host):
|
||||||
results = []
|
results = []
|
||||||
|
|
Loading…
Reference in a new issue