From cc28cde3a2b99ed32451bf6446ba60a34a817a62 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 4 Dec 2022 12:39:30 +0100 Subject: [PATCH] [PR #5647/be22ca06 backport][stable-6] cmd_runner: allow bool format to pass alternate (false) value (#5650) cmd_runner: allow bool format to pass alternate (false) value (#5647) * allow bool format to pass alternate (false) value * add changelog fragment (cherry picked from commit be22ca063354507fbafb6d55032be39636bbf036) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/5647-cmd-runner-as-bool-false.yml | 2 ++ plugins/module_utils/cmd_runner.py | 9 +++++++-- tests/unit/plugins/module_utils/test_cmd_runner.py | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/5647-cmd-runner-as-bool-false.yml diff --git a/changelogs/fragments/5647-cmd-runner-as-bool-false.yml b/changelogs/fragments/5647-cmd-runner-as-bool-false.yml new file mode 100644 index 0000000000..5dc447d235 --- /dev/null +++ b/changelogs/fragments/5647-cmd-runner-as-bool-false.yml @@ -0,0 +1,2 @@ +minor_changes: + - cmd_runner module utils - ``cmd_runner_fmt.as_bool()`` can now take an extra parameter to format when value is false (https://github.com/ansible-collections/community.general/pull/5647). diff --git a/plugins/module_utils/cmd_runner.py b/plugins/module_utils/cmd_runner.py index 38daac6716..21d61a6a5c 100644 --- a/plugins/module_utils/cmd_runner.py +++ b/plugins/module_utils/cmd_runner.py @@ -103,8 +103,13 @@ class _ArgFormat(object): class _Format(object): @staticmethod - def as_bool(args): - return _ArgFormat(lambda value: _ensure_list(args) if value else []) + def as_bool(args_true, args_false=None, ignore_none=None): + if args_false is not None: + if ignore_none is None: + ignore_none = False + else: + args_false = [] + return _ArgFormat(lambda value: _ensure_list(args_true) if value else _ensure_list(args_false), ignore_none=ignore_none) @staticmethod def as_bool_not(args): diff --git a/tests/unit/plugins/module_utils/test_cmd_runner.py b/tests/unit/plugins/module_utils/test_cmd_runner.py index f1c3e25a9b..7cec215a76 100644 --- a/tests/unit/plugins/module_utils/test_cmd_runner.py +++ b/tests/unit/plugins/module_utils/test_cmd_runner.py @@ -18,6 +18,10 @@ TC_FORMATS = dict( simple_boolean__true=(fmt.as_bool, ("--superflag",), True, ["--superflag"]), simple_boolean__false=(fmt.as_bool, ("--superflag",), False, []), simple_boolean__none=(fmt.as_bool, ("--superflag",), None, []), + simple_boolean_both__true=(fmt.as_bool, ("--superflag", "--falseflag"), True, ["--superflag"]), + simple_boolean_both__false=(fmt.as_bool, ("--superflag", "--falseflag"), False, ["--falseflag"]), + simple_boolean_both__none=(fmt.as_bool, ("--superflag", "--falseflag"), None, ["--falseflag"]), + simple_boolean_both__none_ig=(fmt.as_bool, ("--superflag", "--falseflag", True), None, []), simple_boolean_not__true=(fmt.as_bool_not, ("--superflag",), True, []), simple_boolean_not__false=(fmt.as_bool_not, ("--superflag",), False, ["--superflag"]), simple_boolean_not__none=(fmt.as_bool_not, ("--superflag",), None, ["--superflag"]),