mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 06:10:22 -07:00
Cleaning up inventory bugs from earlier group change
This commit is contained in:
parent
8b56bc1c01
commit
11c27078c0
2 changed files with 12 additions and 37 deletions
|
@ -60,16 +60,14 @@ class Inventory(object):
|
||||||
self._vars_per_host = {}
|
self._vars_per_host = {}
|
||||||
self._vars_per_group = {}
|
self._vars_per_group = {}
|
||||||
self._hosts_cache = {}
|
self._hosts_cache = {}
|
||||||
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
|
||||||
|
|
||||||
# the inventory object holds a list of groups
|
# the inventory object holds a list of groups
|
||||||
self.groups = []
|
self.groups = {}
|
||||||
|
|
||||||
# a list of host(names) to contain current inquiries to
|
# a list of host(names) to contain current inquiries to
|
||||||
self._restriction = None
|
self._restriction = None
|
||||||
|
@ -111,9 +109,7 @@ class Inventory(object):
|
||||||
self.parser = get_file_parser(host_list, self.groups, self._loader)
|
self.parser = get_file_parser(host_list, self.groups, self._loader)
|
||||||
vars_loader.add_directory(self.basedir(), with_subdir=True)
|
vars_loader.add_directory(self.basedir(), with_subdir=True)
|
||||||
|
|
||||||
if self.parser:
|
if not self.parser:
|
||||||
self.groups = self.parser.groups.values()
|
|
||||||
else:
|
|
||||||
# should never happen, but JIC
|
# should never happen, but JIC
|
||||||
raise AnsibleError("Unable to parse %s as an inventory source" % host_list)
|
raise AnsibleError("Unable to parse %s as an inventory source" % host_list)
|
||||||
|
|
||||||
|
@ -122,7 +118,7 @@ class Inventory(object):
|
||||||
# FIXME: shouldn't be required, since the group/host vars file
|
# FIXME: shouldn't be required, since the group/host vars file
|
||||||
# management will be done in VariableManager
|
# management will be done in VariableManager
|
||||||
# get group vars from group_vars/ files and vars plugins
|
# get group vars from group_vars/ files and vars plugins
|
||||||
for group in self.groups:
|
for group in self.groups.values():
|
||||||
group.vars = combine_vars(group.vars, self.get_group_variables(group.name))
|
group.vars = combine_vars(group.vars, self.get_group_variables(group.name))
|
||||||
|
|
||||||
# get host vars from host_vars/ files and vars plugins
|
# get host vars from host_vars/ files and vars plugins
|
||||||
|
@ -372,7 +368,7 @@ class Inventory(object):
|
||||||
results.append(host)
|
results.append(host)
|
||||||
|
|
||||||
groups = self.get_groups()
|
groups = self.get_groups()
|
||||||
for group in groups:
|
for group in groups.values():
|
||||||
if pattern == 'all':
|
if pattern == 'all':
|
||||||
for host in group.get_hosts():
|
for host in group.get_hosts():
|
||||||
__append_host_to_results(host)
|
__append_host_to_results(host)
|
||||||
|
@ -408,19 +404,6 @@ class Inventory(object):
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def groups_list(self):
|
|
||||||
if not self._groups_list:
|
|
||||||
groups = {}
|
|
||||||
for g in self.groups:
|
|
||||||
groups[g.name] = [h.name for h in g.get_hosts()]
|
|
||||||
ancestors = g.get_ancestors()
|
|
||||||
for a in ancestors:
|
|
||||||
if a.name not in groups:
|
|
||||||
groups[a.name] = [h.name for h in a.get_hosts()]
|
|
||||||
self._groups_list = groups
|
|
||||||
self._groups_cache = {}
|
|
||||||
return self._groups_list
|
|
||||||
|
|
||||||
def get_groups(self):
|
def get_groups(self):
|
||||||
return self.groups
|
return self.groups
|
||||||
|
|
||||||
|
@ -439,7 +422,7 @@ class Inventory(object):
|
||||||
return host
|
return host
|
||||||
return self._create_implicit_localhost(hostname)
|
return self._create_implicit_localhost(hostname)
|
||||||
matching_host = None
|
matching_host = None
|
||||||
for group in self.groups:
|
for group in self.groups.values():
|
||||||
for host in group.get_hosts():
|
for host in group.get_hosts():
|
||||||
if hostname == host.name:
|
if hostname == host.name:
|
||||||
matching_host = host
|
matching_host = host
|
||||||
|
@ -447,11 +430,7 @@ class Inventory(object):
|
||||||
return matching_host
|
return matching_host
|
||||||
|
|
||||||
def get_group(self, groupname):
|
def get_group(self, groupname):
|
||||||
if not self._groups_cache:
|
return self.groups[groupname]
|
||||||
for group in self.groups:
|
|
||||||
self._groups_cache[group.name] = group
|
|
||||||
|
|
||||||
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:
|
||||||
|
@ -522,10 +501,8 @@ class Inventory(object):
|
||||||
return vars
|
return vars
|
||||||
|
|
||||||
def add_group(self, group):
|
def add_group(self, group):
|
||||||
if group.name not in self.groups_list():
|
if group.name not in self.groups:
|
||||||
self.groups.append(group)
|
self.groups[group.name] = group
|
||||||
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)
|
||||||
|
|
||||||
|
@ -539,7 +516,7 @@ class Inventory(object):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def list_groups(self):
|
def list_groups(self):
|
||||||
return sorted([ g.name for g in self.groups ], key=lambda x: x)
|
return sorted(self.groups.keys(), key=lambda x: x)
|
||||||
|
|
||||||
def restrict_to_hosts(self, restriction):
|
def restrict_to_hosts(self, restriction):
|
||||||
"""
|
"""
|
||||||
|
@ -703,8 +680,6 @@ class Inventory(object):
|
||||||
self._hosts_cache = {}
|
self._hosts_cache = {}
|
||||||
self._vars_per_host = {}
|
self._vars_per_host = {}
|
||||||
self._vars_per_group = {}
|
self._vars_per_group = {}
|
||||||
self._groups_list = {}
|
self.groups = {}
|
||||||
self._groups_cache = {}
|
|
||||||
self.groups = []
|
|
||||||
|
|
||||||
self.parse_inventory(self.host_list)
|
self.parse_inventory(self.host_list)
|
||||||
|
|
|
@ -261,10 +261,10 @@ class VariableManager:
|
||||||
all_vars['playbook_dir'] = loader.get_basedir()
|
all_vars['playbook_dir'] = loader.get_basedir()
|
||||||
|
|
||||||
if host:
|
if host:
|
||||||
all_vars['groups'] = [group.name for group in host.get_groups()]
|
all_vars['group_names'] = [group.name for group in host.get_groups()]
|
||||||
|
|
||||||
if self._inventory is not None:
|
if self._inventory is not None:
|
||||||
all_vars['groups'] = self._inventory.groups_list()
|
all_vars['groups'] = self._inventory.groups.keys()
|
||||||
if include_hostvars:
|
if include_hostvars:
|
||||||
hostvars = HostVars(vars_manager=self, play=play, inventory=self._inventory, loader=loader)
|
hostvars = HostVars(vars_manager=self, play=play, inventory=self._inventory, loader=loader)
|
||||||
all_vars['hostvars'] = hostvars
|
all_vars['hostvars'] = hostvars
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue