diff --git a/changelogs/fragments/11149-rv-exception.yml b/changelogs/fragments/11149-rv-exception.yml new file mode 100644 index 0000000000..3ca5595297 --- /dev/null +++ b/changelogs/fragments/11149-rv-exception.yml @@ -0,0 +1,3 @@ +minor_changes: + - rundeck module utils - improve handling the return value ``exception``. It now contains the full stack trace of the exception, while the message is included in ``msg`` (https://github.com/ansible-collections/community.general/pull/11149). + - vmadm - in case of failure, the module no longer returns the stderr output as ``exception``, but instead as ``stderr``. Other information (``stdout``, ``rc``) is now also returned (https://github.com/ansible-collections/community.general/pull/11149). diff --git a/plugins/module_utils/rundeck.py b/plugins/module_utils/rundeck.py index dca79639be..a9a213446e 100644 --- a/plugins/module_utils/rundeck.py +++ b/plugins/module_utils/rundeck.py @@ -5,9 +5,9 @@ from __future__ import annotations import json +import traceback from ansible.module_utils.urls import fetch_url, url_argument_spec -from ansible.module_utils.common.text.converters import to_native def api_argument_spec(): @@ -82,6 +82,10 @@ def api_request(module, endpoint, data=None, method="GET", content_type="applica json_response = json.loads(content) return json_response, info except AttributeError as error: - module.fail_json(msg="Rundeck API request error", exception=to_native(error), execution_info=info) + module.fail_json( + msg=f"Rundeck API request error: {error}", exception=traceback.format_exc(), execution_info=info + ) except ValueError as error: - module.fail_json(msg="No valid JSON response", exception=to_native(error), execution_info=content) + module.fail_json( + msg=f"No valid JSON response: {error}", exception=traceback.format_exc(), execution_info=content + ) diff --git a/plugins/modules/vmadm.py b/plugins/modules/vmadm.py index f657e9c20c..740ed91934 100644 --- a/plugins/modules/vmadm.py +++ b/plugins/modules/vmadm.py @@ -361,7 +361,7 @@ def get_vm_prop(module, uuid, prop): (rc, stdout, stderr) = module.run_command(cmd) if rc != 0: - module.fail_json(msg=f"Could not perform lookup of {prop} on {uuid}", exception=stderr) + module.fail_json(msg=f"Could not perform lookup of {prop} on {uuid}", rc=rc, stdout=stdout, stderr=stderr) try: stdout_json = json.loads(stdout) @@ -384,7 +384,7 @@ def get_vm_uuid(module, alias): (rc, stdout, stderr) = module.run_command(cmd) if rc != 0: - module.fail_json(msg=f"Could not retrieve UUID of {alias}", exception=stderr) + module.fail_json(msg=f"Could not retrieve UUID of {alias}", rc=rc, stdout=stdout, stderr=stderr) # If no VM was found matching the given alias, we get back an empty array. # That is not an error condition as we might be explicitly checking for its @@ -409,7 +409,7 @@ def get_all_vm_uuids(module): (rc, stdout, stderr) = module.run_command(cmd) if rc != 0: - module.fail_json(msg="Failed to get VMs list", exception=stderr) + module.fail_json(msg="Failed to get VMs list", rc=rc, stdout=stdout, stderr=stderr) try: stdout_json = json.loads(stdout) @@ -421,11 +421,11 @@ def get_all_vm_uuids(module): def new_vm(module, uuid, vm_state): payload_file = create_payload(module, uuid) - (rc, dummy, stderr) = vmadm_create_vm(module, payload_file) + (rc, stdout, stderr) = vmadm_create_vm(module, payload_file) if rc != 0: changed = False - module.fail_json(msg="Could not create VM", exception=stderr) + module.fail_json(msg="Could not create VM", rc=rc, stdout=stdout, stderr=stderr) else: changed = True # 'vmadm create' returns all output to stderr...