Add fail_on_undefined flag
Add a fail_on_undefined flag to the template and template_from_string methods. If this flag is true, then re-raise the ninja2.excpetions.UndefinedError instead of swallowing it.
This commit is contained in:
parent
cbb1808f05
commit
aecb71d8b7
2 changed files with 9 additions and 16 deletions
|
@ -30,7 +30,6 @@ import base64
|
|||
import sys
|
||||
import shlex
|
||||
import pipes
|
||||
import re
|
||||
import jinja2
|
||||
|
||||
import ansible.constants as C
|
||||
|
@ -576,8 +575,8 @@ class Runner(object):
|
|||
|
||||
# render module_args and complex_args templates
|
||||
try:
|
||||
module_args = template.template(self.basedir, module_args, inject)
|
||||
complex_args = template.template(self.basedir, complex_args, inject)
|
||||
module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
|
||||
complex_args = template.template(self.basedir, complex_args, inject, fail_on_undefined=self.error_on_undefined_vars)
|
||||
except jinja2.exceptions.UndefinedError, e:
|
||||
raise errors.AnsibleUndefinedVariable("Undefined variables: %s" % str(e))
|
||||
|
||||
|
@ -694,16 +693,8 @@ class Runner(object):
|
|||
|
||||
# *****************************************************
|
||||
|
||||
def _contains_undefined_vars(self, module_args):
|
||||
''' return true if there are undefined variables '''
|
||||
return '{{' in module_args
|
||||
|
||||
def _copy_module(self, conn, tmp, module_name, module_args, inject, complex_args=None):
|
||||
''' transfer a module over SFTP, does not run it '''
|
||||
if self.error_on_undefined_vars and self._contains_undefined_vars(module_args):
|
||||
vars = re.findall(r'{{(.*?)}}', module_args)
|
||||
raise errors.AnsibleUndefinedVariable("Undefined variables: %s" %
|
||||
', '.join(vars))
|
||||
|
||||
# FIXME if complex args is none, set to {}
|
||||
|
||||
|
|
|
@ -295,7 +295,7 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis
|
|||
|
||||
# TODO: varname is misnamed here
|
||||
|
||||
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False):
|
||||
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False, fail_on_undefined=False):
|
||||
''' templates a data structure by traversing it and substituting for other data structures '''
|
||||
|
||||
if convert_bare and isinstance(varname, basestring):
|
||||
|
@ -305,7 +305,7 @@ 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)
|
||||
varname = template_from_string(basedir, varname, vars, fail_on_undefined)
|
||||
if not '$' in varname:
|
||||
return varname
|
||||
|
||||
|
@ -461,7 +461,7 @@ def template_from_file(basedir, path, vars):
|
|||
res = res + '\n'
|
||||
return template(basedir, res, vars)
|
||||
|
||||
def template_from_string(basedir, data, vars):
|
||||
def template_from_string(basedir, data, vars, fail_on_undefined=False):
|
||||
''' run a string through the (Jinja2) templating engine '''
|
||||
|
||||
try:
|
||||
|
@ -496,7 +496,9 @@ def template_from_string(basedir, data, vars):
|
|||
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
|
||||
return res
|
||||
except jinja2.exceptions.UndefinedError:
|
||||
raise
|
||||
if fail_on_undefined:
|
||||
raise
|
||||
else:
|
||||
# this shouldn't happen due to undeclared check above
|
||||
# return data
|
||||
return data
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue