1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

use set to quicken group host membership

This commit is contained in:
Brian Coca 2017-09-21 17:51:23 -04:00 committed by Brian Coca
parent a819cfcad7
commit 3f9a885b83

View file

@ -31,6 +31,7 @@ class Group:
self.depth = 0 self.depth = 0
self.name = name self.name = name
self.hosts = [] self.hosts = []
self._hosts = set()
self.vars = {} self.vars = {}
self.child_groups = [] self.child_groups = []
self.parent_groups = [] self.parent_groups = []
@ -54,11 +55,14 @@ class Group:
for parent in self.parent_groups: for parent in self.parent_groups:
parent_groups.append(parent.serialize()) parent_groups.append(parent.serialize())
self._hosts = None
result = dict( result = dict(
name=self.name, name=self.name,
vars=self.vars.copy(), vars=self.vars.copy(),
parent_groups=parent_groups, parent_groups=parent_groups,
depth=self.depth, depth=self.depth,
hosts=self.hosts,
) )
return result return result
@ -68,6 +72,9 @@ class Group:
self.name = data.get('name') self.name = data.get('name')
self.vars = data.get('vars', dict()) self.vars = data.get('vars', dict())
self.depth = data.get('depth', 0) self.depth = data.get('depth', 0)
self.hosts = data.get('hosts', {})
self._hosts = set(self.hosts)
parent_groups = data.get('parent_groups', []) parent_groups = data.get('parent_groups', [])
for parent_data in parent_groups: for parent_data in parent_groups:
@ -112,17 +119,19 @@ class Group:
raise AnsibleError("The group named '%s' has a recursive dependency loop." % self.name) raise AnsibleError("The group named '%s' has a recursive dependency loop." % self.name)
def add_host(self, host): def add_host(self, host):
if host in self.hosts: if host.name not in self._hosts:
return self.hosts.append(host)
self.hosts.append(host) self._hosts.add(host.name)
host.add_group(self) host.add_group(self)
self.clear_hosts_cache() self.clear_hosts_cache()
def remove_host(self, host): def remove_host(self, host):
self.hosts.remove(host) if host.name in self._hosts:
host.remove_group(self) self.hosts.remove(host)
self.clear_hosts_cache() self._hosts.remove(host.name)
host.remove_group(self)
self.clear_hosts_cache()
def set_variable(self, key, value): def set_variable(self, key, value):