From c64193b4c67053e6e197b89c7143b9770cf71f23 Mon Sep 17 00:00:00 2001 From: Gregory Duchatelet Date: Mon, 26 Nov 2012 20:43:30 +0100 Subject: [PATCH] Added possibility to filter hosts from a group, with a regex, separating groupname and regex with a ~ Usage in group pattern: group~filterpattern Samples: ansible group~server-0[1236] -m ping ansible web~proxy -m ping ansible web~(proxy|frontend) -m ping --- lib/ansible/inventory/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 12bf0ffac3b..501835eb3f3 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 @@ -164,7 +165,7 @@ class Inventory(object): a tuple of (start, stop) or None """ - if not "[" in pattern: + if not "[" in pattern or '~' in pattern: return (pattern, None) (first, rest) = pattern.split("[") rest = rest.replace("]","") @@ -201,11 +202,18 @@ class Inventory(object): hosts = {} # ignore any negative checks here, this is handled elsewhere pattern = pattern.replace("!","") + is_re = False + if '~' in pattern: + is_re = True + hostregex = pattern.split('~', 1)[1] groups = self.get_groups() for group in groups: for host in group.get_hosts(): - if pattern == 'all' or self._match(group.name, pattern) or self._match(host.name, pattern): + if pattern == 'all' \ + or self._match(group.name, pattern) \ + or self._match(host.name, pattern) \ + or (is_re and re.search(hostregex, host.name)): hosts[host.name] = host return sorted(hosts.values(), key=lambda x: x.name)