mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-03 23:20:19 -07:00
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:
parent
52bf021904
commit
4650d8910e
5 changed files with 24 additions and 27 deletions
|
@ -224,7 +224,7 @@ This approach is similar to applying a conditional to a task such as::
|
||||||
.. note::
|
.. note::
|
||||||
When used together with "serial", tasks marked as "run_once" will be run on one host in *each* serial batch.
|
When used together with "serial", tasks marked as "run_once" will be run on one host in *each* serial batch.
|
||||||
If it's crucial that the task is run only once regardless of "serial" mode, use
|
If it's crucial that the task is run only once regardless of "serial" mode, use
|
||||||
:code:`inventory_hostname == my_group_name[0]` construct.
|
:code:`when: inventory_hostname == ansible_play_hosts[0]` construct.
|
||||||
|
|
||||||
.. _local_playbooks:
|
.. _local_playbooks:
|
||||||
|
|
||||||
|
|
|
@ -674,6 +674,7 @@ reasons. If you have a long FQDN, ``inventory_hostname_short`` also contains th
|
||||||
period, without the rest of the domain.
|
period, without the rest of the domain.
|
||||||
|
|
||||||
``play_hosts`` is available as a list of hostnames that are in scope for the current play. This may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer.
|
``play_hosts`` is available as a list of hostnames that are in scope for the current play. This may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer.
|
||||||
|
``ansible_play_hosts`` is the same as ``play_hosts`` but includes all hosts in all ``serial`` batches, if you're not using ``serial`` it's the same as ``play_hosts``.
|
||||||
|
|
||||||
Don't worry about any of this unless you think you need it. You'll know when you do.
|
Don't worry about any of this unless you think you need it. You'll know when you do.
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,7 @@ class Inventory(object):
|
||||||
self.get_group_vars(group)
|
self.get_group_vars(group)
|
||||||
|
|
||||||
# get host vars from host_vars/ files and vars plugins
|
# 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))
|
host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
|
||||||
self.get_host_vars(host)
|
self.get_host_vars(host)
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ class Inventory(object):
|
||||||
results.append(item)
|
results.append(item)
|
||||||
return results
|
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
|
Takes a pattern or list of patterns and returns a list of matching
|
||||||
inventory host names, taking into account any active restrictions
|
inventory host names, taking into account any active restrictions
|
||||||
|
@ -206,10 +206,10 @@ class Inventory(object):
|
||||||
else:
|
else:
|
||||||
pattern_hash = pattern
|
pattern_hash = pattern
|
||||||
|
|
||||||
if not ignore_limits_and_restrictions:
|
if not ignore_limits and self._subset:
|
||||||
if self._subset:
|
|
||||||
pattern_hash += u":%s" % to_text(self._subset)
|
pattern_hash += u":%s" % to_text(self._subset)
|
||||||
if self._restriction:
|
|
||||||
|
if not ignore_restrictions and self._restriction:
|
||||||
pattern_hash += u":%s" % to_text(self._restriction)
|
pattern_hash += u":%s" % to_text(self._restriction)
|
||||||
|
|
||||||
if pattern_hash not in HOSTS_PATTERNS_CACHE:
|
if pattern_hash not in HOSTS_PATTERNS_CACHE:
|
||||||
|
@ -218,14 +218,13 @@ class Inventory(object):
|
||||||
hosts = self._evaluate_patterns(patterns)
|
hosts = self._evaluate_patterns(patterns)
|
||||||
|
|
||||||
# mainly useful for hostvars[host] access
|
# 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
|
# exclude hosts not in a subset, if defined
|
||||||
if self._subset:
|
|
||||||
subset = self._evaluate_patterns(self._subset)
|
subset = self._evaluate_patterns(self._subset)
|
||||||
hosts = [ h for h in hosts if h in 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)
|
# 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()
|
seen = set()
|
||||||
|
|
|
@ -413,9 +413,8 @@ class VariableManager:
|
||||||
# DEPRECATED: play_hosts should be deprecated in favor of ansible_play_hosts,
|
# DEPRECATED: play_hosts should be deprecated in favor of ansible_play_hosts,
|
||||||
# however this would take work in the templating engine, so for now
|
# 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
|
# 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'] = [x.name for x in self._inventory.get_hosts()]
|
||||||
variables['play_hosts'] = host_list
|
variables['ansible_play_hosts'] = [x.name for x in self._inventory.get_hosts(pattern=play.hosts or 'all', ignore_restrictions=True)]
|
||||||
variables['ansible_play_hosts'] = host_list
|
|
||||||
|
|
||||||
# the 'omit' value alows params to be left out if the variable they are based on is undefined
|
# the 'omit' value alows params to be left out if the variable they are based on is undefined
|
||||||
variables['omit'] = self._omit_token
|
variables['omit'] = self._omit_token
|
||||||
|
@ -490,7 +489,7 @@ class VariableManager:
|
||||||
if delegated_host_name in C.LOCALHOST:
|
if delegated_host_name in C.LOCALHOST:
|
||||||
delegated_host = self._inventory.localhost
|
delegated_host = self._inventory.localhost
|
||||||
else:
|
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
|
# check if the address matches, or if both the delegated_to host
|
||||||
# and the current host are in the list of localhost aliases
|
# and the current host are in the list of localhost aliases
|
||||||
if h.address == delegated_host_name:
|
if h.address == delegated_host_name:
|
||||||
|
|
|
@ -20,19 +20,17 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
import sys
|
|
||||||
|
|
||||||
from jinja2 import Undefined as j2undefined
|
from jinja2 import Undefined as j2undefined
|
||||||
|
|
||||||
from ansible import constants as C
|
from ansible import constants as C
|
||||||
from ansible.inventory.host import Host
|
|
||||||
from ansible.template import Templar
|
from ansible.template import Templar
|
||||||
|
|
||||||
STATIC_VARS = [
|
STATIC_VARS = [
|
||||||
'inventory_hostname', 'inventory_hostname_short',
|
'inventory_hostname', 'inventory_hostname_short',
|
||||||
'inventory_file', 'inventory_dir', 'playbook_dir',
|
'inventory_file', 'inventory_dir', 'playbook_dir',
|
||||||
'ansible_play_hosts', 'play_hosts', 'groups', 'ungrouped', 'group_names',
|
'ansible_play_hosts', 'play_hosts', 'groups',
|
||||||
'ansible_version', 'omit', 'role_names'
|
'ungrouped', 'group_names', 'ansible_version', 'omit', 'role_names'
|
||||||
]
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -103,15 +101,15 @@ class HostVars(collections.Mapping):
|
||||||
return self._find_host(host_name) is not None
|
return self._find_host(host_name) is not None
|
||||||
|
|
||||||
def __iter__(self):
|
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
|
yield host
|
||||||
|
|
||||||
def __len__(self):
|
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):
|
def __repr__(self):
|
||||||
out = {}
|
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
|
name = host.name
|
||||||
out[name] = self.get(name)
|
out[name] = self.get(name)
|
||||||
return repr(out)
|
return repr(out)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue