|
|
|
@ -19,26 +19,9 @@ from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
|
|
|
|
|
|
from ansible import constants as C
|
|
|
|
|
from ansible.template import Templar, AnsibleUndefined
|
|
|
|
|
|
|
|
|
|
STATIC_VARS = [
|
|
|
|
|
'ansible_version',
|
|
|
|
|
'ansible_play_hosts',
|
|
|
|
|
'ansible_dependent_role_names',
|
|
|
|
|
'ansible_play_role_names',
|
|
|
|
|
'ansible_role_names',
|
|
|
|
|
'inventory_hostname',
|
|
|
|
|
'inventory_hostname_short',
|
|
|
|
|
'inventory_file',
|
|
|
|
|
'inventory_dir',
|
|
|
|
|
'groups',
|
|
|
|
|
'group_names',
|
|
|
|
|
'omit',
|
|
|
|
|
'playbook_dir',
|
|
|
|
|
'play_hosts',
|
|
|
|
|
'role_names',
|
|
|
|
|
'ungrouped',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
__all__ = ['HostVars', 'HostVarsVars']
|
|
|
|
|
|
|
|
|
@ -131,10 +114,12 @@ class HostVarsVars(Mapping):
|
|
|
|
|
def __init__(self, variables, loader):
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, var):
|
|
|
|
|
templar = Templar(variables=self._vars, loader=self._loader)
|
|
|
|
|
return templar.template(self._vars[var], fail_on_undefined=False, static_vars=STATIC_VARS)
|
|
|
|
|
return self._templar.template(self._vars[var], fail_on_undefined=False, static_vars=C.INTERNAL_STATIC_VARS)
|
|
|
|
|
|
|
|
|
|
def __contains__(self, var):
|
|
|
|
|
return (var in self._vars)
|
|
|
|
@ -146,5 +131,4 @@ class HostVarsVars(Mapping):
|
|
|
|
|
return len(self._vars.keys())
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
templar = Templar(variables=self._vars, loader=self._loader)
|
|
|
|
|
return repr(templar.template(self._vars, fail_on_undefined=False, static_vars=STATIC_VARS))
|
|
|
|
|
return repr(self._templar.template(self._vars, fail_on_undefined=False, static_vars=C.INTERNAL_STATIC_VARS))
|
|
|
|
|