From ff4119adc040afe1dfdd3a554acca98ac80bbb9e Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Wed, 26 Mar 2014 16:24:54 +0100 Subject: [PATCH] Performance optimization in resolving host patterns Avoid resolving a pattern that is a plain host. When matching a hostname in the hosts_cache, just use the host object from there. When running a task on say 750 hosts, this yields a huge improvement. --- lib/ansible/inventory/__init__.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index bee8988c7b..b2a3825c9c 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -213,15 +213,18 @@ class Inventory(object): hosts = [] for p in patterns: - that = self.__get_hosts(p) - if p.startswith("!"): - hosts = [ h for h in hosts if h not in that ] - elif p.startswith("&"): - hosts = [ h for h in hosts if h in that ] + # avoid resolving a pattern that is a plain host + if p in self._hosts_cache: + hosts.append(self.get_host(p)) else: - to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ] - hosts.extend(to_append) - + that = self.__get_hosts(p) + if p.startswith("!"): + hosts = [ h for h in hosts if h not in that ] + elif p.startswith("&"): + hosts = [ h for h in hosts if h in that ] + else: + to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ] + hosts.extend(to_append) return hosts def __get_hosts(self, pattern):