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