From 0e0fce54ab210a84b6ef00451bfc181012947a62 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 20 Jul 2023 08:34:35 +0200 Subject: [PATCH] [PR #6968/f6714eda backport][stable-6] cmd_runner module utils: fix bug when argument spec has implicit type (#6977) cmd_runner module utils: fix bug when argument spec has implicit type (#6968) * cmd_runner module utils: fix bug when argument spec has implicit type * add changelog frag (cherry picked from commit f6714edabb4b26bf1955c6e7e2a3500fc4caaa3c) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/6968-cmdrunner-implicit-type.yml | 2 ++ plugins/module_utils/cmd_runner.py | 2 +- .../targets/cmd_runner/library/cmd_echo.py | 1 + .../targets/cmd_runner/vars/main.yml | 17 +++++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/6968-cmdrunner-implicit-type.yml diff --git a/changelogs/fragments/6968-cmdrunner-implicit-type.yml b/changelogs/fragments/6968-cmdrunner-implicit-type.yml new file mode 100644 index 0000000000..6474c57fe0 --- /dev/null +++ b/changelogs/fragments/6968-cmdrunner-implicit-type.yml @@ -0,0 +1,2 @@ +bugfixes: + - cmd_runner module utils - when a parameter in ``argument_spec`` has no type, meaning it is implicitly a ``str``, ``CmdRunner`` would fail trying to find the ``type`` key in that dictionary (https://github.com/ansible-collections/community.general/pull/6968). diff --git a/plugins/module_utils/cmd_runner.py b/plugins/module_utils/cmd_runner.py index 21d61a6a5c..ddeac941c0 100644 --- a/plugins/module_utils/cmd_runner.py +++ b/plugins/module_utils/cmd_runner.py @@ -203,7 +203,7 @@ class CmdRunner(object): for mod_param_name, spec in iteritems(module.argument_spec): if mod_param_name not in self.arg_formats: - self.arg_formats[mod_param_name] = _Format.as_default_type(spec['type'], mod_param_name) + self.arg_formats[mod_param_name] = _Format.as_default_type(spec.get('type', 'str'), mod_param_name) def __call__(self, args_order=None, output_process=None, ignore_value_none=True, check_mode_skip=False, check_mode_return=None, **kwargs): if output_process is None: diff --git a/tests/integration/targets/cmd_runner/library/cmd_echo.py b/tests/integration/targets/cmd_runner/library/cmd_echo.py index cd87662647..47af478cd2 100644 --- a/tests/integration/targets/cmd_runner/library/cmd_echo.py +++ b/tests/integration/targets/cmd_runner/library/cmd_echo.py @@ -26,6 +26,7 @@ def main(): arg_values=dict(type="dict", default={}), check_mode_skip=dict(type="bool", default=False), aa=dict(type="raw"), + tt=dict(), ), supports_check_mode=True, ) diff --git a/tests/integration/targets/cmd_runner/vars/main.yml b/tests/integration/targets/cmd_runner/vars/main.yml index 7f0027d499..a1d74934fd 100644 --- a/tests/integration/targets/cmd_runner/vars/main.yml +++ b/tests/integration/targets/cmd_runner/vars/main.yml @@ -121,3 +121,20 @@ cmd_echo_tests: - test_result.rc == None - test_result.out == None - test_result.err == None + + - name: set aa and tt value + arg_formats: + aa: + func: as_opt_eq_val + args: [--answer] + tt: + func: as_opt_val + args: [--tt-arg] + arg_order: 'aa tt' + arg_values: + tt: potatoes + aa: 11 + assertions: + - test_result.rc == 0 + - test_result.out == "-- --answer=11 --tt-arg potatoes\n" + - test_result.err == ""