From 98c98125f06f6903bf8f12ab76b5794c72a1f37c Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Wed, 22 Mar 2017 10:01:41 +0100 Subject: [PATCH] Fallback to show-run on ios_banner for devices where show banner does not work (#22793) On switches 'show banner' command doesn't work, fallback to show run|begin banner and extract the banner text in case that fails. --- lib/ansible/modules/network/ios/ios_banner.py | 13 +++++++++++-- test/units/modules/network/ios/test_ios_banner.py | 9 +++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/network/ios/ios_banner.py b/lib/ansible/modules/network/ios/ios_banner.py index 4d32fe3215..6187a6f4a7 100644 --- a/lib/ansible/modules/network/ios/ios_banner.py +++ b/lib/ansible/modules/network/ios/ios_banner.py @@ -82,8 +82,10 @@ commands: - string """ from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.connection import exec_command from ansible.module_utils.ios import load_config, run_commands from ansible.module_utils.ios import ios_argument_spec, check_args +import re def map_obj_to_commands(updates, module): commands = list() @@ -104,10 +106,17 @@ def map_obj_to_commands(updates, module): return commands def map_config_to_obj(module): - output = run_commands(module, ['show banner %s' % module.params['banner']]) + rc, out, err = exec_command(module, 'show banner %s' % module.params['banner']) + if rc == 0: + output = out + else: + rc, out, err = exec_command(module, + 'show running-config | begin banner %s' + % module.params['banner']) + output = re.search('\^C(.*)\^C', out, re.S).group(1).strip() obj = {'banner': module.params['banner'], 'state': 'absent'} if output: - obj['text'] = output[0] + obj['text'] = output obj['state'] = 'present' return obj diff --git a/test/units/modules/network/ios/test_ios_banner.py b/test/units/modules/network/ios/test_ios_banner.py index 46fd9aca53..72876fd693 100644 --- a/test/units/modules/network/ios/test_ios_banner.py +++ b/test/units/modules/network/ios/test_ios_banner.py @@ -29,18 +29,19 @@ class TestIosBannerModule(TestIosModule): module = ios_banner def setUp(self): - self.mock_run_commands = patch('ansible.modules.network.ios.ios_banner.run_commands') - self.run_commands = self.mock_run_commands.start() + self.mock_exec_command = patch('ansible.modules.network.ios.ios_banner.exec_command') + self.exec_command = self.mock_exec_command.start() self.mock_load_config = patch('ansible.modules.network.ios.ios_banner.load_config') self.load_config = self.mock_load_config.start() def tearDown(self): - self.mock_run_commands.stop() + self.mock_exec_command.stop() self.mock_load_config.stop() def load_fixtures(self, commands=None): - self.run_commands.return_value = [load_fixture('ios_banner_show_banner.txt').strip()] + self.exec_command.return_value = (0, + load_fixture('ios_banner_show_banner.txt').strip(), None) self.load_config.return_value = dict(diff=None, session='session') def test_ios_banner_create(self):