1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

roll up of updates to iosxr_config module

* 'before' and 'after' arguments now only apply to 'lines'
* update doc strings
* remove update argument
* clean up warnings
This commit is contained in:
Peter Sprygada 2016-09-11 23:14:28 -04:00 committed by Matt Clay
parent 6a0b04d6a7
commit 8444b1ed8f

View file

@ -97,7 +97,7 @@ options:
line is not correct line is not correct
required: false required: false
default: line default: line
choices: ['line', 'block'] choices: ['line', 'block', 'config']
force: force:
description: description:
- The force argument instructs the module to not consider the - The force argument instructs the module to not consider the
@ -111,19 +111,6 @@ options:
default: false default: false
choices: [ "yes", "no" ] choices: [ "yes", "no" ]
version_added: "2.2" 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', 'replace', 'check']
version_added: "2.2"
config: config:
description: description:
- The module, by default, will connect to the remote device and - The module, by default, will connect to the remote device and
@ -191,7 +178,7 @@ 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
@ -206,25 +193,7 @@ from ansible.module_utils.iosxr import NetworkModule, NetworkError
DEFAULT_COMMIT_COMMENT = 'configured by iosxr_config' DEFAULT_COMMIT_COMMENT = 'configured by iosxr_config'
def invoke(name, *args, **kwargs):
func = globals().get(name)
if func:
return func(*args, **kwargs)
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['match'] == 'none' and module.params['replace']:
warnings.append('ignoring unnecessary argument replace')
if module.params['update'] == 'replace' and not module.params['src']:
module.fail_json(msg='Must specify src when update is `replace`')
if module.params['src'] and module.params['match'] not in ['line', 'none']:
module.fail_json(msg='match argument must be set to either `line` or '
'`none` when src argument is defined')
if module.params['src'] and module.params['replace'] != 'line':
module.fail_json(msg='replace argument must be set to `line` when '
'src argument is specified')
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 '
@ -232,7 +201,7 @@ def check_args(module, warnings):
def get_config(module, result): def get_config(module, result):
contents = module.params['config'] or result.get('__config__') contents = module.params['config']
if not contents: if not contents:
contents = module.config.get_config() contents = module.config.get_config()
return NetworkConfig(indent=1, contents=contents) return NetworkConfig(indent=1, contents=contents)
@ -247,75 +216,80 @@ def get_candidate(module):
return candidate return candidate
def load_config(module, commands, result): def load_config(module, commands, result):
replace = module.params['update'] == 'replace' replace = module.params['replace'] == 'config'
comment = module.params['comment'] comment = module.params['comment']
commit = not module.check_mode commit = not module.check_mode
diff = module.config.load_config(commands, replace=replace, commit=commit, diff = module.config.load_config(commands, replace=replace, commit=commit,
comment=comment) comment=comment)
result['diff'] = dict(prepared=diff)
result['changed'] = True if diff:
result['diff'] = dict(prepared=diff)
result['changed'] = True
def run(module, result): def run(module, result):
match = module.params['match'] match = module.params['match']
replace = module.params['replace'] replace = module.params['replace']
update = module.params['update']
path = module.params['parents'] path = module.params['parents']
candidate = get_candidate(module) candidate = get_candidate(module)
if match != 'none' and update != 'replace': if match != 'none' and replace != 'config':
config = get_config(module, result) config = get_config(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 result['updates'] = commands
if update != 'check': load_config(module, commands, result)
load_config(module, commands, result)
def main(): def main():
"""main entry point for module execution """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'),
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', 'config']),
update=dict(choices=['merge', 'replace', 'check'], default='merge'),
backup=dict(type='bool', default=False),
comment=dict(default=DEFAULT_COMMIT_COMMENT),
# 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
force=dict(default=False, type='bool'), force=dict(default=False, type='bool'),
config=dict(), config=dict(),
backup=dict(type='bool', default=False),
comment=dict(default=DEFAULT_COMMIT_COMMENT),
) )
mutually_exclusive = [('lines', 'src')] mutually_exclusive = [('lines', 'src')]
required_if = [('match', 'strict', ['lines']),
('match', 'exact', ['lines']),
('replace', 'block', ['lines']),
('replace', 'config', ['src'])]
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:
@ -327,9 +301,7 @@ def main():
result = dict(changed=False, warnings=warnings) result = dict(changed=False, warnings=warnings)
if module.params['backup']: if module.params['backup']:
config = module.config.get_config() result['__backup__'] = module.config.get_config()
result['__config__'] = config
result['__backup__'] = config
try: try:
run(module, result) run(module, result)