Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ignore SyntaxError from jinja2.Environment.from_string #82607

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/82606-template-python-syntax-error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- templating - ensure syntax errors originating from a template being compiled into Python code object result in a failure (https://github.com/ansible/ansible/issues/82606)
2 changes: 1 addition & 1 deletion lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=

try:
t = myenv.from_string(data)
except TemplateSyntaxError as e:
except (TemplateSyntaxError, SyntaxError) as e:
raise AnsibleError("template error while templating string: %s. String: %s" % (to_native(e), to_native(data)), orig_exc=e)
except Exception as e:
if 'recursion' in to_native(e):
Expand Down
11 changes: 11 additions & 0 deletions test/integration/targets/templating/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@
- result is failed
- >-
"TemplateSyntaxError: Could not load \"asdf \": 'invalid plugin name: ansible.builtin.asdf '" in result.msg
- name: Make sure syntax errors originating from a template being compiled into Python code object result in a failure
debug:
msg: "{{ lookup('vars', 'v1', default='', default='') }}"
ignore_errors: true
register: r

- assert:
that:
- r is failed
- "'keyword argument repeated' in r.msg"