return empty host list when pattern is empty

fixes #37894
pull/4420/head
Brian Coca 2018-03-26 10:58:57 -04:00 committed by Brian Coca
parent fda9312379
commit 2e852fcd6d
1 changed files with 35 additions and 32 deletions

View File

@ -330,49 +330,52 @@ class InventoryManager(object):
or applied subsets or applied subsets
""" """
hosts = []
# Check if pattern already computed # Check if pattern already computed
if isinstance(pattern, list): if isinstance(pattern, list):
pattern_hash = u":".join(pattern) pattern_hash = u":".join(pattern)
else: else:
pattern_hash = pattern pattern_hash = pattern
if not ignore_limits and self._subset: if pattern_hash:
pattern_hash += u":%s" % to_text(self._subset, errors='surrogate_or_strict')
if not ignore_restrictions and self._restriction:
pattern_hash += u":%s" % to_text(self._restriction, errors='surrogate_or_strict')
if pattern_hash not in self._hosts_patterns_cache:
patterns = split_host_pattern(pattern)
hosts = self._evaluate_patterns(patterns)
# mainly useful for hostvars[host] access
if not ignore_limits and self._subset: if not ignore_limits and self._subset:
# exclude hosts not in a subset, if defined pattern_hash += u":%s" % to_text(self._subset, errors='surrogate_or_strict')
subset = self._evaluate_patterns(self._subset)
hosts = [h for h in hosts if h in subset]
if not ignore_restrictions and self._restriction: if not ignore_restrictions and self._restriction:
# exclude hosts mentioned in any restriction (ex: failed hosts) pattern_hash += u":%s" % to_text(self._restriction, errors='surrogate_or_strict')
hosts = [h for h in hosts if h.name in self._restriction]
seen = set() if pattern_hash not in self._hosts_patterns_cache:
self._hosts_patterns_cache[pattern_hash] = [x for x in hosts if x not in seen and not seen.add(x)]
# sort hosts list if needed (should only happen when called from strategy) patterns = split_host_pattern(pattern)
if order in ['sorted', 'reverse_sorted']: hosts = self._evaluate_patterns(patterns)
from operator import attrgetter
hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], key=attrgetter('name'), reverse=(order == 'reverse_sorted')) # mainly useful for hostvars[host] access
elif order == 'reverse_inventory': if not ignore_limits and self._subset:
hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], reverse=True) # exclude hosts not in a subset, if defined
else: subset = self._evaluate_patterns(self._subset)
hosts = self._hosts_patterns_cache[pattern_hash][:] hosts = [h for h in hosts if h in subset]
if order == 'shuffle':
from random import shuffle if not ignore_restrictions and self._restriction:
shuffle(hosts) # exclude hosts mentioned in any restriction (ex: failed hosts)
elif order not in [None, 'inventory']: hosts = [h for h in hosts if h.name in self._restriction]
AnsibleOptionsError("Invalid 'order' specified for inventory hosts: %s" % order)
seen = set()
self._hosts_patterns_cache[pattern_hash] = [x for x in hosts if x not in seen and not seen.add(x)]
# sort hosts list if needed (should only happen when called from strategy)
if order in ['sorted', 'reverse_sorted']:
from operator import attrgetter
hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], key=attrgetter('name'), reverse=(order == 'reverse_sorted'))
elif order == 'reverse_inventory':
hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], reverse=True)
else:
hosts = self._hosts_patterns_cache[pattern_hash][:]
if order == 'shuffle':
from random import shuffle
shuffle(hosts)
elif order not in [None, 'inventory']:
AnsibleOptionsError("Invalid 'order' specified for inventory hosts: %s" % order)
return hosts return hosts