Refactor iosxr_config to make use of network_connection plugin

This commit is contained in:
Ricardo Carrillo Cruz 2017-01-23 10:44:13 +01:00
commit a8cfe4a5d7
5 changed files with 257 additions and 49 deletions

View file

@ -24,7 +24,7 @@ DOCUMENTATION = """
---
module: iosxr_config
version_added: "2.1"
author: "Peter Sprygada (@privateip)"
author: "Ricardo Carrillo Cruz (@rcarrillocruz)"
short_description: Manage Cisco IOS XR configuration sections
description:
- Cisco IOS XR configurations use a simple block indent file syntax
@ -148,19 +148,9 @@ options:
"""
EXAMPLES = """
# Note: examples below use the following provider dict to handle
# transport and authentication to the node.
vars:
cli:
host: "{{ inventory_hostname }}"
username: cisco
password: cisco
transport: cli
- name: configure top level configuration
iosxr_config:
lines: hostname {{ inventory_hostname }}
provider: "{{ cli }}"
- name: configure interface settings
iosxr_config:
@ -168,14 +158,12 @@ vars:
- description test interface
- ip address 172.31.1.1 255.255.255.0
parents: interface GigabitEthernet0/0/0/0
provider: "{{ cli }}"
- name: load a config from disk and replace the current config
iosxr_config:
src: config.cfg
update: replace
backup: yes
provider: "{{ cli }}"
"""
RETURN = """
@ -189,10 +177,26 @@ backup_path:
returned: when backup is yes
type: path
sample: /playbooks/ansible/backup/iosxr01.2016-07-16@22:28:34
start:
description: The time the job started
returned: always
type: str
sample: "2016-11-16 10:38:15.126146"
end:
description: The time the job ended
returned: always
type: str
sample: "2016-11-16 10:38:25.595612"
delta:
description: The time elapsed to perform all operations
returned: always
type: str
sample: "0:00:10.469466"
"""
from ansible.module_utils.basic import get_exception
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.iosxr import NetworkModule, NetworkError
from ansible.module_utils.iosxr import load_config,get_config
from ansible.module_utils.network import NET_TRANSPORT_ARGS, _transitional_argument_spec
DEFAULT_COMMIT_COMMENT = 'configured by iosxr_config'
@ -206,11 +210,18 @@ def check_args(module, warnings):
'match=none instead. This argument will be '
'removed in the future')
for key in NET_TRANSPORT_ARGS:
if module.params[key]:
warnings.append(
'network provider arguments are no longer supported. Please '
'use connection: network_cli for the task'
)
break
def get_config(module, result):
def get_running_config(module):
contents = module.params['config']
if not contents:
contents = module.config.get_config()
contents = get_config(module)
return NetworkConfig(indent=1, contents=contents)
def get_candidate(module):
@ -222,36 +233,28 @@ def get_candidate(module):
candidate.add(module.params['lines'], parents=parents)
return candidate
def load_config(module, commands, result):
replace = module.params['replace'] == 'config'
comment = module.params['comment']
commit = not module.check_mode
diff = module.config.load_config(commands, replace=replace, commit=commit,
comment=comment)
if diff:
result['diff'] = dict(prepared=diff)
result['changed'] = True
def run(module, result):
match = module.params['match']
replace = module.params['replace']
replace_config = replace == 'config'
path = module.params['parents']
comment = module.params['comment']
check_mode = module.check_mode
candidate = get_candidate(module)
if match != 'none' and replace != 'config':
config = get_config(module, result)
configobjs = candidate.difference(config, path=path, match=match,
contents = get_running_config(module)
configobj = NetworkConfig(contents=contents, indent=1)
commands = candidate.difference(configobj, path=path, match=match,
replace=replace)
else:
configobjs = candidate.items
commands = candidate.items
if configobjs:
commands = dumps(configobjs, 'commands').split('\n')
if commands:
commands = dumps(commands, 'commands').split('\n')
if module.params['lines']:
if any((module.params['lines'], module.params['src'])):
if module.params['before']:
commands[:0] = module.params['before']
@ -260,7 +263,11 @@ def run(module, result):
result['updates'] = commands
load_config(module, commands, result)
diff = load_config(module, commands, not check_mode,
replace_config, comment)
if diff:
result['diff'] = dict(prepared=diff)
result['changed'] = True
def main():
"""main entry point for module execution
@ -286,6 +293,8 @@ def main():
comment=dict(default=DEFAULT_COMMIT_COMMENT),
)
argument_spec.update(_transitional_argument_spec())
mutually_exclusive = [('lines', 'src')]
required_if = [('match', 'strict', ['lines']),
@ -293,11 +302,10 @@ def main():
('replace', 'block', ['lines']),
('replace', 'config', ['src'])]
module = NetworkModule(argument_spec=argument_spec,
connect_on_load=False,
mutually_exclusive=mutually_exclusive,
required_if=required_if,
supports_check_mode=True)
module = LocalAnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
required_if=required_if,
supports_check_mode=True)
if module.params['force'] is True:
module.params['match'] = 'none'
@ -308,13 +316,9 @@ def main():
result = dict(changed=False, warnings=warnings)
if module.params['backup']:
result['__backup__'] = module.config.get_config()
result['__backup__'] = get_config(module)
try:
run(module, result)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs)
run(module, result)
module.exit_json(**result)