Since host variables are becoming important, it did not make sense to sustain --override-hosts, with the ability

to create hosts that didn't have inventory information, but also existed, in various groups.
This commit is contained in:
Michael DeHaan 2012-05-07 23:16:20 -04:00
parent 036b779188
commit 07508ad535
4 changed files with 37 additions and 49 deletions

View file

@ -37,34 +37,33 @@ class Inventory(object):
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
# the host file file or script path
if type(host_list) not in [ str, unicode ]:
raise Exception("host list must be a path")
self.host_list = host_list
# the inventory object holds a list of groups
self.groups = []
# a list of host(names) to contain current inquiries to
self._restriction = None
# whether the inventory file is a script
self._is_script = False
if host_list:
if type(host_list) == list:
self.groups = self._groups_from_override_hosts(host_list)
elif os.access(host_list, os.X_OK):
self._is_script = True
self.parser = InventoryScript(filename=host_list)
if os.access(host_list, os.X_OK):
self._is_script = True
self.parser = InventoryScript(filename=host_list)
self.groups = self.parser.groups.values()
else:
data = file(host_list).read()
if not data.startswith("---"):
self.parser = InventoryParser(filename=host_list)
self.groups = self.parser.groups.values()
else:
data = file(host_list).read()
if not data.startswith("---"):
self.parser = InventoryParser(filename=host_list)
self.groups = self.parser.groups.values()
else:
self.parser = InventoryParserYaml(filename=host_list)
self.groups = self.parser.groups.values()
def _groups_from_override_hosts(self, list):
# support for playbook's --override-hosts only
all = Group(name='all')
for h in list:
all.add_host(Host(name=h))
return dict(all=all)
else:
self.parser = InventoryParserYaml(filename=host_list)
self.groups = self.parser.groups.values()
def _match(self, str, pattern_str):
return fnmatch.fnmatch(str, pattern_str)
@ -73,7 +72,8 @@ class Inventory(object):
hosts = {}
patterns = pattern.replace(";",":").split(":")
for group in self.get_groups():
groups = self.get_groups()
for group in groups:
for host in group.get_hosts():
for pat in patterns:
if group.name == pat or pat == 'all' or self._match(host.name, pat):
@ -131,19 +131,22 @@ class Inventory(object):
self.groups.append(group)
def list_hosts(self, pattern="all"):
""" DEPRECATED: Get all host names matching the pattern """
return [ h.name for h in self.get_hosts(pattern) ]
def list_groups(self):
return [ g.name for g in self.groups ]
def restrict_to(self, restriction):
def get_restriction(self):
return self._restriction
def restrict_to(self, restriction, append_missing=False):
""" Restrict list operations to the hosts given in restriction """
if type(restriction) != list:
restriction = [ restriction ]
self._restriction = restriction
if type(restriction) != list:
restriction = [ restriction ]
self._restriction = restriction
def lift_restriction(self):
""" Do not restrict list operations """
self._restriction = None
self._restriction = None