From c753ae6e22ffaefb19a95af13c3064689823d11c Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Mon, 9 Nov 2015 20:55:13 -0500 Subject: [PATCH] Fixing up host pattern caching to avoid bugs * Always cache and return unique list objects, so that if the list is changed later it does not impact the cached results * Take additional parameters and the type of the pattern into account when building the hash string --- lib/ansible/inventory/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index f8357d9e85..f56f652a33 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -166,9 +166,14 @@ class Inventory(object): """ # Check if pattern already computed - pattern_hash = str(pattern) + if isinstance(pattern, list): + pattern_hash = u":".join(pattern) + else: + pattern_hash = pattern + pattern_hash += u":%s" % ignore_limits_and_restrictions + if pattern_hash in HOSTS_PATTERNS_CACHE: - return HOSTS_PATTERNS_CACHE[pattern_hash] + return HOSTS_PATTERNS_CACHE[pattern_hash][:] patterns = Inventory.split_host_pattern(pattern) hosts = self._evaluate_patterns(patterns) @@ -184,7 +189,7 @@ class Inventory(object): if self._restriction is not None: hosts = [ h for h in hosts if h in self._restriction ] - HOSTS_PATTERNS_CACHE[pattern_hash] = hosts + HOSTS_PATTERNS_CACHE[pattern_hash] = hosts[:] return hosts @classmethod