From ef7d574920f2d4806bd966a99677dfc19796ad7a Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Tue, 20 Feb 2018 23:21:41 -0500 Subject: [PATCH] Fix nxos_banner module for unstructured output (#36411) * Fix nxos_banner module for unstructured output * Refactor and bug fixes * Fix pep8 error --- .../modules/network/nxos/nxos_banner.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/network/nxos/nxos_banner.py b/lib/ansible/modules/network/nxos/nxos_banner.py index cbf61122d99..ae4a7b04e8f 100644 --- a/lib/ansible/modules/network/nxos/nxos_banner.py +++ b/lib/ansible/modules/network/nxos/nxos_banner.py @@ -94,6 +94,23 @@ from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_arg import re +def execute_show_command(module, command): + format = 'json' + cmds = [{ + 'command': command, + 'output': format, + }] + output = run_commands(module, cmds, False) + if len(output) == 0 or len(output[0]) == 0: + # If we get here the platform does not + # support structured output. Resend as + # text. + cmds[0]['output'] = 'text' + output = run_commands(module, cmds, False) + + return output + + def map_obj_to_commands(want, have, module): commands = list() state = module.params['state'] @@ -111,7 +128,8 @@ def map_obj_to_commands(want, have, module): def map_config_to_obj(module): - output = run_commands(module, ['show banner %s' % module.params['banner']], False)[0] + command = 'show banner %s' % module.params['banner'] + output = execute_show_command(module, command)[0] if "Invalid command" in output: module.fail_json(msg="banner: exec may not be supported on this platform. Possible values are : exec | motd") @@ -128,6 +146,8 @@ def map_config_to_obj(module): output = output[0] else: output = '' + else: + output = output.rstrip() obj = {'banner': module.params['banner'], 'state': 'absent'} if output: