Fix host pattern matching and enhance error detection

Fixes #8614
This commit is contained in:
James Cammarata 2014-08-14 12:13:40 -05:00
parent a01d97f12b
commit f48fa3759a

View file

@ -150,17 +150,24 @@ class Inventory(object):
def _match(self, str, pattern_str): def _match(self, str, pattern_str):
if pattern_str.startswith('~'): try:
return re.search(pattern_str[1:], str) if pattern_str.startswith('~'):
else: return re.search(pattern_str[1:], str)
return fnmatch.fnmatch(str, pattern_str) else:
return fnmatch.fnmatch(str, pattern_str)
except Exception, e:
raise errors.AnsibleError('invalid host pattern: %s' % pattern_str)
def _match_list(self, items, item_attr, pattern_str): def _match_list(self, items, item_attr, pattern_str):
results = [] results = []
if not pattern_str.startswith('~'): try:
pattern = re.compile(fnmatch.translate(pattern_str)) if not pattern_str.startswith('~'):
else: pattern = re.compile('^' + fnmatch.translate(pattern_str))
pattern = re.compile(pattern_str[1:]) else:
pattern = re.compile(pattern_str[1:])
except Exception, e:
raise errors.AnsibleError('invalid host pattern: %s' % pattern_str)
for item in items: for item in items:
if pattern.search(getattr(item, item_attr)): if pattern.search(getattr(item, item_attr)):
results.append(item) results.append(item)