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
pull/421/head
Andrew Pantuso 2022-03-08 07:23:09 -05:00 committed by GitHub
parent 47d9fad45f
commit 033bab7db1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -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).

View File

@ -21,9 +21,11 @@ __metaclass__ = type
import abc import abc
import os import os
import stat import stat
import traceback
from ansible.module_utils import six 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 ( from ansible_collections.community.crypto.plugins.module_utils.openssh.utils import (
parse_openssh_version, parse_openssh_version,
) )
@ -75,7 +77,14 @@ class OpensshModule(object):
self.check_mode = self.module.check_mode self.check_mode = self.module.check_mode
def execute(self): def execute(self):
try:
self._execute() 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) self.module.exit_json(**self.result)
@abc.abstractmethod @abc.abstractmethod