From 033bab7db12a68436737718a7824325e308e9822 Mon Sep 17 00:00:00 2001 From: Andrew Pantuso Date: Tue, 8 Mar 2022 07:23:09 -0500 Subject: [PATCH] openssh_* - catch and report top-level exceptions via `fail_json` (#417) * ensure exceptions are properly reported * adding changelog fragment * applying review suggestions * typo * adding back exception msg --- .../417-openssh_modules-fix-exception-reporting.yml | 4 ++++ plugins/module_utils/openssh/backends/common.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/417-openssh_modules-fix-exception-reporting.yml diff --git a/changelogs/fragments/417-openssh_modules-fix-exception-reporting.yml b/changelogs/fragments/417-openssh_modules-fix-exception-reporting.yml new file mode 100644 index 00000000..e923f72f --- /dev/null +++ b/changelogs/fragments/417-openssh_modules-fix-exception-reporting.yml @@ -0,0 +1,4 @@ +--- +bugfixes: +- openssh_* modules - fix exception handling to report traceback to users for + enhanced traceability (https://github.com/ansible-collections/community.crypto/pull/417). diff --git a/plugins/module_utils/openssh/backends/common.py b/plugins/module_utils/openssh/backends/common.py index fce51e79..d9689616 100644 --- a/plugins/module_utils/openssh/backends/common.py +++ b/plugins/module_utils/openssh/backends/common.py @@ -21,9 +21,11 @@ __metaclass__ = type import abc import os import stat +import traceback from ansible.module_utils import six +from ansible.module_utils.common.text.converters import to_native from ansible_collections.community.crypto.plugins.module_utils.openssh.utils import ( parse_openssh_version, ) @@ -75,7 +77,14 @@ class OpensshModule(object): self.check_mode = self.module.check_mode def execute(self): - self._execute() + try: + self._execute() + except Exception as e: + self.module.fail_json( + msg="unexpected error occurred: %s" % to_native(e), + exception=traceback.format_exc(), + ) + self.module.exit_json(**self.result) @abc.abstractmethod