Feature/add ansible play hosts all (#17498)

* refactor ignore_limits_and_restrictions

into ignore_limits and ignore_limitations

* add ansible_play_hosts_all

* update docs re ansible_play_hosts_all

* only use play.hosts when is has a value

* replace ansible_play_hosts with ansible_play_hosts_all

* remove unnecessary var
This commit is contained in:
j0hnsmith 2016-09-23 16:17:46 +01:00 committed by Brian Coca
commit 4650d8910e
5 changed files with 24 additions and 27 deletions

View file

@ -165,7 +165,7 @@ class Inventory(object):
self.get_group_vars(group)
# get host vars from host_vars/ files and vars plugins
for host in self.get_hosts(ignore_limits_and_restrictions=True):
for host in self.get_hosts(ignore_limits=True, ignore_restrictions=True):
host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
self.get_host_vars(host)
@ -193,7 +193,7 @@ class Inventory(object):
results.append(item)
return results
def get_hosts(self, pattern="all", ignore_limits_and_restrictions=False):
def get_hosts(self, pattern="all", ignore_limits=False, ignore_restrictions=False):
"""
Takes a pattern or list of patterns and returns a list of matching
inventory host names, taking into account any active restrictions
@ -206,11 +206,11 @@ class Inventory(object):
else:
pattern_hash = pattern
if not ignore_limits_and_restrictions:
if self._subset:
pattern_hash += u":%s" % to_text(self._subset)
if self._restriction:
pattern_hash += u":%s" % to_text(self._restriction)
if not ignore_limits and self._subset:
pattern_hash += u":%s" % to_text(self._subset)
if not ignore_restrictions and self._restriction:
pattern_hash += u":%s" % to_text(self._restriction)
if pattern_hash not in HOSTS_PATTERNS_CACHE:
@ -218,15 +218,14 @@ class Inventory(object):
hosts = self._evaluate_patterns(patterns)
# mainly useful for hostvars[host] access
if not ignore_limits_and_restrictions:
if not ignore_limits and self._subset:
# exclude hosts not in a subset, if defined
if self._subset:
subset = self._evaluate_patterns(self._subset)
hosts = [ h for h in hosts if h in subset ]
subset = self._evaluate_patterns(self._subset)
hosts = [ h for h in hosts if h in subset ]
if not ignore_restrictions and self._restriction:
# exclude hosts mentioned in any restriction (ex: failed hosts)
if self._restriction:
hosts = [ h for h in hosts if h.name in self._restriction ]
hosts = [ h for h in hosts if h.name in self._restriction ]
seen = set()
HOSTS_PATTERNS_CACHE[pattern_hash] = [x for x in hosts if x not in seen and not seen.add(x)]

View file

@ -413,9 +413,8 @@ class VariableManager:
# DEPRECATED: play_hosts should be deprecated in favor of ansible_play_hosts,
# however this would take work in the templating engine, so for now
# we'll add both so we can give users something transitional to use
host_list = [x.name for x in self._inventory.get_hosts()]
variables['play_hosts'] = host_list
variables['ansible_play_hosts'] = host_list
variables['play_hosts'] = [x.name for x in self._inventory.get_hosts()]
variables['ansible_play_hosts'] = [x.name for x in self._inventory.get_hosts(pattern=play.hosts or 'all', ignore_restrictions=True)]
# the 'omit' value alows params to be left out if the variable they are based on is undefined
variables['omit'] = self._omit_token
@ -490,7 +489,7 @@ class VariableManager:
if delegated_host_name in C.LOCALHOST:
delegated_host = self._inventory.localhost
else:
for h in self._inventory.get_hosts(ignore_limits_and_restrictions=True):
for h in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
# check if the address matches, or if both the delegated_to host
# and the current host are in the list of localhost aliases
if h.address == delegated_host_name:

View file

@ -20,19 +20,17 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import collections
import sys
from jinja2 import Undefined as j2undefined
from ansible import constants as C
from ansible.inventory.host import Host
from ansible.template import Templar
STATIC_VARS = [
'inventory_hostname', 'inventory_hostname_short',
'inventory_file', 'inventory_dir', 'playbook_dir',
'ansible_play_hosts', 'play_hosts', 'groups', 'ungrouped', 'group_names',
'ansible_version', 'omit', 'role_names'
'inventory_hostname', 'inventory_hostname_short',
'inventory_file', 'inventory_dir', 'playbook_dir',
'ansible_play_hosts', 'play_hosts', 'groups',
'ungrouped', 'group_names', 'ansible_version', 'omit', 'role_names'
]
try:
@ -103,15 +101,15 @@ class HostVars(collections.Mapping):
return self._find_host(host_name) is not None
def __iter__(self):
for host in self._inventory.get_hosts(ignore_limits_and_restrictions=True):
for host in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
yield host
def __len__(self):
return len(self._inventory.get_hosts(ignore_limits_and_restrictions=True))
return len(self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True))
def __repr__(self):
out = {}
for host in self._inventory.get_hosts(ignore_limits_and_restrictions=True):
for host in self._inventory.get_hosts(ignore_limits=True, ignore_restrictions=True):
name = host.name
out[name] = self.get(name)
return repr(out)