diff --git a/changelogs/fragments/78703_undefined.yml b/changelogs/fragments/78703_undefined.yml new file mode 100644 index 00000000000..d9e4a48770e --- /dev/null +++ b/changelogs/fragments/78703_undefined.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - Do not print undefined error message twice (https://github.com/ansible/ansible/issues/78703). diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index efdaa0aec74..77a03389ed9 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -1011,12 +1011,16 @@ class Templar: if unsafe: res = wrap_var(res) return res - except (UndefinedError, AnsibleUndefinedVariable) as e: + except UndefinedError as e: if fail_on_undefined: - raise AnsibleUndefinedVariable(e, orig_exc=e) - else: - display.debug("Ignoring undefined failure: %s" % to_text(e)) - return data + raise AnsibleUndefinedVariable(e) + display.debug("Ignoring undefined failure: %s" % to_text(e)) + return data + except AnsibleUndefinedVariable as e: + if fail_on_undefined: + raise + display.debug("Ignoring undefined failure: %s" % to_text(e)) + return data # for backwards compatibility in case anyone is using old private method directly _do_template = do_template diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml index 0a2bebaae99..bd2d8db60ef 100644 --- a/test/integration/targets/template/tasks/main.yml +++ b/test/integration/targets/template/tasks/main.yml @@ -714,7 +714,7 @@ - name: check that proper error message is emitted when in operator is used assert: - that: "\"'y' is undefined\" in error.msg" + that: "\"The error was: 'y' is undefined\n\n\" in error.msg" - template: src: template_import_macro_globals.j2