Merge pull request #12002 from amenonsen/9437-rebase

Make get_group run in O(n*log(n)) instead of O(n^2) by using a cache (#9437 rebase)
pull/11791/merge
Brian Coca 9 years ago
commit fd361c7f5d

@ -67,6 +67,7 @@ class Inventory(object):
self._groups_list = {} self._groups_list = {}
self._pattern_cache = {} self._pattern_cache = {}
self._vars_plugins = [] self._vars_plugins = []
self._groups_cache = {}
# to be set by calling set_playbook_basedir by playbook code # to be set by calling set_playbook_basedir by playbook code
self._playbook_basedir = None self._playbook_basedir = None
@ -394,6 +395,7 @@ class Inventory(object):
if a.name not in groups: if a.name not in groups:
groups[a.name] = [h.name for h in a.get_hosts()] groups[a.name] = [h.name for h in a.get_hosts()]
self._groups_list = groups self._groups_list = groups
self._groups_cache = {}
return self._groups_list return self._groups_list
def get_groups(self): def get_groups(self):
@ -422,10 +424,11 @@ class Inventory(object):
return matching_host return matching_host
def get_group(self, groupname): def get_group(self, groupname):
for group in self.groups: if not self._groups_cache:
if group.name == groupname: for group in self.groups:
return group self._groups_cache[group.name] = group
return None
return self._groups_cache.get(groupname)
def get_group_variables(self, groupname, update_cached=False, vault_password=None): def get_group_variables(self, groupname, update_cached=False, vault_password=None):
if groupname not in self._vars_per_group or update_cached: if groupname not in self._vars_per_group or update_cached:
@ -499,6 +502,7 @@ class Inventory(object):
if group.name not in self.groups_list(): if group.name not in self.groups_list():
self.groups.append(group) self.groups.append(group)
self._groups_list = None # invalidate internal cache self._groups_list = None # invalidate internal cache
self._groups_cache = {}
else: else:
raise AnsibleError("group already in inventory: %s" % group.name) raise AnsibleError("group already in inventory: %s" % group.name)
@ -668,6 +672,7 @@ class Inventory(object):
self._vars_per_host = {} self._vars_per_host = {}
self._vars_per_group = {} self._vars_per_group = {}
self._groups_list = {} self._groups_list = {}
self._groups_cache = {}
self.groups = [] self.groups = []
self.parse_inventory(self.host_list) self.parse_inventory(self.host_list)

Loading…
Cancel
Save