@ -100,33 +100,33 @@ def lookup(name, *args, **kwargs):
else :
else :
raise errors . AnsibleError ( " lookup plugin ( %s ) not found " % name )
raise errors . AnsibleError ( " lookup plugin ( %s ) not found " % name )
def template ( basedir , varname , vars, lookup_fatal = True , depth = 0 , expand_lists = True , convert_bare = False , fail_on_undefined = False , filter_fatal = True ) :
def template ( basedir , varname , template vars, lookup_fatal = True , depth = 0 , expand_lists = True , convert_bare = False , fail_on_undefined = False , filter_fatal = True ) :
''' templates a data structure by traversing it and substituting for other data structures '''
''' templates a data structure by traversing it and substituting for other data structures '''
from ansible import utils
from ansible import utils
try :
try :
if convert_bare and isinstance ( varname , basestring ) :
if convert_bare and isinstance ( varname , basestring ) :
first_part = varname . split ( " . " ) [ 0 ] . split ( " [ " ) [ 0 ]
first_part = varname . split ( " . " ) [ 0 ] . split ( " [ " ) [ 0 ]
if first_part in vars and ' {{ ' not in varname and ' $ ' not in varname :
if first_part in template vars and ' {{ ' not in varname and ' $ ' not in varname :
varname = " {{ %s }} " % varname
varname = " {{ %s }} " % varname
if isinstance ( varname , basestring ) :
if isinstance ( varname , basestring ) :
if ' {{ ' in varname or ' { % ' in varname :
if ' {{ ' in varname or ' { % ' in varname :
varname = template_from_string ( basedir , varname , vars, fail_on_undefined )
varname = template_from_string ( basedir , varname , template vars, fail_on_undefined )
if ( varname . startswith ( " { " ) and not varname . startswith ( " {{ " ) ) or varname . startswith ( " [ " ) :
if ( varname . startswith ( " { " ) and not varname . startswith ( " {{ " ) ) or varname . startswith ( " [ " ) :
eval_results = utils . safe_eval ( varname , locals = vars, include_exceptions = True )
eval_results = utils . safe_eval ( varname , locals = template vars, include_exceptions = True )
if eval_results [ 1 ] is None :
if eval_results [ 1 ] is None :
varname = eval_results [ 0 ]
varname = eval_results [ 0 ]
return varname
return varname
elif isinstance ( varname , ( list , tuple ) ) :
elif isinstance ( varname , ( list , tuple ) ) :
return [ template ( basedir , v , vars, lookup_fatal , depth , expand_lists , fail_on_undefined= fail_on_undefined ) for v in varname ]
return [ template ( basedir , v , template vars, lookup_fatal , depth , expand_lists , convert_bare, fail_on_undefined , filter_fatal ) for v in varname ]
elif isinstance ( varname , dict ) :
elif isinstance ( varname , dict ) :
d = { }
d = { }
for ( k , v ) in varname . iteritems ( ) :
for ( k , v ) in varname . iteritems ( ) :
d [ k ] = template ( basedir , v , vars, lookup_fatal , depth , expand_lists , fail_on_undefined= fail_on_undefined )
d [ k ] = template ( basedir , v , template vars, lookup_fatal , depth , expand_lists , convert_bare, fail_on_undefined , filter_fatal )
return d
return d
else :
else :
return varname
return varname