def load(module, commands, result): candidate = NetworkConfig(indent=2, contents='\n'.join(commands)) config = get_config(module) configobjs = candidate.difference(config) if configobjs: commands = dumps(configobjs, 'commands').split('\n') result['updates'] = commands if not module.check_mode: load_config(module, commands, result) result['changed'] = True
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(required=True), force=dict(default=False, type='bool'), include_defaults=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), replace=dict(default=False, type='bool'), config=dict() ) argument_spec.update(eapi.eapi_argument_spec) mutually_exclusive = [('config', 'backup'), ('config', 'force')] cls = get_ansible_module() module = cls(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = check_args(module) result = {'changed': False} if warnings: result['warnings'] = warnings src = module.params['src'] candidate = NetworkConfig(contents=src, indent=3) if module.params['backup']: result['__backup__'] = get_config() if not module.params['force']: contents = get_current_config(module) configobj = NetworkConfig(contents=contents, indent=3) commands = candidate.difference(configobj) commands = dumps(commands, 'commands').split('\n') commands = [str(c).strip() for c in commands if c] else: commands = [c.strip() for c in str(candidate).split('\n')] # FIXME not implemented yet!! if replace: if module.params['transport'] == 'cli': module.fail_json(msg='config replace is only supported over eapi') commands = str(candidate).split('\n') if commands: commands = filter_exit(commands) commit = not module.check_mode load_config(commands, commit=commit) result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict(src=dict(required=True), force=dict(default=False, type='bool'), include_defaults=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), replace=dict(default=False, type='bool'), config=dict()) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) replace = module.params['replace'] commands = list() running = None result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=3) if replace: if module.params['transport'] == 'cli': module.fail_json(msg='config replace is only supported over eapi') commands = str(candidate).split('\n') else: contents = get_config(module) if contents: running = NetworkConfig(contents=contents, indent=3) result['_backup'] = contents if not module.params['force']: commands = candidate.difference((running or list())) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') commands = filter_exit(commands) if commands: if not module.check_mode: response = module.config.load_config(commands, replace=replace, session='eos_template', commit=True) module.cli('no configure session eos_template') result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(required=True), force=dict(default=False, type='bool'), include_defaults=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), replace=dict(default=False, type='bool'), config=dict() ) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) replace = module.params['replace'] commands = list() running = None result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=3) if replace: if module.params['transport'] == 'cli': module.fail_json(msg='config replace is only supported over eapi') commands = str(candidate).split('\n') else: contents = get_config(module) if contents: running = NetworkConfig(contents=contents, indent=3) result['_backup'] = contents if not module.params['force']: commands = candidate.difference((running or list())) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') commands = filter_exit(commands) if commands: if not module.check_mode: response = module.config.load_config(commands, replace=replace, commit=True) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict(src=dict(required=True), force=dict(default=False, type='bool'), include_defaults=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), replace=dict(default=False, type='bool'), config=dict()) argument_spec.update(eos_argument_spec) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings src = module.params['src'] candidate = NetworkConfig(contents=src, indent=3) if module.params['backup']: result['__backup__'] = get_config(module) if not module.params['force']: contents = get_current_config(module) configobj = NetworkConfig(contents=contents, indent=3) commands = candidate.difference(configobj) commands = dumps(commands, 'commands').split('\n') commands = [str(c).strip() for c in commands if c] else: commands = [c.strip() for c in str(candidate).split('\n')] #commands = str(candidate).split('\n') if commands: commands = filter_exit(commands) commit = not module.check_mode replace = module.params['replace'] or False load_config(module, commands, commit=commit, replace=replace) result['changed'] = True result['commands'] = commands result['updates'] = commands module.exit_json(**result)
def load(module, instance, commands, result): candidate = NetworkConfig(indent=3) candidate.add(commands, parents=['management api http-commands']) config = get_config(module) configobjs = candidate.difference(config) if configobjs: commands = dumps(configobjs, 'commands').split('\n') result['updates'] = commands load_config(module, instance, commands, result)
def main(): argument_spec = dict(lines=dict(aliases=['commands'], required=True, type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact']), replace=dict(default='line', choices=['line', 'block']), force=dict(default=False, type='bool'), config=dict()) argument_spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) lines = module.params['lines'] result = {'changed': False} candidate = NetworkConfig(indent=1) candidate.add(lines) acl_name = parse_acl_name(module) if not module.params['force']: contents = get_acl_config(module, acl_name) config = NetworkConfig(indent=1, contents=contents) commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) if not module.check_mode: load_config(module, commands) result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): argument_spec = dict( lines=dict(aliases=['commands'], required=True, type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact']), replace=dict(default='line', choices=['line', 'block']), force=dict(default=False, type='bool'), config=dict() ) module = NetworkModule(argument_spec=argument_spec, supports_check_mode=True) lines = module.params['lines'] before = module.params['before'] after = module.params['after'] match = module.params['match'] replace = module.params['replace'] result = dict(changed=False) candidate = NetworkConfig(indent=1) candidate.add(lines) acl_name = parse_acl_name(module) if not module.params['force']: contents = get_config(module, acl_name) config = NetworkConfig(indent=1, contents=contents) commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if not module.check_mode: response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(), force=dict(default=False, type='bool'), include_defaults=dict(default=True, type='bool'), backup=dict(default=False, type='bool'), config=dict(), ) argument_spec.update(ios_cli.ios_cli_argument_spec) mutually_exclusive = [('config', 'backup'), ('config', 'force')] cls = get_ansible_module() module = cls(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) candidate = NetworkConfig(contents=module.params['src'], indent=1) result = {'changed': False} if warnings: result['warnings'] = warnings if module.params['backup']: result['__backup__'] = get_config(module=module) if not module.params['force']: contents = get_current_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj) commands = dumps(commands, 'commands').split('\n') commands = [str(c).strip() for c in commands if c] else: commands = [c.strip() for c in str(candidate).split('\n')] if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(), force=dict(default=False, type='bool'), include_defaults=dict(default=True, type='bool'), backup=dict(default=False, type='bool'), 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 = LocalAnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = check_args(module) result = dict(changed=False, warnings=warnings) candidate = NetworkConfig(contents=module.params['src'], indent=1) result = {'changed': False} if module.params['backup']: result['__backup__'] = get_config() if not module.params['force']: contents = get_current_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj) commands = dumps(commands, 'commands').split('\n') commands = [str(c).strip() for c in commands if c] else: commands = [c.strip() for c in str(candidate).split('\n')] if commands: if not module.check_mode: load_config(commands) result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): argument_spec = dict(lines=dict(aliases=['commands'], required=True, type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact']), replace=dict(default='line', choices=['line', 'block']), force=dict(default=False, type='bool'), config=dict()) module = NetworkModule(argument_spec=argument_spec, supports_check_mode=True) lines = module.params['lines'] before = module.params['before'] after = module.params['after'] match = module.params['match'] replace = module.params['replace'] candidate = NetworkConfig(indent=1) candidate.add(lines) module.filter = check_input_acl(lines, module) if not module.params['force']: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') else: commands = str(candidate).split('\n') if commands: if not module.check_mode: commands = [str(c) for c in commands if c] response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def load_config(module, commands, result): candidate = NetworkConfig(device_os='sros', contents='\n'.join(commands)) config = get_config(module) configobjs = candidate.difference(config) if configobjs: commands = dumps(configobjs, 'lines') commands = sanitize_config(commands.split('\n')) result['updates'] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: module.config(commands) result['changed'] = True
def main(): """ main entry point for module execution """ argument_spec = dict( rollback_location=dict(), local_max_checkpoints=dict(type='int'), remote_max_checkpoints=dict(type='int'), rescue_location=dict(), state=dict(default='present', choices=['present', 'absent']) ) argument_spec.update(sros_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) state = module.params['state'] result = dict(changed=False) commands = list() invoke(state, module, commands) candidate = NetworkConfig(indent=4, contents='\n'.join(commands)) config = get_device_config(module) configobjs = candidate.difference(config) if configobjs: #commands = dumps(configobjs, 'lines') commands = dumps(configobjs, 'commands') commands = sanitize_config(commands.split('\n')) result['updates'] = commands result['commands'] = 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 module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(), force=dict(default=False, type='bool'), include_defaults=dict(default=True, type='bool'), backup=dict(default=False, type='bool'), config=dict(), ) argument_spec.update(nxos_argument_spec) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=2) contents = get_current_config(module) if contents: config = NetworkConfig(contents=contents, indent=2) result['__backup__'] = str(contents) if not module.params['force']: commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['updates'] = commands result['commands'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict(rollback_location=dict(), local_max_checkpoints=dict(type='int'), remote_max_checkpoints=dict(type='int'), rescue_location=dict(), state=dict(default='present', choices=['present', 'absent'])) argument_spec.update(sros_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) state = module.params['state'] result = dict(changed=False) commands = list() invoke(state, module, commands) candidate = NetworkConfig(indent=4, contents='\n'.join(commands)) config = get_device_config(module) configobjs = candidate.difference(config) if configobjs: #commands = dumps(configobjs, 'lines') commands = dumps(configobjs, 'commands') commands = sanitize_config(commands.split('\n')) result['updates'] = commands result['commands'] = 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 module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(), force=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), config=dict(), ) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=1) contents = get_config(module) if contents: config = NetworkConfig(contents=contents[0], indent=1) result['_backup'] = contents[0] commands = list() if not module.params['force']: commands = dumps(candidate.difference(config), 'commands') else: commands = str(candidate) if commands: commands = commands.split('\n') if not module.check_mode: response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(), force=dict(default=False, type='bool'), include_defaults=dict(default=True, type='bool'), backup=dict(default=False, type='bool'), config=dict(), ) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=2) contents = get_config(module) if contents: config = NetworkConfig(contents=contents, indent=2) result['_backup'] = str(contents) if not module.params['force']: commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if not module.check_mode: response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='str'), force=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), config=dict(type='dict'), ) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) if not module.params['transport'] and not HAS_OPS: module.fail_json(msg='unable to import ops.dc library') result = dict(changed=False) contents = get_config(module) result['_backup'] = contents if module.params['transport'] in ['ssh', 'rest']: config = contents try: src = module.from_json(module.params['src']) except ValueError: module.fail_json(msg='unable to load src due to json parsing error') changeset = diff(src, config) candidate = merge(changeset, config) updates = dict() for path, key, new_value, old_value in changeset: path = '%s.%s' % ('.'.join(path), key) updates[path] = str(new_value) result['updates'] = updates if changeset: if not module.check_mode: module.config(config) result['changed'] = True else: candidate = NetworkConfig(contents=module.params['src'], indent=4) if contents: config = NetworkConfig(contents=contents, indent=4) if not module.params['force']: commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if not module.check_mode: response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='str'), force=dict(default=False, type='bool'), backup=dict(default=False, type='bool'), config=dict(type='dict'), ) mutually_exclusive = [('config', 'backup'), ('config', 'force')] module = NetworkModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) if not module.params['transport'] and not HAS_OPS: module.fail_json(msg='unable to import ops.dc library') result = dict(changed=False) contents = get_config(module) result['_backup'] = contents if module.params['transport'] in ['ssh', 'rest']: config = contents try: src = module.from_json(module.params['src']) except ValueError: module.fail_json( msg='unable to load src due to json parsing error') changeset = diff(src, config) candidate = merge(changeset, config) updates = dict() for path, key, new_value, old_value in changeset: path = '%s.%s' % ('.'.join(path), key) updates[path] = str(new_value) result['updates'] = updates if changeset: if not module.check_mode: module.config(config) result['changed'] = True else: candidate = NetworkConfig(contents=module.params['src'], indent=4) if contents: config = NetworkConfig(contents=contents, indent=4) if not module.params['force']: commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if not module.check_mode: response = module.config(commands) result['responses'] = response result['changed'] = True result['updates'] = commands module.exit_json(**result)