|
|
@ -146,6 +146,17 @@ class Inventory(object):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return fnmatch.fnmatch(str, pattern_str)
|
|
|
|
return fnmatch.fnmatch(str, pattern_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _match_list(self, items, item_attr, pattern_str):
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
if not pattern_str.startswith('~'):
|
|
|
|
|
|
|
|
pattern = re.compile(fnmatch.translate(pattern_str))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
pattern = re.compile(pattern_str[1:])
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
|
|
|
|
if pattern.search(getattr(item, item_attr)):
|
|
|
|
|
|
|
|
results.append(item)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def get_hosts(self, pattern="all"):
|
|
|
|
def get_hosts(self, pattern="all"):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
find all host names matching a pattern string, taking into account any inventory restrictions or
|
|
|
|
find all host names matching a pattern string, taking into account any inventory restrictions or
|
|
|
@ -201,15 +212,18 @@ class Inventory(object):
|
|
|
|
hosts = []
|
|
|
|
hosts = []
|
|
|
|
|
|
|
|
|
|
|
|
for p in patterns:
|
|
|
|
for p in patterns:
|
|
|
|
that = self.__get_hosts(p)
|
|
|
|
# avoid resolving a pattern that is a plain host
|
|
|
|
if p.startswith("!"):
|
|
|
|
if p in self._hosts_cache:
|
|
|
|
hosts = [ h for h in hosts if h not in that ]
|
|
|
|
hosts.append(self.get_host(p))
|
|
|
|
elif p.startswith("&"):
|
|
|
|
|
|
|
|
hosts = [ h for h in hosts if h in that ]
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ]
|
|
|
|
that = self.__get_hosts(p)
|
|
|
|
hosts.extend(to_append)
|
|
|
|
if p.startswith("!"):
|
|
|
|
|
|
|
|
hosts = [ h for h in hosts if h not in that ]
|
|
|
|
|
|
|
|
elif p.startswith("&"):
|
|
|
|
|
|
|
|
hosts = [ h for h in hosts if h in that ]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ]
|
|
|
|
|
|
|
|
hosts.extend(to_append)
|
|
|
|
return hosts
|
|
|
|
return hosts
|
|
|
|
|
|
|
|
|
|
|
|
def __get_hosts(self, pattern):
|
|
|
|
def __get_hosts(self, pattern):
|
|
|
@ -296,20 +310,31 @@ class Inventory(object):
|
|
|
|
def _hosts_in_unenumerated_pattern(self, pattern):
|
|
|
|
def _hosts_in_unenumerated_pattern(self, pattern):
|
|
|
|
""" Get all host names matching the pattern """
|
|
|
|
""" Get all host names matching the pattern """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
hosts = []
|
|
|
|
hosts = []
|
|
|
|
hostnames = set()
|
|
|
|
hostnames = set()
|
|
|
|
|
|
|
|
|
|
|
|
# ignore any negative checks here, this is handled elsewhere
|
|
|
|
# ignore any negative checks here, this is handled elsewhere
|
|
|
|
pattern = pattern.replace("!","").replace("&", "")
|
|
|
|
pattern = pattern.replace("!","").replace("&", "")
|
|
|
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
def __append_host_to_results(host):
|
|
|
|
|
|
|
|
if host not in results and host.name not in hostnames:
|
|
|
|
|
|
|
|
hostnames.add(host.name)
|
|
|
|
|
|
|
|
results.append(host)
|
|
|
|
|
|
|
|
|
|
|
|
groups = self.get_groups()
|
|
|
|
groups = self.get_groups()
|
|
|
|
for group in groups:
|
|
|
|
for group in groups:
|
|
|
|
for host in group.get_hosts():
|
|
|
|
if pattern == 'all':
|
|
|
|
if pattern == 'all' or self._match(group.name, pattern) or self._match(host.name, pattern):
|
|
|
|
for host in group.get_hosts():
|
|
|
|
if host not in results and host.name not in hostnames:
|
|
|
|
__append_host_to_results(host)
|
|
|
|
results.append(host)
|
|
|
|
else:
|
|
|
|
hostnames.add(host.name)
|
|
|
|
if self._match(group.name, pattern):
|
|
|
|
|
|
|
|
for host in group.get_hosts():
|
|
|
|
|
|
|
|
__append_host_to_results(host)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
matching_hosts = self._match_list(group.get_hosts(), 'name', pattern)
|
|
|
|
|
|
|
|
for host in matching_hosts:
|
|
|
|
|
|
|
|
__append_host_to_results(host)
|
|
|
|
|
|
|
|
|
|
|
|
if pattern in ["localhost", "127.0.0.1"] and len(results) == 0:
|
|
|
|
if pattern in ["localhost", "127.0.0.1"] and len(results) == 0:
|
|
|
|
new_host = self._create_implicit_localhost(pattern)
|
|
|
|
new_host = self._create_implicit_localhost(pattern)
|
|
|
@ -324,10 +349,8 @@ class Inventory(object):
|
|
|
|
results = []
|
|
|
|
results = []
|
|
|
|
groups = self.get_groups()
|
|
|
|
groups = self.get_groups()
|
|
|
|
for group in groups:
|
|
|
|
for group in groups:
|
|
|
|
for hostn in group.get_hosts():
|
|
|
|
if host in group.get_hosts():
|
|
|
|
if host == hostn.name:
|
|
|
|
results.append(group)
|
|
|
|
results.append(group)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
return results
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def groups_list(self):
|
|
|
|
def groups_list(self):
|
|
|
|