@ -21,26 +21,9 @@ __metaclass__ = type
from collections . abc import Mapping
from collections . abc import Mapping
from ansible import constants as C
from ansible . template import Templar , AnsibleUndefined
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 ' ]
__all__ = [ ' HostVars ' , ' HostVarsVars ' ]
@ -134,10 +117,12 @@ class HostVarsVars(Mapping):
def __init__ ( self , variables , loader ) :
def __init__ ( self , variables , loader ) :
self . _vars = variables
self . _vars = variables
self . _loader = loader
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 ) :
def __getitem__ ( self , var ) :
templar = Templar ( variables = self . _vars , loader = self . _loader )
return self . _templar . template ( self . _vars [ var ] , fail_on_undefined = False , static_vars = C . INTERNAL_STATIC_VARS )
return templar . template ( self . _vars [ var ] , fail_on_undefined = False , static_vars = STATIC_VARS )
def __contains__ ( self , var ) :
def __contains__ ( self , var ) :
return ( var in self . _vars )
return ( var in self . _vars )
@ -150,5 +135,4 @@ class HostVarsVars(Mapping):
return len ( self . _vars . keys ( ) )
return len ( self . _vars . keys ( ) )
def __repr__ ( self ) :
def __repr__ ( self ) :
templar = Templar ( variables = self . _vars , loader = self . _loader )
return repr ( self . _templar . template ( self . _vars , fail_on_undefined = False , static_vars = C . INTERNAL_STATIC_VARS ) )
return repr ( templar . template ( self . _vars , fail_on_undefined = False , static_vars = STATIC_VARS ) )