From c5cbe2943be0665ba1297c588b51d4d275c73ef4 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 11 Jul 2021 11:43:40 +1200 Subject: [PATCH] =?UTF-8?q?module=5Fhelper=20cmd=20-=20added=20feature=20f?= =?UTF-8?q?lag=20to=20control=20whether=20CmdMixin=20adds=20rc,=20out=20an?= =?UTF-8?q?d=20err=20t=E2=80=A6=20(#2922)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added feature flag to control whether CmdMixin adds rc, out and err to the result of the module * added changelog fragment * changed from a global flag to parameters in run_command * updated changelog * fixed brainless copy-paste of yours truly --- .../2922-mh-cmd-output-feature-flag.yml | 2 ++ plugins/module_utils/mh/mixins/cmd.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/2922-mh-cmd-output-feature-flag.yml diff --git a/changelogs/fragments/2922-mh-cmd-output-feature-flag.yml b/changelogs/fragments/2922-mh-cmd-output-feature-flag.yml new file mode 100644 index 0000000000..e071e3413b --- /dev/null +++ b/changelogs/fragments/2922-mh-cmd-output-feature-flag.yml @@ -0,0 +1,2 @@ +minor_changes: + - module_helper module utils - added feature flag parameters to ``CmdMixin`` to control whether ``rc``, ``out`` and ``err`` are automatically added to the module output (https://github.com/ansible-collections/community.general/pull/2922). diff --git a/plugins/module_utils/mh/mixins/cmd.py b/plugins/module_utils/mh/mixins/cmd.py index 0367b6173c..aed4174c4f 100644 --- a/plugins/module_utils/mh/mixins/cmd.py +++ b/plugins/module_utils/mh/mixins/cmd.py @@ -152,7 +152,14 @@ class CmdMixin(object): def process_command_output(self, rc, out, err): return rc, out, err - def run_command(self, extra_params=None, params=None, process_output=None, *args, **kwargs): + def run_command(self, + extra_params=None, + params=None, + process_output=None, + publish_rc=True, + publish_out=True, + publish_err=True, + *args, **kwargs): self.vars.cmd_args = self._calculate_args(extra_params, params) options = dict(self.run_command_fixed_options) options['check_rc'] = options.get('check_rc', self.check_rc) @@ -166,7 +173,12 @@ class CmdMixin(object): self.update_output(force_lang=self.force_lang) options['environ_update'] = env_update rc, out, err = self.module.run_command(self.vars.cmd_args, *args, **options) - self.update_output(rc=rc, stdout=out, stderr=err) + if publish_rc: + self.update_output(rc=rc) + if publish_out: + self.update_output(stdout=out) + if publish_err: + self.update_output(stderr=err) if process_output is None: _process = self.process_command_output else: