diff --git a/changelogs/fragments/4660-mh-added-do-raise.yaml b/changelogs/fragments/4660-mh-added-do-raise.yaml new file mode 100644 index 0000000000..01585afb96 --- /dev/null +++ b/changelogs/fragments/4660-mh-added-do-raise.yaml @@ -0,0 +1,2 @@ +minor_changes: + - ModuleHelper module utils - ``ModuleHelperBase`` now has a convenience method ``do_raise`` (https://github.com/ansible-collections/community.general/pull/4660). diff --git a/plugins/module_utils/mh/base.py b/plugins/module_utils/mh/base.py index 3d311de865..0871a527be 100644 --- a/plugins/module_utils/mh/base.py +++ b/plugins/module_utils/mh/base.py @@ -31,6 +31,9 @@ class ModuleHelperBase(object): def diff_mode(self): return self.module._diff + def do_raise(self, *args, **kwargs): + raise _MHE(*args, **kwargs) + def __getattr__(self, attr): if attr in self._delegated_to_module: return getattr(self.module, attr) diff --git a/plugins/module_utils/mh/mixins/cmd.py b/plugins/module_utils/mh/mixins/cmd.py index 88b89d159b..58d50fbdf8 100644 --- a/plugins/module_utils/mh/mixins/cmd.py +++ b/plugins/module_utils/mh/mixins/cmd.py @@ -128,8 +128,7 @@ class CmdMixin(object): for param in param_list: if isinstance(param, dict): if len(param) != 1: - raise self.ModuleHelperException("run_command parameter as a dict must " - "contain only one key: {0}".format(param)) + self.do_raise("run_command parameter as a dict must contain only one key: {0}".format(param)) _param = list(param.keys())[0] fmt = find_format(_param) value = param[_param] @@ -141,9 +140,9 @@ class CmdMixin(object): fmt = find_format(param) value = extra_params[param] else: - raise self.ModuleHelperException('Cannot determine value for parameter: {0}'.format(param)) + self.do_raise('Cannot determine value for parameter: {0}'.format(param)) else: - raise self.ModuleHelperException("run_command parameter must be either a str or a dict: {0}".format(param)) + self.do_raise("run_command parameter must be either a str or a dict: {0}".format(param)) cmd_args = add_arg_formatted_param(cmd_args, fmt, value) return cmd_args