summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKate Case <kcase@redhat.com>2023-02-07 13:11:59 -0500
committerGitHub <noreply@github.com>2023-02-07 18:11:59 +0000
commitdac4b610c8a0dd67fad408649d855b8384017ddd (patch)
treef0ae8b94393a92227cf8210dc3146b9c30e4a541
parenta4a40e9d4ec540fae1b4ecb1622786d0832ed840 (diff)
downloadvyos-ansible-collection-dac4b610c8a0dd67fad408649d855b8384017ddd.tar.gz
vyos-ansible-collection-dac4b610c8a0dd67fad408649d855b8384017ddd.zip
[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>
-rw-r--r--changelogs/fragments/command_retries.yaml3
-rw-r--r--docs/vyos.vyos.vyos_command_module.rst3
-rw-r--r--plugins/modules/vyos_command.py9
-rw-r--r--tests/unit/modules/network/vyos/test_vyos_command.py8
4 files changed, 18 insertions, 5 deletions
diff --git a/changelogs/fragments/command_retries.yaml b/changelogs/fragments/command_retries.yaml
new file mode 100644
index 0000000..24febed
--- /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 c6ce2cc..042624b 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 94f16f3..2ed920c 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 7ed049b..e03ea7d 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 = [