From 2a432a093b392e3f8babea6b760df6a5c8716b03 Mon Sep 17 00:00:00 2001 From: vaneuk <31311894+vaneuk@users.noreply.github.com> Date: Thu, 31 Jan 2019 15:41:53 +0300 Subject: [PATCH] added timestamps to ios_command module (#50323) ios_command module now returns timestamps field, which shows command execution time --- lib/ansible/module_utils/network/ios/ios.py | 4 ++-- lib/ansible/modules/network/ios/ios_command.py | 3 ++- lib/ansible/plugins/cliconf/ios.py | 11 +++++++++-- test/units/modules/network/ios/test_ios_command.py | 5 ++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_utils/network/ios/ios.py b/lib/ansible/module_utils/network/ios/ios.py index 7aeae8e313a..779138b5e02 100644 --- a/lib/ansible/module_utils/network/ios/ios.py +++ b/lib/ansible/module_utils/network/ios/ios.py @@ -129,10 +129,10 @@ def get_config(module, flags=None): return cfg -def run_commands(module, commands, check_rc=True): +def run_commands(module, commands, check_rc=True, return_timestamps=False): connection = get_connection(module) 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: module.fail_json(msg=to_text(exc)) diff --git a/lib/ansible/modules/network/ios/ios_command.py b/lib/ansible/modules/network/ios/ios_command.py index cd7c34ab627..837211e8d1b 100644 --- a/lib/ansible/modules/network/ios/ios_command.py +++ b/lib/ansible/modules/network/ios/ios_command.py @@ -198,7 +198,7 @@ def main(): match = module.params['match'] while retries > 0: - responses = run_commands(module, commands) + responses, timestamps = run_commands(module, commands, return_timestamps=True) for item in list(conditionals): if item(responses): @@ -221,6 +221,7 @@ def main(): result.update({ 'stdout': responses, 'stdout_lines': list(to_lines(responses)), + 'timestamps': timestamps }) module.exit_json(**result) diff --git a/lib/ansible/plugins/cliconf/ios.py b/lib/ansible/plugins/cliconf/ios.py index d0855a1a226..a90b829c67b 100644 --- a/lib/ansible/plugins/cliconf/ios.py +++ b/lib/ansible/plugins/cliconf/ios.py @@ -27,6 +27,7 @@ from itertools import chain from ansible.errors import AnsibleConnectionFailure 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.six import iteritems from ansible.module_utils.network.common.config import NetworkConfig, dumps @@ -271,11 +272,12 @@ class Cliconf(CliconfBase): return resp - 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: raise ValueError("'commands' value is required") responses = list() + timestamps = list() for cmd in to_list(commands): if not isinstance(cmd, Mapping): cmd = {'command': cmd} @@ -286,14 +288,19 @@ class Cliconf(CliconfBase): try: out = self.send_command(**cmd) + timestamp = get_timestamp() except AnsibleConnectionFailure as e: if check_rc: raise out = getattr(e, 'err', to_text(e)) responses.append(out) + timestamps.append(timestamp) - return responses + if return_timestamps: + return responses, timestamps + else: + return responses def get_defaults_flag(self): """ diff --git a/test/units/modules/network/ios/test_ios_command.py b/test/units/modules/network/ios/test_ios_command.py index 1dab227b9ac..127683a61b9 100644 --- a/test/units/modules/network/ios/test_ios_command.py +++ b/test/units/modules/network/ios/test_ios_command.py @@ -22,6 +22,7 @@ __metaclass__ = type import json from units.compat.mock import patch +from ansible.module_utils.basic import get_timestamp from ansible.modules.network.ios import ios_command from units.modules.utils import set_module_args from .ios_module import TestIosModule, load_fixture @@ -46,6 +47,7 @@ class TestIosCommandModule(TestIosModule): def load_from_file(*args, **kwargs): module, commands = args output = list() + timestamps = list() for item in commands: try: @@ -55,7 +57,8 @@ class TestIosCommandModule(TestIosModule): command = item['command'] filename = str(command).replace(' ', '_') output.append(load_fixture(filename)) - return output + timestamps.append(get_timestamp()) + return output, timestamps self.run_commands.side_effect = load_from_file