From f48fa3759adfa92c9ed618e771c4cf8b136b0470 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 2ca8b54affa..1ff15c0581e 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)