From 84eb37ffa7cbab514294485275835b9beb7d51be Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sat, 16 Mar 2024 10:04:38 +0100 Subject: [PATCH] added wrapper for fail_json and exit_json --- changelogs/fragments/0.1.1.yml | 1 + plugins/module_utils/module.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/changelogs/fragments/0.1.1.yml b/changelogs/fragments/0.1.1.yml index 6bcef84..79e17c8 100644 --- a/changelogs/fragments/0.1.1.yml +++ b/changelogs/fragments/0.1.1.yml @@ -2,3 +2,4 @@ minor_changes: - Added an type hint to the update_doc function - Added Type hint to the dictionary argument of the diff method + - added an wrapper for fail_json and exit_json diff --git a/plugins/module_utils/module.py b/plugins/module_utils/module.py index a42ad78..fc77ae7 100644 --- a/plugins/module_utils/module.py +++ b/plugins/module_utils/module.py @@ -168,12 +168,8 @@ class AnsibleModule(object): except Exception as exc: import traceback - self.module.fail_json( - "".join(traceback.format_exception(type(exc), exc, exc.__traceback__)), - **self.result, - ) - self.module.exit_json(**self.result) - raise Exception("exit_json failed") + self.fail("".join(traceback.format_exception(type(exc), exc, exc.__traceback__))) + self.exit() @classmethod def doc(cls) -> str: @@ -205,6 +201,14 @@ class AnsibleModule(object): ) ) + def fail(self, message: str) -> NoReturn: + """Wrapper for AnsibleModule.fail_json""" + self.module.fail_json(message, **self.result) + + def exit(self) -> NoReturn: + """Wrapper for AnsibleModule.exit_json""" + self.module.exit_json(**self.result) + class SystemdUnitModule(AnsibleModule): #: path of the unitfile managed by this module