From 160470bc49cd47abe8ba42b04ee6bae8eb91d705 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Sat, 25 Feb 2012 01:06:18 -0500 Subject: [PATCH] - add match_hosts() method to runner.Runner() - add pattern parameter to self._matches() and match_hosts() so you can query for matches against the host list - these are useful when writing scripts and you want to tell the user what hosts you will be running on before actually executing anything. --- lib/ansible/runner.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index fddac73919..ed0d184dd0 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -73,11 +73,13 @@ class Runner(object): return host_list - def _matches(self, host_name): + def _matches(self, host_name, pattern=None): ''' returns if a hostname is matched by the pattern ''' if host_name == '': return False - if fnmatch.fnmatch(host_name, self.pattern): + if not pattern: + pattern = self.pattern + if fnmatch.fnmatch(host_name, pattern): return True return False @@ -161,11 +163,15 @@ class Runner(object): sftp.close() return out_path + def match_hosts(self, pattern=None): + ''' return all matched hosts ''' + return [ h for h in self.host_list if self._matches(h, pattern) ] + def run(self): ''' xfer & run module on all matched hosts ''' # find hosts that match the pattern - hosts = [ h for h in self.host_list if self._matches(h) ] + hosts = self.match_hosts() # attack pool of hosts in N forks pool = multiprocessing.Pool(self.forks)