def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='path'), lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), config=dict(), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), ) mutually_exclusive = [('lines', 'src'), ('parents', 'src')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) result = {'changed': False} if module.params['backup']: result['__backup__'] = get_config(module) run(module, result) module.exit_json(**result)
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none': contents = module.params['config'] if not contents: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['lines']: if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['updates'] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: load_config(module, commands) result['changed'] = True if module.params['save']: if not module.check_mode: run_commands(module, 'configuration write') result['changed'] = True