Handle TemplateNotFound to render more helpful error message.

At the point the exception is handled, it is likely due to error loading
a sub-template included from main template. Besides file not found, it
can be caused also by include path failing Jinja2 checks. Now, when
rendering the exception from Jinja, it will include exception name. This
will give basic context or *what* the error is.

Fixes #7103
Fixes #7105
pull/7105/merge
Paul Sokolovsky 10 years ago committed by James Cammarata
parent 52c7d50f1e
commit f8bf9cdeec

@ -87,7 +87,7 @@ class ActionModule(object):
try:
resultant = template.template_from_file(self.runner.basedir, source, inject, vault_password=self.runner.vault_pass)
except Exception, e:
result = dict(failed=True, msg=str(e))
result = dict(failed=True, msg=type(e).__name__ + ": " + str(e))
return ReturnData(conn=conn, comm_ok=False, result=result)
local_md5 = utils.md5s(resultant)

@ -281,6 +281,16 @@ def template_from_file(basedir, path, vars, vault_password=None):
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals, fail_on_undefined), shared=True)))
except jinja2.exceptions.UndefinedError, e:
raise errors.AnsibleUndefinedVariable("One or more undefined variables: %s" % str(e))
except jinja2.exceptions.TemplateNotFound, e:
# Throw an exception which includes a more user friendly error message
# This likely will happen for included sub-template. Not that besides
# pure "file not found" it may happen due to Jinja2's "security"
# checks on path.
values = {'name': realpath, 'subname': str(e)}
msg = 'file: %(name)s, error: Cannot find/not allowed to load (include) template %(subname)s' % \
values
error = errors.AnsibleError(msg)
raise error
# The low level calls above do not preserve the newline
# characters at the end of the input data, so we use the

Loading…
Cancel
Save