roll up of more fixes for ios_config multiline banners (#5524)

* now works for any banner in the config
* provides a configurable delimiter

link #5318
pull/18777/head
Peter Sprygada 8 years ago committed by Matt Clay
parent ddd13bf53a
commit 7d374689ad

@ -98,6 +98,15 @@ options:
required: false required: false
default: line default: line
choices: ['line', 'block'] choices: ['line', 'block']
multiline_delimiter:
description:
- This arugment is used when pushing a multiline configuration
element to the IOS device. It specifies the character to use
as the delimiting character. This only applies to the
configuration action
required: false
default: "@"
version_added: "2.2"
force: force:
description: description:
- The force argument instructs the module to not consider the - The force argument instructs the module to not consider the
@ -212,6 +221,10 @@ from ansible.module_utils.netcli import Command
def check_args(module, warnings): def check_args(module, warnings):
if module.params['multiline_delimiter']:
if len(module.params['multiline_delimiter']) != 1:
module.fail_json(msg='multiline_delimiter value can only be a '
'single character')
if module.params['force']: if module.params['force']:
warnings.append('The force argument is deprecated, please use ' warnings.append('The force argument is deprecated, please use '
'match=none instead. This argument will be ' 'match=none instead. This argument will be '
@ -219,12 +232,18 @@ def check_args(module, warnings):
def extract_banners(config): def extract_banners(config):
banners = {} banners = {}
for cmd in ['exec', 'login', 'incoming']: banner_cmds = re.findall(r'^banner (\w+)', config, re.M)
for cmd in banner_cmds:
regex = r'banner %s \^C(.+?)(?=\^C)' % cmd regex = r'banner %s \^C(.+?)(?=\^C)' % cmd
match = re.search(regex, config, re.S) match = re.search(regex, config, re.S)
if match: if match:
key = 'banner %s' % cmd key = 'banner %s' % cmd
banners[key] = match.group(1).strip() banners[key] = match.group(1).strip()
for cmd in banner_cmds:
regex = r'banner %s \^C(.+?)(?=\^C)' % cmd
match = re.search(regex, config, re.S)
if match:
config = config.replace(str(match.group(1)), '') config = config.replace(str(match.group(1)), '')
config = re.sub(r'banner \w+ \^C\^C', '!! banner removed', config) config = re.sub(r'banner \w+ \^C\^C', '!! banner removed', config)
@ -238,9 +257,10 @@ def diff_banners(want, have):
return candidate return candidate
def load_banners(module, banners): def load_banners(module, banners):
delimiter = module.params['multiline_delimiter']
for key, value in iteritems(banners): for key, value in iteritems(banners):
key += ' @' key += ' %s' % delimiter
for cmd in ['config terminal', key, value, '@', 'end']: for cmd in ['config terminal', key, value, delimiter, 'end']:
cmd += '\r' cmd += '\r'
module.connection.shell.shell.sendall(cmd) module.connection.shell.shell.sendall(cmd)
time.sleep(1) time.sleep(1)
@ -283,6 +303,7 @@ def run(module, result):
replace=replace) replace=replace)
else: else:
configobjs = candidate.items configobjs = candidate.items
have_banners = {}
banners = diff_banners(want_banners, have_banners) banners = diff_banners(want_banners, have_banners)
@ -302,7 +323,9 @@ def run(module, result):
# send the configuration commands to the device and merge # send the configuration commands to the device and merge
# them with the current running config # them with the current running config
if not module.check_mode: if not module.check_mode:
if commands:
module.config(commands) module.config(commands)
if banners:
load_banners(module, banners) load_banners(module, banners)
result['changed'] = True result['changed'] = True
@ -327,6 +350,7 @@ def main():
match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']),
replace=dict(default='line', choices=['line', 'block']), replace=dict(default='line', choices=['line', 'block']),
multiline_delimiter=dict(default='@'),
# this argument is deprecated in favor of setting match: none # this argument is deprecated in favor of setting match: none
# it will be removed in a future version # it will be removed in a future version

Loading…
Cancel
Save