allowing banner without stripping spaces (#62573)

Signed-off-by: rohitthakur2590 <rohitthakur2590@outlook.com>
This commit is contained in:
Rohit 2019-12-27 12:43:09 +05:30 committed by GitHub
parent da07b98b7a
commit 52f3ce8a80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 29 deletions

View file

@ -89,10 +89,9 @@ commands:
- string - string
""" """
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import exec_command from ansible.module_utils.network.ios.ios import get_config, load_config
from ansible.module_utils.network.ios.ios import load_config
from ansible.module_utils.network.ios.ios import ios_argument_spec 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): 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')): if want['text'] and (want['text'] != have.get('text')):
banner_cmd = 'banner %s' % module.params['banner'] banner_cmd = 'banner %s' % module.params['banner']
banner_cmd += ' @\n' banner_cmd += ' @\n'
banner_cmd += want['text'].strip() banner_cmd += want['text'].strip('\n')
banner_cmd += '\n@' banner_cmd += '\n@'
commands.append(banner_cmd) commands.append(banner_cmd)
@ -115,15 +114,19 @@ def map_obj_to_commands(updates, module):
def map_config_to_obj(module): def map_config_to_obj(module):
rc, out, err = exec_command(module, 'show banner %s' % module.params['banner']) """
if rc == 0: This function gets the banner config without stripping any whitespaces,
output = out and then fetches the required banner from it.
else: :param module:
rc, out, err = exec_command(module, :return: banner config dict object.
'show running-config | begin banner %s' """
% module.params['banner']) out = get_config(module, flags='| begin banner %s' % module.params['banner'])
if out: if out:
output = re.search(r'\^C(.*?)\^C', out, re.S).group(1).strip() 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: else:
output = None output = None
obj = {'banner': module.params['banner'], 'state': 'absent'} obj = {'banner': module.params['banner'], 'state': 'absent'}
@ -135,9 +138,6 @@ def map_config_to_obj(module):
def map_params_to_obj(module): def map_params_to_obj(module):
text = module.params['text'] text = module.params['text']
if text:
text = str(text).strip()
return { return {
'banner': module.params['banner'], 'banner': module.params['banner'],
'text': text, 'text': text,

View file

@ -30,20 +30,21 @@ class TestIosBannerModule(TestIosModule):
def setUp(self): def setUp(self):
super(TestIosBannerModule, self).setUp() super(TestIosBannerModule, self).setUp()
self.mock_exec_command = patch('ansible.modules.network.ios.ios_banner.exec_command') self.mock_get_config = patch('ansible.modules.network.ios.ios_banner.get_config')
self.exec_command = self.mock_exec_command.start() self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible.modules.network.ios.ios_banner.load_config') self.mock_load_config = patch('ansible.modules.network.ios.ios_banner.load_config')
self.load_config = self.mock_load_config.start() self.load_config = self.mock_load_config.start()
def tearDown(self): def tearDown(self):
super(TestIosBannerModule, self).tearDown() super(TestIosBannerModule, self).tearDown()
self.mock_exec_command.stop() self.mock_get_config.stop()
self.mock_load_config.stop() self.mock_load_config.stop()
def load_fixtures(self, commands=None): def load_fixtures(self, commands=None):
self.exec_command.return_value = (0, load_fixture('ios_banner_show_banner.txt').strip(), None) def load_from_file(*args, **kwargs):
self.load_config.return_value = dict(diff=None, session='session') return load_fixture('ios_banner_show_running_config_ios12.txt')
self.get_config.side_effect = load_from_file
def test_ios_banner_create(self): def test_ios_banner_create(self):
for banner_type in ('login', 'motd', 'exec', 'incoming', 'slip-ppp'): for banner_type in ('login', 'motd', 'exec', 'incoming', 'slip-ppp'):
@ -57,21 +58,19 @@ class TestIosBannerModule(TestIosModule):
self.execute_module(changed=True, commands=commands) self.execute_module(changed=True, commands=commands)
def test_ios_banner_nochange(self): 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)) set_module_args(dict(banner='login', text=banner_text))
self.execute_module() self.execute_module()
class TestIosBannerIos12Module(TestIosBannerModule): class TestIosBannerIos12Module(TestIosBannerModule):
def load_fixtures(self, commands): def load_fixtures(self, commands=None):
show_banner_return_value = (1, '', None) def load_from_file(*args, **kwargs):
show_running_config_return_value = \ return load_fixture('ios_banner_show_running_config_ios12.txt')
(0, load_fixture('ios_banner_show_running_config_ios12.txt').strip(), None) self.get_config.side_effect = load_from_file
self.exec_command.side_effect = [show_banner_return_value,
show_running_config_return_value]
def test_ios_banner_nochange(self): 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)) set_module_args(dict(banner='exec', text=banner_text))
self.execute_module() self.execute_module()