added response_timestamps to ios_xr_command module (#50095)
This commit is contained in:
parent
daf1cfbde0
commit
2a0c356da9
4 changed files with 17 additions and 6 deletions
|
@ -477,10 +477,10 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
|
||||||
def run_commands(module, commands, check_rc=True):
|
def run_commands(module, commands, check_rc=True, return_timestamps=False):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
try:
|
try:
|
||||||
return connection.run_commands(commands=commands, check_rc=check_rc)
|
return connection.run_commands(commands=commands, check_rc=check_rc, return_timestamps=return_timestamps)
|
||||||
except ConnectionError as exc:
|
except ConnectionError as exc:
|
||||||
module.fail_json(msg=to_text(exc))
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
|
|
|
@ -178,7 +178,7 @@ def main():
|
||||||
match = module.params['match']
|
match = module.params['match']
|
||||||
|
|
||||||
while retries > 0:
|
while retries > 0:
|
||||||
responses = run_commands(module, commands)
|
responses, timestamps = run_commands(module, commands, return_timestamps=True)
|
||||||
|
|
||||||
for item in list(conditionals):
|
for item in list(conditionals):
|
||||||
if item(responses):
|
if item(responses):
|
||||||
|
@ -201,6 +201,7 @@ def main():
|
||||||
result.update({
|
result.update({
|
||||||
'stdout': responses,
|
'stdout': responses,
|
||||||
'stdout_lines': list(to_lines(responses)),
|
'stdout_lines': list(to_lines(responses)),
|
||||||
|
'timestamps': timestamps
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
|
@ -24,6 +24,7 @@ import json
|
||||||
|
|
||||||
from ansible.errors import AnsibleConnectionFailure
|
from ansible.errors import AnsibleConnectionFailure
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
|
from ansible.module_utils.basic import get_timestamp
|
||||||
from ansible.module_utils.common._collections_compat import Mapping
|
from ansible.module_utils.common._collections_compat import Mapping
|
||||||
from ansible.module_utils.connection import ConnectionError
|
from ansible.module_utils.connection import ConnectionError
|
||||||
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
||||||
|
@ -172,10 +173,11 @@ class Cliconf(CliconfBase):
|
||||||
|
|
||||||
self.send_command(**cmd_obj)
|
self.send_command(**cmd_obj)
|
||||||
|
|
||||||
def run_commands(self, commands=None, check_rc=True):
|
def run_commands(self, commands=None, check_rc=True, return_timestamps=False):
|
||||||
if commands is None:
|
if commands is None:
|
||||||
raise ValueError("'commands' value is required")
|
raise ValueError("'commands' value is required")
|
||||||
responses = list()
|
responses = list()
|
||||||
|
timestamps = list()
|
||||||
for cmd in to_list(commands):
|
for cmd in to_list(commands):
|
||||||
if not isinstance(cmd, Mapping):
|
if not isinstance(cmd, Mapping):
|
||||||
cmd = {'command': cmd}
|
cmd = {'command': cmd}
|
||||||
|
@ -185,6 +187,7 @@ class Cliconf(CliconfBase):
|
||||||
raise ValueError("'output' value %s is not supported for run_commands" % output)
|
raise ValueError("'output' value %s is not supported for run_commands" % output)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
timestamp = get_timestamp()
|
||||||
out = self.send_command(**cmd)
|
out = self.send_command(**cmd)
|
||||||
except AnsibleConnectionFailure as e:
|
except AnsibleConnectionFailure as e:
|
||||||
if check_rc:
|
if check_rc:
|
||||||
|
@ -203,7 +206,11 @@ class Cliconf(CliconfBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
responses.append(out)
|
responses.append(out)
|
||||||
return responses
|
timestamps.append(timestamp)
|
||||||
|
if return_timestamps:
|
||||||
|
return responses, timestamps
|
||||||
|
else:
|
||||||
|
return responses
|
||||||
|
|
||||||
def discard_changes(self):
|
def discard_changes(self):
|
||||||
self.send_command('abort')
|
self.send_command('abort')
|
||||||
|
|
|
@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
from units.compat.mock import patch
|
from units.compat.mock import patch
|
||||||
|
from ansible.module_utils.basic import get_timestamp
|
||||||
from ansible.modules.network.iosxr import iosxr_command
|
from ansible.modules.network.iosxr import iosxr_command
|
||||||
from units.modules.utils import set_module_args
|
from units.modules.utils import set_module_args
|
||||||
from .iosxr_module import TestIosxrModule, load_fixture
|
from .iosxr_module import TestIosxrModule, load_fixture
|
||||||
|
@ -45,6 +46,7 @@ class TestIosxrCommandModule(TestIosxrModule):
|
||||||
def load_from_file(*args, **kwargs):
|
def load_from_file(*args, **kwargs):
|
||||||
module, commands = args
|
module, commands = args
|
||||||
output = list()
|
output = list()
|
||||||
|
timestamps = list()
|
||||||
|
|
||||||
for item in commands:
|
for item in commands:
|
||||||
try:
|
try:
|
||||||
|
@ -53,7 +55,8 @@ class TestIosxrCommandModule(TestIosxrModule):
|
||||||
command = item
|
command = item
|
||||||
filename = str(command).replace(' ', '_')
|
filename = str(command).replace(' ', '_')
|
||||||
output.append(load_fixture(filename))
|
output.append(load_fixture(filename))
|
||||||
return output
|
timestamps.append(get_timestamp())
|
||||||
|
return output, timestamps
|
||||||
|
|
||||||
self.run_commands.side_effect = load_from_file
|
self.run_commands.side_effect = load_from_file
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue