roll up of updates to ios_config module

* 'before' and 'after' are now only applied to 'lines'
* remove update argument
* update doc strings
* add path argument when performing config difference
This commit is contained in:
Peter Sprygada 2016-09-11 19:42:10 -04:00
parent 0c05f0dfa4
commit 9b5e6bbfa1

View file

@ -109,20 +109,7 @@ options:
will be removed in a future release. will be removed in a future release.
required: false required: false
default: false default: false
choices: [ "true", "false" ] choices: ["true", "false"]
version_added: "2.2"
update:
description:
- The I(update) argument controls how the configuration statements
are processed on the remote device. Valid choices for the I(update)
argument are I(merge) and I(check). When the argument is set to
I(merge), the configuration changes are merged with the current
device running configuration. When the argument is set to I(check)
the configuration updates are determined but not actually configured
on the remote device.
required: false
default: merge
choices: ['merge', 'check']
version_added: "2.2" version_added: "2.2"
commit: commit:
description: description:
@ -218,18 +205,13 @@ RETURN = """
updates: updates:
description: The set of commands that will be pushed to the remote device description: The set of commands that will be pushed to the remote device
returned: always returned: always
type: list type: when lines is defined
sample: ['...', '...'] sample: ['...', '...']
backup_path: backup_path:
description: The full path to the backup file description: The full path to the backup file
returned: when backup is yes returned: when backup is yes
type: path type: path
sample: /playbooks/ansible/backup/ios_config.2016-07-16@22:28:34 sample: /playbooks/ansible/backup/ios_config.2016-07-16@22:28:34
responses:
description: The set of responses from issuing the commands on the device
returned: when not check_mode
type: list
sample: ['...', '...']
""" """
import re import re
@ -239,27 +221,16 @@ from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.netcli import Command from ansible.module_utils.netcli import Command
def check_args(module, warnings): def check_args(module, warnings):
if module.params['parents']:
if not module.params['lines'] or module.params['src']:
warnings.append('ignoring unnecessary argument parents')
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 '
'removed in the future') 'removed in the future')
def get_config(module, result): def get_config(module, result):
defaults = module.params['default'] contents = module.params['config']
if defaults is True:
key = '__configall__'
else:
key = '__config__'
contents = module.params['config'] or result.get(key)
if not contents: if not contents:
defaults = module.params['default']
contents = module.config.get_config(include_defaults=defaults) contents = module.config.get_config(include_defaults=defaults)
result[key] = contents
return NetworkConfig(indent=1, contents=contents) return NetworkConfig(indent=1, contents=contents)
def get_candidate(module): def get_candidate(module):
@ -275,19 +246,13 @@ def load_backup(module):
try: try:
module.cli(['exit', 'config replace flash:/ansible-rollback force']) module.cli(['exit', 'config replace flash:/ansible-rollback force'])
except NetworkError: except NetworkError:
module.fail_json(msg='unable to rollback configuration') module.fail_json(msg='unable to load backup configuration')
def backup_config(module): def backup_config(module):
cmd = 'copy running-config flash:/ansible-rollback' cmd = 'copy running-config flash:/ansible-rollback'
cmd = Command(cmd, prompt=re.compile('\? $'), response='\n') cmd = Command(cmd, prompt=re.compile('\? $'), response='\n')
module.cli(cmd) module.cli(cmd)
def load_config(module, commands, result):
if not module.check_mode and module.params['update'] != 'check':
module.config(commands)
result['changed'] = module.params['update'] != 'check'
result['updates'] = commands
def run(module, result): def run(module, result):
match = module.params['match'] match = module.params['match']
replace = module.params['replace'] replace = module.params['replace']
@ -301,17 +266,19 @@ def run(module, result):
configobjs = candidate.difference(config, path=path,match=match, configobjs = candidate.difference(config, path=path,match=match,
replace=replace) replace=replace)
else: else:
config = None
configobjs = candidate.items configobjs = candidate.items
if configobjs: if configobjs:
commands = dumps(configobjs, 'commands').split('\n') commands = dumps(configobjs, 'commands').split('\n')
if module.params['before']: if module.params['lines']:
commands[:0] = module.params['before'] if module.params['before']:
commands[:0] = module.params['before']
if module.params['after']: if module.params['after']:
commands.extend(module.params['after']) commands.extend(module.params['after'])
result['updates'] = commands
# create a backup copy of the current running-config on # create a backup copy of the current running-config on
# device flash drive # device flash drive
@ -319,23 +286,29 @@ 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
load_config(module, commands, result) if not module.check_mode:
module.config(commands)
result['changed'] = True
# remove the backup copy of the running-config since its # remove the backup copy of the running-config since its
# no longer needed # no longer needed
module.cli('delete /force flash:/ansible-rollback') module.cli('delete /force flash:/ansible-rollback')
if module.params['save'] and not module.check_mode: if module.params['save']:
module.config.save_config() if not module.check_mode:
module.config.save_config()
result['changed'] = True
def main(): def main():
""" main entry point for module execution
"""
argument_spec = dict( argument_spec = dict(
src=dict(type='path'),
lines=dict(aliases=['commands'], type='list'), lines=dict(aliases=['commands'], type='list'),
parents=dict(type='list'), parents=dict(type='list'),
src=dict(type='path'),
before=dict(type='list'), before=dict(type='list'),
after=dict(type='list'), after=dict(type='list'),
@ -346,20 +319,23 @@ def main():
# it will be removed in a future version # it will be removed in a future version
force=dict(default=False, type='bool'), force=dict(default=False, type='bool'),
update=dict(choices=['merge', 'check'], default='merge'),
backup=dict(type='bool', default=False),
config=dict(), config=dict(),
default=dict(type='bool', default=False), default=dict(type='bool', default=False),
save=dict(type='bool', default=False), save=dict(type='bool', default=False),
backup=dict(type='bool', default=False),
) )
mutually_exclusive = [('lines', 'src')] mutually_exclusive = [('lines', 'src')]
required_if = [('match', 'strict', ['lines']),
('match', 'exact', ['lines']),
('replace', 'block', ['lines'])]
module = NetworkModule(argument_spec=argument_spec, module = NetworkModule(argument_spec=argument_spec,
connect_on_load=False, connect_on_load=False,
mutually_exclusive=mutually_exclusive, mutually_exclusive=mutually_exclusive,
required_if=required_if,
supports_check_mode=True) supports_check_mode=True)
if module.params['force'] is True: if module.params['force'] is True: