From b49bf081f8fce789f78cbf996d201c1ac99add01 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Wed, 4 Jan 2023 23:59:06 +1300 Subject: [PATCH] ModuleHelper - fix bug when adjusting conflicting output (#5755) * ModuleHelper - fix bug when adjusting conflicting output * add changelog fragment * remove commented test code --- .../fragments/5755-mh-fix-output-conflict.yml | 2 ++ plugins/module_utils/mh/module_helper.py | 2 +- .../targets/module_helper/library/msimple.py | 6 +++- .../targets/module_helper/tasks/main.yml | 1 + .../tasks/msimple_output_conflict.yml | 31 +++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/5755-mh-fix-output-conflict.yml create mode 100644 tests/integration/targets/module_helper/tasks/msimple_output_conflict.yml diff --git a/changelogs/fragments/5755-mh-fix-output-conflict.yml b/changelogs/fragments/5755-mh-fix-output-conflict.yml new file mode 100644 index 00000000000..f433cc02903 --- /dev/null +++ b/changelogs/fragments/5755-mh-fix-output-conflict.yml @@ -0,0 +1,2 @@ +bugfixes: + - ModuleHelper - fix bug when adjusting the name of reserved output variables (https://github.com/ansible-collections/community.general/pull/5755). diff --git a/plugins/module_utils/mh/module_helper.py b/plugins/module_utils/mh/module_helper.py index c844acba500..51696b6ff6e 100644 --- a/plugins/module_utils/mh/module_helper.py +++ b/plugins/module_utils/mh/module_helper.py @@ -60,7 +60,7 @@ def output(self): vars_diff = self.vars.diff() or {} result['diff'] = dict_merge(dict(diff), vars_diff) - for varname in result: + for varname in list(result): if varname in self._output_conflict_list: result["_" + varname] = result[varname] del result[varname] diff --git a/tests/integration/targets/module_helper/library/msimple.py b/tests/integration/targets/module_helper/library/msimple.py index 60a49dccb18..3729b6c7028 100644 --- a/tests/integration/targets/module_helper/library/msimple.py +++ b/tests/integration/targets/module_helper/library/msimple.py @@ -35,12 +35,13 @@ class MSimple(ModuleHelper): - output_params = ('a', 'b', 'c') + output_params = ('a', 'b', 'c', 'm') module = dict( argument_spec=dict( a=dict(type='int', default=0), b=dict(type='str'), c=dict(type='str'), + m=dict(type='str'), ), supports_check_mode=True, ) @@ -65,6 +66,9 @@ def __run__(self): self.vars['c'] = str(self.vars.c) * 2 self.process_a3_bc() + if self.vars.m: + self.vars.msg = self.vars.m + def main(): msimple = MSimple() diff --git a/tests/integration/targets/module_helper/tasks/main.yml b/tests/integration/targets/module_helper/tasks/main.yml index 90006f701d1..2368cfcbb58 100644 --- a/tests/integration/targets/module_helper/tasks/main.yml +++ b/tests/integration/targets/module_helper/tasks/main.yml @@ -3,6 +3,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later - include_tasks: msimple.yml +- include_tasks: msimple_output_conflict.yml - include_tasks: mdepfail.yml - include_tasks: mstate.yml - include_tasks: msimpleda.yml diff --git a/tests/integration/targets/module_helper/tasks/msimple_output_conflict.yml b/tests/integration/targets/module_helper/tasks/msimple_output_conflict.yml new file mode 100644 index 00000000000..21ffd37d350 --- /dev/null +++ b/tests/integration/targets/module_helper/tasks/msimple_output_conflict.yml @@ -0,0 +1,31 @@ +# Copyright (c) 2023, Alexei Znamensky +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: test msimple (set a=80) + msimple: + a: 80 + register: simple1 + +- name: assert simple1 + assert: + that: + - simple1.a == 80 + - simple1.abc == "abc" + - simple1 is not changed + - simple1.value is none + +- name: test msimple 2 + msimple: + a: 80 + m: a message in a bottle + register: simple2 + +- name: assert simple2 + assert: + that: + - simple1.a == 80 + - simple1.abc == "abc" + - simple1 is not changed + - simple1.value is none + - 'simple2._msg == "a message in a bottle"'