This allows variable references in playbooks in a different way.

pull/4738/head
Michael DeHaan 11 years ago
parent b5c9d5a59b
commit 90cce35919

@ -49,7 +49,9 @@ class ActionModule(object):
else:
result = dict(msg=args['msg'])
elif 'var' in args:
(intermediate, exception) = utils.safe_eval(args['var'], inject, include_exceptions=True, template_call=True)
results = utils.safe_eval(args['var'], inject, include_exceptions=True, template_call=True)
intermediate = results[0]
exception = results[1]
if exception is not None:
intermediate = "failed to evaluate: %s" % str(exception)
result[args['var']] = intermediate

@ -917,11 +917,17 @@ def safe_eval(str, locals=None, include_exceptions=False, template_call=False):
# do not allow method calls to modules
if not isinstance(str, basestring):
# already templated to a datastructure, perhaps?
if include_exceptions:
return (str, None)
return str
if re.search(r'\w\.\w+\(', str):
if include_exceptions:
return (str, None)
return str
# do not allow imports
if re.search(r'import \w+', str):
if include_exceptions:
return (str, None)
return str
try:
result = None

@ -30,6 +30,7 @@ import subprocess
import datetime
import pwd
import ast
import traceback
class Globals(object):
@ -304,6 +305,7 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis
def template(basedir, varname, 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 '''
from ansible import utils
try:
if convert_bare and isinstance(varname, basestring):
@ -314,6 +316,12 @@ def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=Tr
if isinstance(varname, basestring):
if '{{' in varname or '{%' in varname:
varname = template_from_string(basedir, varname, vars, fail_on_undefined)
if (varname.startswith("{") and not varname.startswith("{{")) or varname.startswith("["):
eval_results = utils.safe_eval(varname, locals=vars, include_exceptions=True)
if eval_results[1] is None:
varname = eval_results[0]
if not '$' in varname:
return varname

Loading…
Cancel
Save