mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #4936 from stoned/group-get_hosts-perf2
Improve Group.get_hosts() performance.
This commit is contained in:
commit
93223510af
1 changed files with 22 additions and 3 deletions
|
@ -18,7 +18,7 @@
|
||||||
class Group(object):
|
class Group(object):
|
||||||
''' a group of ansible hosts '''
|
''' a group of ansible hosts '''
|
||||||
|
|
||||||
__slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth' ]
|
__slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth', '_hosts_cache' ]
|
||||||
|
|
||||||
def __init__(self, name=None):
|
def __init__(self, name=None):
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ class Group(object):
|
||||||
self.vars = {}
|
self.vars = {}
|
||||||
self.child_groups = []
|
self.child_groups = []
|
||||||
self.parent_groups = []
|
self.parent_groups = []
|
||||||
|
self.clear_hosts_cache()
|
||||||
if self.name is None:
|
if self.name is None:
|
||||||
raise Exception("group name is required")
|
raise Exception("group name is required")
|
||||||
|
|
||||||
|
@ -41,26 +42,44 @@ class Group(object):
|
||||||
self.child_groups.append(group)
|
self.child_groups.append(group)
|
||||||
group.depth = max([self.depth+1, group.depth])
|
group.depth = max([self.depth+1, group.depth])
|
||||||
group.parent_groups.append(self)
|
group.parent_groups.append(self)
|
||||||
|
self.clear_hosts_cache()
|
||||||
|
|
||||||
def add_host(self, host):
|
def add_host(self, host):
|
||||||
|
|
||||||
self.hosts.append(host)
|
self.hosts.append(host)
|
||||||
host.add_group(self)
|
host.add_group(self)
|
||||||
|
self.clear_hosts_cache()
|
||||||
|
|
||||||
def set_variable(self, key, value):
|
def set_variable(self, key, value):
|
||||||
|
|
||||||
self.vars[key] = value
|
self.vars[key] = value
|
||||||
|
|
||||||
|
def clear_hosts_cache(self):
|
||||||
|
|
||||||
|
self._hosts_cache = None
|
||||||
|
for g in self.parent_groups:
|
||||||
|
g.clear_hosts_cache()
|
||||||
|
|
||||||
def get_hosts(self):
|
def get_hosts(self):
|
||||||
|
|
||||||
|
if self._hosts_cache is None:
|
||||||
|
self._hosts_cache = self._get_hosts()
|
||||||
|
|
||||||
|
return self._hosts_cache
|
||||||
|
|
||||||
|
def _get_hosts(self):
|
||||||
|
|
||||||
hosts = []
|
hosts = []
|
||||||
|
seen = {}
|
||||||
for kid in self.child_groups:
|
for kid in self.child_groups:
|
||||||
kid_hosts = kid.get_hosts()
|
kid_hosts = kid.get_hosts()
|
||||||
for kk in kid_hosts:
|
for kk in kid_hosts:
|
||||||
if kk not in hosts:
|
if kk not in seen:
|
||||||
|
seen[kk] = 1
|
||||||
hosts.append(kk)
|
hosts.append(kk)
|
||||||
for mine in self.hosts:
|
for mine in self.hosts:
|
||||||
if mine not in hosts:
|
if mine not in seen:
|
||||||
|
seen[mine] = 1
|
||||||
hosts.append(mine)
|
hosts.append(mine)
|
||||||
return hosts
|
return hosts
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue