diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 0daaf9263a4..248a5273180 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -592,25 +592,31 @@ class AnsibleEnvironment(NativeEnvironment): if not C.JINJA2_BYTECODE_CACHE: return super().compile(source, name=name, filename=filename, raw=raw, defer_init=defer_init) # type: ignore[call-overload] - # Environment._parse - parsed = Parser(self, source, name, filename).parse() - - # This wrapper ensures that all templates are not considered literal/constant - eval_ctx = nodes.ScopedEvalContextModifier(lineno=-1) - eval_ctx.options = [nodes.Keyword('volatile', nodes.Const(True))] - eval_ctx.body = parsed.body - - # Environment._generate - generated = generate( - nodes.Template([eval_ctx], lineno=-1), - self, - name, - filename, - defer_init=False, - optimized=self.optimized, - ) + try: + # Environment._parse + parsed = Parser(self, source, name, filename).parse() + + # This wrapper ensures that all templates are not considered literal/constant + eval_ctx = nodes.ScopedEvalContextModifier(lineno=-1) + eval_ctx.options = [nodes.Keyword('volatile', nodes.Const(True))] + eval_ctx.body = parsed.body + + # Environment._generate + generated = generate( + nodes.Template([eval_ctx], lineno=-1), + self, + name, + filename, + defer_init=False, + optimized=self.optimized, + ) + + if raw: + return generated - return compile(generated, filename or '