From 52f3ce8a808f943561803bd664e695fed1841fe8 Mon Sep 17 00:00:00 2001 From: Rohit Date: Fri, 27 Dec 2019 12:43:09 +0530 Subject: [PATCH] allowing banner without stripping spaces (#62573) Signed-off-by: rohitthakur2590 --- lib/ansible/modules/network/ios/ios_banner.py | 32 +++++++++---------- .../modules/network/ios/test_ios_banner.py | 25 +++++++-------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/ansible/modules/network/ios/ios_banner.py b/lib/ansible/modules/network/ios/ios_banner.py index be85781058..c28838407c 100644 --- a/lib/ansible/modules/network/ios/ios_banner.py +++ b/lib/ansible/modules/network/ios/ios_banner.py @@ -89,10 +89,9 @@ commands: - string """ from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.connection import exec_command -from ansible.module_utils.network.ios.ios import load_config +from ansible.module_utils.network.ios.ios import get_config, load_config from ansible.module_utils.network.ios.ios import ios_argument_spec -import re +from re import search, M def map_obj_to_commands(updates, module): @@ -107,7 +106,7 @@ def map_obj_to_commands(updates, module): if want['text'] and (want['text'] != have.get('text')): banner_cmd = 'banner %s' % module.params['banner'] banner_cmd += ' @\n' - banner_cmd += want['text'].strip() + banner_cmd += want['text'].strip('\n') banner_cmd += '\n@' commands.append(banner_cmd) @@ -115,17 +114,21 @@ def map_obj_to_commands(updates, module): def map_config_to_obj(module): - 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']) - if out: - output = re.search(r'\^C(.*?)\^C', out, re.S).group(1).strip() + """ + This function gets the banner config without stripping any whitespaces, + and then fetches the required banner from it. + :param module: + :return: banner config dict object. + """ + out = get_config(module, flags='| begin banner %s' % module.params['banner']) + if out: + regex = 'banner ' + module.params['banner'] + ' ^C\n' + if search('banner ' + module.params['banner'], out, M): + output = str((out.split(regex))[1].split("^C\n")[0]) else: output = None + else: + output = None obj = {'banner': module.params['banner'], 'state': 'absent'} if output: obj['text'] = output @@ -135,9 +138,6 @@ def map_config_to_obj(module): def map_params_to_obj(module): text = module.params['text'] - if text: - text = str(text).strip() - return { 'banner': module.params['banner'], 'text': text, diff --git a/test/units/modules/network/ios/test_ios_banner.py b/test/units/modules/network/ios/test_ios_banner.py index 4e7106e966..cdd43d8e79 100644 --- a/test/units/modules/network/ios/test_ios_banner.py +++ b/test/units/modules/network/ios/test_ios_banner.py @@ -30,20 +30,21 @@ class TestIosBannerModule(TestIosModule): def setUp(self): super(TestIosBannerModule, self).setUp() - self.mock_exec_command = patch('ansible.modules.network.ios.ios_banner.exec_command') - self.exec_command = self.mock_exec_command.start() + self.mock_get_config = patch('ansible.modules.network.ios.ios_banner.get_config') + self.get_config = self.mock_get_config.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): super(TestIosBannerModule, self).tearDown() - self.mock_exec_command.stop() + self.mock_get_config.stop() self.mock_load_config.stop() def load_fixtures(self, commands=None): - 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 load_from_file(*args, **kwargs): + return load_fixture('ios_banner_show_running_config_ios12.txt') + self.get_config.side_effect = load_from_file def test_ios_banner_create(self): for banner_type in ('login', 'motd', 'exec', 'incoming', 'slip-ppp'): @@ -57,21 +58,19 @@ class TestIosBannerModule(TestIosModule): self.execute_module(changed=True, commands=commands) def test_ios_banner_nochange(self): - banner_text = load_fixture('ios_banner_show_banner.txt').strip() + banner_text = load_fixture('ios_banner_show_banner.txt') set_module_args(dict(banner='login', text=banner_text)) self.execute_module() class TestIosBannerIos12Module(TestIosBannerModule): - def load_fixtures(self, commands): - show_banner_return_value = (1, '', None) - show_running_config_return_value = \ - (0, load_fixture('ios_banner_show_running_config_ios12.txt').strip(), None) - self.exec_command.side_effect = [show_banner_return_value, - show_running_config_return_value] + def load_fixtures(self, commands=None): + def load_from_file(*args, **kwargs): + return load_fixture('ios_banner_show_running_config_ios12.txt') + self.get_config.side_effect = load_from_file def test_ios_banner_nochange(self): - banner_text = load_fixture('ios_banner_show_banner.txt').strip() + banner_text = load_fixture('ios_banner_show_banner.txt') set_module_args(dict(banner='exec', text=banner_text)) self.execute_module()