From 9f9e9759907ab4932dc975f146c6c1fb9c16895a Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 14 Aug 2014 12:13:40 -0500 Subject: [PATCH] Fix host pattern matching and enhance error detection Fixes #8614 --- lib/ansible/inventory/__init__.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 49724c3cb7f..5e9226b425b 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -150,17 +150,24 @@ class Inventory(object): def _match(self, str, pattern_str): - if pattern_str.startswith('~'): - return re.search(pattern_str[1:], str) - else: - return fnmatch.fnmatch(str, pattern_str) + try: + if pattern_str.startswith('~'): + return re.search(pattern_str[1:], 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): results = [] - if not pattern_str.startswith('~'): - pattern = re.compile(fnmatch.translate(pattern_str)) - else: - pattern = re.compile(pattern_str[1:]) + try: + if not pattern_str.startswith('~'): + pattern = re.compile('^' + fnmatch.translate(pattern_str)) + else: + pattern = re.compile(pattern_str[1:]) + except Exception, e: + raise errors.AnsibleError('invalid host pattern: %s' % pattern_str) + for item in items: if pattern.search(getattr(item, item_attr)): results.append(item)