summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2024-09-24 19:58:41 +0200
committerGitHub <noreply@github.com>2024-09-24 19:58:41 +0200
commit0c8efa29b2c1d891f6a2c12f78bc2cdff9b70bd2 (patch)
tree39d13ede3a38b8c09cf07ad49f351d0cd8f23b36
parentsplit: Fix incorrect example result of the split builtin filter (#83982) (diff)
downloadansible-0c8efa29b2c1d891f6a2c12f78bc2cdff9b70bd2.tar.xz
ansible-0c8efa29b2c1d891f6a2c12f78bc2cdff9b70bd2.zip
Ansible Errors, Don't hide stacked messages when yaml (#83933)
Also remove redundant msg now that we fixed yaml case So no more need to %s % e. Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r--changelogs/fragments/fix_errors.yml2
-rw-r--r--lib/ansible/errors/__init__.py8
-rw-r--r--lib/ansible/playbook/base.py10
-rw-r--r--test/integration/targets/template/tasks/main.yml4
4 files changed, 16 insertions, 8 deletions
diff --git a/changelogs/fragments/fix_errors.yml b/changelogs/fragments/fix_errors.yml
new file mode 100644
index 0000000000..995cc28ffd
--- /dev/null
+++ b/changelogs/fragments/fix_errors.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - Errors now preserve stacked error messages even when YAML is involved.
diff --git a/lib/ansible/errors/__init__.py b/lib/ansible/errors/__init__.py
index f003b589c8..78853757f8 100644
--- a/lib/ansible/errors/__init__.py
+++ b/lib/ansible/errors/__init__.py
@@ -66,14 +66,18 @@ class AnsibleError(Exception):
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
message = [self._message]
+
+ # Add from previous exceptions
+ if self.orig_exc:
+ message.append('. %s' % to_native(self.orig_exc))
+
+ # Add from yaml to give specific file/line no
if isinstance(self.obj, AnsibleBaseYAMLObject):
extended_error = self._get_extended_error()
if extended_error and not self._suppress_extended_error:
message.append(
'\n\n%s' % to_native(extended_error)
)
- elif self.orig_exc:
- message.append('. %s' % to_native(self.orig_exc))
return ''.join(message)
diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py
index f93fb5ef5f..46483efda2 100644
--- a/lib/ansible/playbook/base.py
+++ b/lib/ansible/playbook/base.py
@@ -19,7 +19,7 @@ from ansible import context
from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleAssertionError
from ansible.module_utils.six import string_types
from ansible.module_utils.parsing.convert_bool import boolean
-from ansible.module_utils.common.text.converters import to_text, to_native
+from ansible.module_utils.common.text.converters import to_text
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.attribute import Attribute, FieldAttribute, ConnectionFieldAttribute, NonInheritableFieldAttribute
from ansible.plugins.loader import module_loader, action_loader
@@ -567,14 +567,14 @@ class FieldAttributeBase:
setattr(self, name, value)
except (TypeError, ValueError) as e:
value = getattr(self, name)
- raise AnsibleParserError("the field '%s' has an invalid value (%s), and could not be converted to %s. "
- "The error was: %s" % (name, value, attribute.isa, e), obj=self.get_ds(), orig_exc=e)
+ raise AnsibleParserError(f"the field '{name}' has an invalid value ({value!r}), and could not be converted to {attribute.isa}.",
+ obj=self.get_ds(), orig_exc=e)
except (AnsibleUndefinedVariable, UndefinedError) as e:
if templar._fail_on_undefined_errors and name != 'name':
if name == 'args':
- msg = "The task includes an option with an undefined variable. The error was: %s" % (to_native(e))
+ msg = "The task includes an option with an undefined variable."
else:
- msg = "The field '%s' has an invalid value, which includes an undefined variable. The error was: %s" % (name, to_native(e))
+ msg = f"The field '{name}' has an invalid value, which includes an undefined variable."
raise AnsibleParserError(msg, obj=self.get_ds(), orig_exc=e)
self._finalized = True
diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml
index bd2d8db60e..36f85b8b79 100644
--- a/test/integration/targets/template/tasks/main.yml
+++ b/test/integration/targets/template/tasks/main.yml
@@ -714,7 +714,9 @@
- name: check that proper error message is emitted when in operator is used
assert:
- that: "\"The error was: 'y' is undefined\n\n\" in error.msg"
+ that:
+ - '"The task includes an option with an undefined variable" in error.msg'
+ - "\"'y' is undefined\n\n\" in error.msg"
- template:
src: template_import_macro_globals.j2