From 7a0c321054bf3f9c238f09a8c29f140b255112c1 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Mon, 29 Jan 2024 17:00:53 -0800 Subject: [PATCH] Remove redundant undefined error message (#81867) Fixes: #78703 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/78703_undefined.yml | 3 +++ lib/ansible/template/__init__.py | 14 +++++++++----- test/integration/targets/template/tasks/main.yml | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/78703_undefined.yml 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