From dac4b610c8a0dd67fad408649d855b8384017ddd Mon Sep 17 00:00:00 2001 From: Kate Case <kcase@redhat.com> Date: Tue, 7 Feb 2023 13:11:59 -0500 Subject: [command] run at least once when retries is 0 (#292) * [command] run at least once when retries is 0 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- changelogs/fragments/command_retries.yaml | 3 +++ docs/vyos.vyos.vyos_command_module.rst | 3 ++- plugins/modules/vyos_command.py | 9 ++++++--- tests/unit/modules/network/vyos/test_vyos_command.py | 8 +++++++- 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/command_retries.yaml diff --git a/changelogs/fragments/command_retries.yaml b/changelogs/fragments/command_retries.yaml new file mode 100644 index 00000000..24febed4 --- /dev/null +++ b/changelogs/fragments/command_retries.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: + - vyos_command - Run commands at least once even when retries is set to 0 (https://github.com/ansible-collections/cisco.nxos/issues/607). diff --git a/docs/vyos.vyos.vyos_command_module.rst b/docs/vyos.vyos.vyos_command_module.rst index c6ce2cc4..042624b0 100644 --- a/docs/vyos.vyos.vyos_command_module.rst +++ b/docs/vyos.vyos.vyos_command_module.rst @@ -97,10 +97,11 @@ Parameters </div> </td> <td> - <b>Default:</b><br/><div style="color: blue">10</div> + <b>Default:</b><br/><div style="color: blue">9</div> </td> <td> <div>Specifies the number of retries a command should be tried before it is considered failed. The command is run on the target device every retry and evaluated against the <em>wait_for</em> conditionals.</div> + <div>The commands are run once when <em>retries</em> is set to <code>0</code>.</div> </td> </tr> <tr> diff --git a/plugins/modules/vyos_command.py b/plugins/modules/vyos_command.py index 94f16f3d..2ed920cb 100644 --- a/plugins/modules/vyos_command.py +++ b/plugins/modules/vyos_command.py @@ -74,7 +74,8 @@ options: - Specifies the number of retries a command should be tried before it is considered failed. The command is run on the target device every retry and evaluated against the I(wait_for) conditionals. - default: 10 + - The commands are run once when I(retries) is set to C(0). + default: 9 type: int interval: description: @@ -175,7 +176,7 @@ def main(): commands=dict(type="list", required=True, elements="raw"), wait_for=dict(type="list", aliases=["waitfor"], elements="str"), match=dict(default="all", choices=["all", "any"]), - retries=dict(default=10, type="int"), + retries=dict(default=9, type="int"), interval=dict(default=1, type="int"), ) @@ -186,6 +187,7 @@ def main(): commands = parse_commands(module, warnings) wait_for = module.params["wait_for"] or list() + conditionals = [] try: conditionals = [Conditional(c) for c in wait_for] except AttributeError as exc: @@ -195,7 +197,8 @@ def main(): interval = module.params["interval"] match = module.params["match"] - for item in range(retries): + # Always run at least once, and then `retries` more times. + for item in range(retries + 1): responses = run_commands(module, commands) for item in list(conditionals): diff --git a/tests/unit/modules/network/vyos/test_vyos_command.py b/tests/unit/modules/network/vyos/test_vyos_command.py index 7ed049be..e03ea7db 100644 --- a/tests/unit/modules/network/vyos/test_vyos_command.py +++ b/tests/unit/modules/network/vyos/test_vyos_command.py @@ -85,7 +85,13 @@ class TestVyosCommandModule(TestVyosModule): wait_for = 'result[0] contains "test string"' set_module_args(dict(commands=["show version"], wait_for=wait_for, retries=2)) self.execute_module(failed=True) - self.assertEqual(self.run_commands.call_count, 2) + self.assertEqual(self.run_commands.call_count, 3) + + def test_vyos_command_no_retries(self): + wait_for = 'result[0] contains "test string"' + set_module_args(dict(commands=["show version"], wait_for=wait_for, retries=0)) + self.execute_module(failed=True) + self.assertEqual(self.run_commands.call_count, 1) def test_vyos_command_match_any(self): wait_for = [ -- cgit v1.2.3