From 15400fffe643ad3e66d6b5a296fe62d36d9a617a Mon Sep 17 00:00:00 2001 From: Gregory Duchatelet Date: Tue, 27 Nov 2012 09:53:16 +0100 Subject: [PATCH] Enhance _match function in inventory with regex. --limit ~regex could be used to filter hosts or group with a regex. Tested on cli and ansible-playbook. --- lib/ansible/inventory/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 12bf0ffac3b..bde5197e661 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -19,6 +19,7 @@ import fnmatch import os +import re import subprocess import ansible.constants as C @@ -92,7 +93,10 @@ class Inventory(object): raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout") def _match(self, str, pattern_str): - return fnmatch.fnmatch(str, pattern_str) + if pattern_str.startswith('~'): + return re.search(pattern_str[1:], str) + else: + return fnmatch.fnmatch(str, pattern_str) def get_hosts(self, pattern="all"): """ @@ -164,7 +168,7 @@ class Inventory(object): a tuple of (start, stop) or None """ - if not "[" in pattern: + if not "[" in pattern or pattern.startswith('~'): return (pattern, None) (first, rest) = pattern.split("[") rest = rest.replace("]","")