From fa477ebb356610a3dffdfe036ad8a321ce37c525 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 20 May 2021 20:11:08 +0200 Subject: [PATCH] ModuleHelper: CmdMixin custom function for processing cmd results (#2564) (#2565) * MH: custom function for processing cmd results * added changelog fragment * removed case of process_output being a str (cherry picked from commit 1403f5edccd34027b25dfda9fa61309e16b0f3d2) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/2564-mh-cmd-process-output.yml | 2 ++ plugins/module_utils/mh/mixins/cmd.py | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/2564-mh-cmd-process-output.yml diff --git a/changelogs/fragments/2564-mh-cmd-process-output.yml b/changelogs/fragments/2564-mh-cmd-process-output.yml new file mode 100644 index 0000000000..717c0d7fbb --- /dev/null +++ b/changelogs/fragments/2564-mh-cmd-process-output.yml @@ -0,0 +1,2 @@ +minor_changes: + - module_helper module utils - method ``CmdMixin.run_command()`` now accepts ``process_output`` specifying a function to process the outcome of the underlying ``module.run_command()`` (https://github.com/ansible-collections/community.general/pull/2564). diff --git a/plugins/module_utils/mh/mixins/cmd.py b/plugins/module_utils/mh/mixins/cmd.py index eb7cc698cc..724708868e 100644 --- a/plugins/module_utils/mh/mixins/cmd.py +++ b/plugins/module_utils/mh/mixins/cmd.py @@ -152,7 +152,7 @@ class CmdMixin(object): def process_command_output(self, rc, out, err): return rc, out, err - def run_command(self, extra_params=None, params=None, *args, **kwargs): + def run_command(self, extra_params=None, params=None, process_output=None, *args, **kwargs): self.vars.cmd_args = self._calculate_args(extra_params, params) options = dict(self.run_command_fixed_options) env_update = dict(options.get('environ_update', {})) @@ -164,4 +164,9 @@ class CmdMixin(object): options.update(kwargs) rc, out, err = self.module.run_command(self.vars.cmd_args, *args, **options) self.update_output(rc=rc, stdout=out, stderr=err) - return self.process_command_output(rc, out, err) + if process_output is None: + _process = self.process_command_output + else: + _process = process_output + + return _process(rc, out, err)