Cleaning up default group creation in inventory parsers

This commit is contained in:
James Cammarata 2015-09-16 11:27:03 -04:00
parent e899b8e70d
commit c8f2483d6d
4 changed files with 42 additions and 53 deletions

View file

@ -86,12 +86,14 @@ class Inventory(object):
self.parser = None
# Always create the 'all' group, even if host_list is
# empty: in this case we will subsequently an the implicit
# 'localhost' to it.
# Always create the 'all' and 'ungrouped' groups, even if host_list is
# empty: in this case we will subsequently an the implicit 'localhost' to it.
ungrouped = Group(name='ungrouped')
all = Group('all')
self.groups = [ all ]
all.add_child_group(ungrouped)
self.groups = dict(all=all, ungrouped=ungrouped)
if host_list is None:
pass
@ -104,9 +106,9 @@ class Inventory(object):
if self._loader.is_directory(host_list):
# Ensure basedir is inside the directory
host_list = os.path.join(self.host_list, "")
self.parser = InventoryDirectory(loader=self._loader, filename=host_list)
self.parser = InventoryDirectory(loader=self._loader, groups=self.groups, filename=host_list)
else:
self.parser = get_file_parser(host_list, self._loader)
self.parser = get_file_parser(host_list, self.groups, self._loader)
vars_loader.add_directory(self.basedir(), with_subdir=True)
if self.parser:
@ -393,13 +395,7 @@ class Inventory(object):
new_host.set_variable("ansible_python_interpreter", sys.executable)
new_host.set_variable("ansible_connection", "local")
new_host.ipv4_address = '127.0.0.1'
ungrouped = self.get_group("ungrouped")
if ungrouped is None:
self.add_group(Group('ungrouped'))
ungrouped = self.get_group('ungrouped')
self.get_group('all').add_child_group(ungrouped)
ungrouped.add_host(new_host)
self.get_group("ungrouped").add_host(new_host)
return new_host
def clear_pattern_cache(self):