Brian Coca 2 weeks ago committed by GitHub
commit 2ab5837946
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -21,6 +21,7 @@ from collections.abc import Mapping
from ansible import constants as C
from ansible.template import Templar, AnsibleUndefined
from ansible.utils.vars import combine_vars
__all__ = ['HostVars', 'HostVarsVars']
@ -75,7 +76,7 @@ class HostVars(Mapping):
data = self.raw_get(host_name)
if isinstance(data, AnsibleUndefined):
return data
return HostVarsVars(data, loader=self._loader)
return HostVarsVars(data, loader=self._loader, extra_vars=self._variable_manager._extra_vars)
def set_host_variable(self, host, varname, value):
self._variable_manager.set_host_variable(host, varname, value)
@ -111,12 +112,13 @@ class HostVars(Mapping):
class HostVarsVars(Mapping):
def __init__(self, variables, loader):
def __init__(self, variables, loader, extra_vars):
self._vars = variables
self._loader = loader
# NOTE: this only has access to the host's own vars,
# so templates that depend on vars in other scopes will not work.
self._templar = Templar(variables=self._vars, loader=self._loader)
# so templates that depend on vars in other scopes aside
# from self and 'extra vars' will not work.
self._templar = Templar(variables=combine_vars(self._vars, extra_vars), loader=self._loader)
def __getitem__(self, var):
return self._templar.template(self._vars[var], fail_on_undefined=False, static_vars=C.INTERNAL_STATIC_VARS)

@ -156,6 +156,7 @@ class VariableManager:
- task->get_vars (if there is a task context)
- vars_cache[host] (if there is a host context)
- extra vars
- magic vars (values from the engine, not overridable)
``_hosts`` and ``_hosts_all`` should be considered private args, with only internal trusted callers relying
on the functionality they provide. These arguments may be removed at a later date without a deprecation
@ -405,17 +406,22 @@ class VariableManager:
# fact non-persistent cache
all_vars = _combine_and_track(all_vars, self._nonpersistent_fact_cache.get(host.name, dict()), "set_fact")
# next, we merge in role params and task include params
# next, we merge in params (roles/include_tasks)
if task:
# special case for include tasks, where the include params
# special case for where the include params (role, include_tasks)
# may be specified in the vars field for the task, which should
# have higher precedence than the vars/np facts above
if task._role:
all_vars = _combine_and_track(all_vars, task._role.get_role_params(task.get_dep_chain()), "role params")
all_vars = _combine_and_track(all_vars, task.get_include_params(), "include params")
# extra vars
all_vars = _combine_and_track(all_vars, self._extra_vars, "extra vars")
# updated with extra vars
if self._extra_vars:
if host and not play and not task:
# Only take extra vars that override existing hostvars as that is what we are returning in this case
all_vars = _combine_and_track(all_vars, {k: v for (k, v) in self._extra_vars.items() if k in all_vars}, "extra vars")
else:
all_vars = _combine_and_track(all_vars, self._extra_vars, "extra vars")
# magic variables
all_vars = _combine_and_track(all_vars, magic_variables, "magic vars")
@ -428,19 +434,20 @@ class VariableManager:
# 'vars' magic var
if task or play:
# has to be copy, otherwise recursive ref
# TODO: either move to a tempalte object or deprecate, this can hog memory on large inventories
all_vars['vars'] = all_vars.copy()
# if we have a host and task and we're delegating to another host,
# figure out the variables for that host now so we don't have to rely on host vars later
if task and host and task.delegate_to is not None and include_delegate_to:
if include_delegate_to and task and host and task.delegate_to is not None:
all_vars['ansible_delegated_vars'], all_vars['_ansible_loop_cache'] = self._get_delegated_vars(play, task, all_vars)
display.debug("done with get_vars()")
if C.DEFAULT_DEBUG:
# Use VarsWithSources wrapper class to display var sources
return VarsWithSources.new_vars_with_sources(all_vars, _vars_sources)
else:
return all_vars
return all_vars
def _get_magic_variables(self, play, host, task, include_hostvars, _hosts=None, _hosts_all=None):
'''

Loading…
Cancel
Save