Refactor iosxr_template to network_cli plugin

This commit is contained in:
Ricardo Carrillo Cruz 2017-01-16 18:18:18 +01:00
commit ae630fff56
4 changed files with 189 additions and 19 deletions

View file

@ -106,14 +106,23 @@ responses:
type: list
sample: ['...', '...']
"""
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.iosxr import NetworkModule
from ansible.module_utils.iosxr import get_config, load_config
from ansible.module_utils.network import NET_TRANSPORT_ARGS, _transitional_argument_spec
def check_args(module):
warnings = list()
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
return warnings
def get_config(module):
config = module.params['config'] or dict()
if not config and not module.params['force']:
config = module.config.get_config()
return config
def main():
""" main entry point for module execution
@ -126,33 +135,37 @@ def main():
config=dict(),
)
# Removed the use of provider arguments in 2.3 due to network_cli
# connection plugin. To be removed in 2.5
argument_spec.update(_transitional_argument_spec())
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
module = NetworkModule(argument_spec=argument_spec,
module = LocalAnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
result = dict(changed=False)
warnings = check_args(module)
result = dict(changed=False, warnings=warnings)
candidate = NetworkConfig(contents=module.params['src'], indent=1)
contents = get_config(module)
if contents:
config = NetworkConfig(contents=contents, indent=1)
result['_backup'] = str(contents)
if module.params['backup']:
result['__backup__'] = get_config(module)
if not module.params['force']:
commands = candidate.difference(config)
contents = get_config(module)
configobj = NetworkConfig(contents=contents, indent=1)
commands = candidate.difference(configobj)
commands = dumps(commands, 'commands').split('\n')
commands = [str(c) for c in commands if c]
commands = [str(c).strip() for c in commands if c]
else:
commands = str(candidate).split('\n')
commands = [c.strip() for c in str(candidate).split('\n')]
if commands:
if not module.check_mode:
response = module.config(commands)
result['responses'] = response
result['changed'] = True
load_config(module, commands, not module.check_mode)
result['changed'] = not module.check_mode
result['updates'] = commands
module.exit_json(**result)