1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Introduce _split_pattern to parse an "x:y:z" pattern

This function takes a string like 'foo:bar[1:2]:baz[x:y]-quux' and
returns a list of patterns ['foo', 'bar[1:2]', 'baz[x:y]-quux'], i.e.
splits the string on colons that are not part of a range specification.
This commit is contained in:
Abhijit Menon-Sen 2015-08-13 16:32:11 +05:30 committed by James Cammarata
parent baf637b9ae
commit 8aaa06dd50

View file

@ -155,6 +155,23 @@ class Inventory(object):
results.append(item)
return results
def _split_pattern(self, pattern):
"""
takes e.g. "webservers[0:5]:dbservers:others"
and returns ["webservers[0:5]", "dbservers", "others"]
"""
term = re.compile(
r'''(?: # We want to match something comprising:
[^:\[\]] # (anything other than ':', '[', or ']'
| # ...or...
\[[^\]]*\] # a single complete bracketed expression)
)* # repeated as many times as possible
''', re.X
)
return [x for x in term.findall(pattern) if x]
def get_hosts(self, pattern="all"):
"""
find all host names matching a pattern string, taking into account any inventory restrictions or
@ -164,7 +181,7 @@ class Inventory(object):
# process patterns
if isinstance(pattern, list):
pattern = ';'.join(pattern)
patterns = pattern.replace(";",":").split(":")
patterns = self._split_pattern(pattern.replace(";",":"))
hosts = self._get_hosts(patterns)
# exclude hosts not in a subset, if defined
@ -504,10 +521,10 @@ class Inventory(object):
self._subset = None
else:
subset_pattern = subset_pattern.replace(',',':')
subset_pattern = subset_pattern.replace(";",":").split(":")
subset_patterns = self._split_pattern(subset_pattern.replace(";",":"))
results = []
# allow Unix style @filename data
for x in subset_pattern:
for x in subset_patterns:
if x.startswith("@"):
fd = open(x[1:])
results.extend(fd.read().split("\n"))