Skip to content

Commit

Permalink
set _hosts on access if None (ansible#31111)
Browse files Browse the repository at this point in the history
set _hosts on access if None to bpyass srlz10n issues to fix ansible#30903
  • Loading branch information
bcoca authored and BondAnthony committed Oct 4, 2017
1 parent 984d34b commit 3588c45
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/ansible/inventory/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
__metaclass__ = type

from ansible.errors import AnsibleError
from ansible.utils.vars import combine_vars


class Group:
Expand All @@ -31,7 +30,7 @@ def __init__(self, name=None):
self.depth = 0
self.name = name
self.hosts = []
self._hosts = set()
self._hosts = None
self.vars = {}
self.child_groups = []
self.parent_groups = []
Expand Down Expand Up @@ -72,16 +71,21 @@ def deserialize(self, data):
self.name = data.get('name')
self.vars = data.get('vars', dict())
self.depth = data.get('depth', 0)
self.hosts = data.get('hosts', {})

self._hosts = set(self.hosts)
self.hosts = data.get('hosts', [])
self._hosts = None

parent_groups = data.get('parent_groups', [])
for parent_data in parent_groups:
g = Group()
g.deserialize(parent_data)
self.parent_groups.append(g)

@property
def host_names(self):
if self._hosts is None:
self._hosts = set(self.hosts)
return self._hosts

def get_name(self):
return self.name

Expand Down Expand Up @@ -119,15 +123,15 @@ def _check_children_depth(self):
raise AnsibleError("The group named '%s' has a recursive dependency loop." % self.name)

def add_host(self, host):
if host.name not in self._hosts:
if host.name not in self.host_names:
self.hosts.append(host)
self._hosts.add(host.name)
host.add_group(self)
self.clear_hosts_cache()

def remove_host(self, host):

if host.name in self._hosts:
if host.name in self.host_names:
self.hosts.remove(host)
self._hosts.remove(host.name)
host.remove_group(self)
Expand Down

0 comments on commit 3588c45

Please sign in to comment.