Skip to content

Commit

Permalink
Fixing up host pattern caching to avoid bugs
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jimi-c committed Nov 10, 2015
1 parent e8f83dc commit c753ae6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/ansible/inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ def get_hosts(self, pattern="all", ignore_limits_and_restrictions=False):
"""

# 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)
Expand All @@ -184,7 +189,7 @@ def get_hosts(self, pattern="all", ignore_limits_and_restrictions=False):
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
Expand Down

0 comments on commit c753ae6

Please sign in to comment.