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 run(module, result): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none': config = get_running_config(module) path = module.params['parents'] configobjs = candidate.difference(config, match=match, replace=replace, path=path) 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['commands'] = commands result['updates'] = commands if not module.check_mode: load_config(module, commands) 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': contents = get_running_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj, path=path, match=match, replace=replace) else: commands = candidate.items if commands: commands = dumps(commands, 'commands').split('\n') if any((module.params['lines'], module.params['src'])): if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands diff = load_config(module, commands, not check_mode, replace_config, comment) if diff: result['diff'] = dict(prepared=diff) result['changed'] = True
def run(module, result): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none' and replace != 'config': config_text = get_config(module) config = NetworkConfig(indent=3, contents=config_text) configobjs = candidate.difference(config, 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['commands'] = commands replace = module.params['replace'] == 'config' commit = not module.check_mode response = load_config(module, commands, replace=replace, commit=commit) if 'diff' in response: result['diff'] = {'prepared': response['diff']} if 'session' in response: result['session'] = response['session'] result['changed'] = True
def run(module, result): match = module.params['match'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) configobjs = candidate.difference(config) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'lines') commands = sanitize_config(commands.split('\n')) result['updates'] = commands # check if creating checkpoints is possible if not module.connection.rollback_enabled: warn = 'Cannot create checkpoint. Please enable this feature ' \ 'using the sros_rollback module. Automatic rollback ' \ 'will be disabled' result['warnings'].append(warn) # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: module.config.load_config(commands) result['changed'] = True if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
def run(module, result): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none' and replace != 'config': config = get_config(module) configobjs = candidate.difference(config, 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 module.log('commands: %s' % commands) load_config(module, commands, result) if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none' and replace != 'config': config = get_config(module, result) 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 load_config(module, commands, result)
def run(module, result): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) configobjs = candidate.difference(config, match=match, replace=replace) else: config = None configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) # send the configuration commands to the device and merge # them with the current running config load_config(module, commands, result) if module.params['save'] and not module.check_mode: module.config.save_config()
def run(module, result): match = module.params['match'] replace = module.params['replace'] update = module.params['update'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none' and update != 'replace': config = get_config(module, result) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: config = None configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['updates'] = commands if update != 'check': load_config(module, commands, 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(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 get_section(self, path): try: section = self.get_section_objects(path) if self._device_os == 'junos': return dumps(section, output='lines') return self.to_block(section) except ValueError: return list()
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 run(module, result): match = module.params['match'] replace = module.params['replace'] update = module.params['update'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) path = module.params['parents'] configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: config = None configobjs = candidate.items if module.params['backup']: backup_config(module, result) if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['updates'] = commands # create a checkpoint of the current running config in case # there is a problem loading the candidate config checkpoint = 'ansible_%s' % int(time.time()) module.cli(['checkpoint %s' % checkpoint], output='text') result['__checkpoint__'] = checkpoint module.log('create checkpoint %s' % checkpoint) # if the update mode is set to check just return # and do not try to load into the system if update != 'check': load_config(module, commands, result) # remove the checkpoint file used to restore the config # in case of an error if not module.check_mode: module.log('remove checkpoint %s' % checkpoint) module.cli('no checkpoint %s' % checkpoint, output='text') if module.params['save'] and not module.check_mode: module.config.save_config() result['changed'] = True
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 run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate, want_banners = get_candidate(module) if match != 'none': config, have_banners = get_config(module, result) path = module.params['parents'] configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items have_banners = {} banners = diff_banners(want_banners, have_banners) if configobjs or banners: 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 result['banners'] = banners # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: if commands: module.config(commands) if banners: load_banners(module, banners) result['changed'] = True if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
def present(module, result): match = module.params['match'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) configobjs = candidate.difference(config) else: config = None configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'lines') commands = sanitize_config(commands.split('\n')) result['updates'] = commands if module.params['update'] != 'check': # check if creating checkpoints is possible config = module.config.get_config() if 'rollback-location' not in config: warn = 'Cannot create checkpoint. Please enable this feature ' \ 'with "configure system rollback rollback-location" ' \ 'command. Automatic rollback will be disabled' result['warnings'].append(warn) result['__checkpoint__'] = False else: result['__checkpoint__'] = True # create a config checkpoint prior to trying to # configure the device if result.get('__checkpoint__'): module.cli(['admin rollback save']) # 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 # remove checkpoint from system if result.get('__checkpoint__'): module.cli(['admin rollback delete latest-rb']) if module.params['save'] and not module.check_mode: module.config.save_config()
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) path = module.params['parents'] 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 # create a backup copy of the current running-config on # device flash drive backup_config(module) # 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 # remove the backup copy of the running-config since its # no longer needed module.cli('delete /force flash:/ansible-rollback') if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
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 run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate, want_banners = get_candidate(module) if match != 'none': config, have_banners = get_config(module, result) path = module.params['parents'] configobjs = candidate.difference(config, path=path,match=match, replace=replace) else: configobjs = candidate.items have_banners = {} banners = diff_banners(want_banners, have_banners) if configobjs or banners: 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 result['banners'] = banners # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: if commands: module.config(commands) if banners: load_banners(module, banners) result['changed'] = True if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
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 run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] configobjs = None 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 result['changed'] or module.params['save_when'] == 'always': result['changed'] = True if not module.check_mode: cmd = {'command': 'write memory'} run_commands(module, [cmd])
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 run(module, result): match = module.params['match'] replace = module.params['replace'] update = module.params['update'] candidate = get_candidate(module) if match != 'none': config = get_config(module, result) configobjs = candidate.difference(config, match=match, replace=replace) else: config = None configobjs = candidate.items if module.params['backup']: backup_config(module, result) if configobjs: commands = dumps(configobjs, 'commands').split('\n') result['updates'] = commands if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) # if the update mode is set to check just return # and do not try to load into the system if update != 'check': load_config(module, commands, result) # remove the checkpoint file used to restore the config # in case of an error if not module.check_mode: module.cli('no checkpoint %s' % result['__checkpoint__']) if module.params['save'] and not module.check_mode: module.config.save_config() result['changed'] = True
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: module.config.save_config() 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'] admin = module.params['admin'] check_mode = module.check_mode candidate = get_candidate(module) if match != 'none' and replace != 'config': contents = get_running_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj, path=path, match=match, replace=replace) else: commands = candidate.items if commands: commands = dumps(commands, 'commands').split('\n') if any((module.params['lines'], module.params['src'])): if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands diff = load_config(module, commands, result['warnings'], not check_mode, replace_config, comment, admin) if diff: result['diff'] = dict(prepared=diff) result['changed'] = True
def run(module, result): match = module.params['match'] candidate = get_candidate(module) if match != 'none': config_text = get_active_config(module) config = NetworkConfig(indent=4, contents=config_text) configobjs = candidate.difference(config) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands') commands = commands.split('\n') result['commands'] = commands 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
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']), running_config=dict(aliases=['config']), intended_config=dict(), defaults=dict(type='bool', default=False), backup=dict(type='bool', default=False), save_when=dict(choices=['always', 'never', 'modified'], default='never'), diff_against=dict(choices=['running', 'startup', 'intended']), diff_ignore_lines=dict(type='list'), # save is deprecated as of ans2.4, use save_when instead save=dict(default=False, type='bool', removed_in_version='2.4'), # force argument deprecated in ans2.2 force=dict(default=False, type='bool', removed_in_version='2.2') ) argument_spec.update(nxos_argument_spec) mutually_exclusive = [('lines', 'src'), ('save', 'save_when')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines']), ('diff_against', 'intended', ['intended_config'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) warnings = list() nxos_check_args(module, warnings) result = {'changed': False, 'warnings': warnings} config = None if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'): contents = get_config(module) config = NetworkConfig(indent=2, contents=contents) if module.params['backup']: result['__backup__'] = contents if any((module.params['src'], module.params['lines'])): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none': config = get_running_config(module, config) path = module.params['parents'] configobjs = candidate.difference(config, match=match, replace=replace, path=path) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands result['updates'] = commands if not module.check_mode: load_config(module, commands) result['changed'] = True running_config = None startup_config = None diff_ignore_lines = module.params['diff_ignore_lines'] if module.params['save']: module.params['save_when'] = 'always' if module.params['save_when'] != 'never': output = run_commands(module, ['show running-config', 'show startup-config']) running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines) if running_config.sha1 != startup_config.sha1 or module.params['save_when'] == 'always': result['changed'] = True if not module.check_mode: cmd = {'command': 'copy running-config startup-config', 'output': 'text'} run_commands(module, [cmd]) else: module.warn('Skipping command `copy running-config startup-config` ' 'due to check_mode. Configuration not copied to ' 'non-volatile storage') if module._diff: if not running_config: output = run_commands(module, 'show running-config') contents = output[0] else: contents = running_config.config_text # recreate the object in order to process diff_ignore_lines running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if module.params['diff_against'] == 'running': if module.check_mode: module.warn("unable to perform diff against running-config due to check mode") contents = None else: contents = config.config_text elif module.params['diff_against'] == 'startup': if not startup_config: output = run_commands(module, 'show startup-config') contents = output[0] else: contents = output[0] contents = startup_config.config_text elif module.params['diff_against'] == 'intended': contents = module.params['intended_config'] if contents is not None: base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if running_config.sha1 != base_config.sha1: result.update({ 'changed': True, 'diff': {'before': str(base_config), 'after': str(running_config)} }) module.exit_json(**result)
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']), multiline_delimiter=dict(default='@'), # this argument is deprecated (2.2) in favor of setting match: none # it will be removed in a future version force=dict(default=False, type='bool'), config=dict(), defaults=dict(type='bool', default=False), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), ) argument_spec.update(ios_argument_spec) mutually_exclusive = [('lines', '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) if module.params['force'] is True: module.params['match'] = 'none' result = {'changed': False} warnings = list() check_args(module, warnings) result['warnings'] = warnings if any((module.params['lines'], module.params['src'])): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate, want_banners = get_candidate(module) if match != 'none': config, have_banners = get_running_config(module) path = module.params['parents'] configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items have_banners = {} banners = diff_banners(want_banners, have_banners) if configobjs or banners: 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['commands'] = commands result['updates'] = commands result['banners'] = banners # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: if commands: load_config(module, commands) if banners: load_banners(module, banners) result['changed'] = True if module.params['backup']: result['__backup__'] = get_config(module=module) if module.params['save']: if not module.check_mode: run_commands(module, ['copy running-config startup-config\r']) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), src=dict(type='path'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), update=dict(choices=['merge', 'check'], default='merge'), save=dict(type='bool', default=False), config=dict(), backup=dict(type='bool', default=False) ) mutually_exclusive = [('lines', 'src')] module = NetworkModule(argument_spec=argument_spec, connect_on_load=False, mutually_exclusive=mutually_exclusive, supports_check_mode=True) parents = module.params['parents'] or list() match = module.params['match'] replace = module.params['replace'] result = dict(changed=False, saved=False) candidate = get_candidate(module) if match != 'none': config = get_config(module) if parents: config = get_sublevel_config(config, module) configobjs = candidate.difference(config, match=match, replace=replace) else: configobjs = candidate.items if module.params['backup']: result['__backup__'] = module.cli('show running-config')[0] commands = list() if configobjs: commands = dumps(configobjs, 'commands') commands = commands.split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) if not module.check_mode and module.params['update'] == 'merge': response = module.config.load_config(commands) result['responses'] = response if module.params['save']: module.config.save_config() result['saved'] = True result['changed'] = True result['updates'] = commands module.exit_json(**result)
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']), multiline_delimiter=dict(default='@'), running_config=dict(aliases=['config']), intended_config=dict(), defaults=dict(type='bool', default=False), backup=dict(type='bool', default=False), save_when=dict(choices=['always', 'never', 'modified'], default='never'), diff_against=dict(choices=['startup', 'intended', 'running']), diff_ignore_lines=dict(type='list'), # save is deprecated as of ans2.4, use save_when instead save=dict(default=False, type='bool', removed_in_version='2.4'), # force argument deprecated in ans2.2 force=dict(default=False, type='bool', removed_in_version='2.2')) argument_spec.update(ios_argument_spec) mutually_exclusive = [('lines', 'src'), ('save', 'save_when')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines']), ('diff_against', 'intended', ['intended_config'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) result['warnings'] = warnings config = None if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'): contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) if module.params['backup']: result['__backup__'] = contents if any((module.params['lines'], module.params['src'])): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate, want_banners = get_candidate(module) if match != 'none': config, have_banners = get_running_config(module, config) path = module.params['parents'] configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items have_banners = {} banners = diff_banners(want_banners, have_banners) if configobjs or banners: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands result['updates'] = commands result['banners'] = banners # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: if commands: load_config(module, commands) if banners: load_banners(module, banners) result['changed'] = True running_config = None startup_config = None diff_ignore_lines = module.params['diff_ignore_lines'] if module.params['save_when'] != 'never': output = run_commands(module, ['show running-config', 'show startup-config']) running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines) if running_config.sha1 != startup_config.sha1 or module.params[ 'save_when'] == 'always': result['changed'] = True if not module.check_mode: run_commands(module, 'copy running-config startup-config') else: module.warn( 'Skipping command `copy running-config startup-config` ' 'due to check_mode. Configuration not copied to ' 'non-volatile storage') if module._diff: if not running_config: output = run_commands(module, 'show running-config') contents = output[0] else: contents = running_config.config_text # recreate the object in order to process diff_ignore_lines running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if module.params['diff_against'] == 'running': if module.check_mode: module.warn( "unable to perform diff against running-config due to check mode" ) contents = None else: contents = config.config_text elif module.params['diff_against'] == 'startup': if not startup_config: output = run_commands(module, 'show startup-config') contents = output[0] else: contents = startup_config.config_text elif module.params['diff_against'] == 'intended': contents = module.params['intended_config'] if contents is not None: base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if running_config.sha1 != base_config.sha1: result.update({ 'changed': True, 'diff': { 'before': str(base_config), 'after': str(running_config) } }) module.exit_json(**result)
def main(): argument_spec = dict(lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), src=dict(type='path'), before=dict(type='list'), after=dict(type='list'), match=dict( default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), update_config=dict(type='bool', default=False), backup_config=dict(type='bool', default=False)) argument_spec.update(ios_argument_spec) mutually_exclusive = [('lines', 'src')] module = NetworkModule(argument_spec=argument_spec, connect_on_load=False, mutually_exclusive=mutually_exclusive, supports_check_mode=True) module.check_mode = not module.params['update_config'] parents = module.params['parents'] or list() match = module.params['match'] replace = module.params['replace'] warnings = list() invoke('check_args', module, warnings) result = dict(changed=False, saved=False) candidate = get_candidate(module) if module.params['match'] != 'none': config = get_config(module) configobjs = candidate.difference(config, match=match, replace=replace) else: configobjs = candidate.items if module.params['backup_config']: result['__backup__'] = module.cli('show running-config')[0] commands = list() if configobjs: commands = dumps(configobjs, 'commands') commands = commands.split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) if not module.check_mode: response = load_config(module, commands, nodiff=True) result.update(**response) result['changed'] = True result['updates'] = commands result['connected'] = module.connected 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(): argument_spec = dict( lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), src=dict(type='path'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), update=dict(choices=['merge', 'check'], default='merge'), save=dict(type='bool', default=False), config=dict(), backup=dict(type='bool', default=False) ) argument_spec.update(dellos9_argument_spec) mutually_exclusive = [('lines', 'src')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) parents = module.params['parents'] or list() match = module.params['match'] replace = module.params['replace'] warnings = list() check_args(module, warnings) result = dict(changed=False, saved=False, warnings=warnings) candidate = get_candidate(module) if match != 'none': config = get_config(module) if parents: contents = get_sublevel_config(config, module) config = NetworkConfig(contents=contents, indent=1) else: config = NetworkConfig(contents=config, indent=1) configobjs = candidate.difference(config, match=match, replace=replace) else: configobjs = candidate.items if module.params['backup']: result['__backup__'] = get_config(module) commands = list() if configobjs: commands = dumps(configobjs, 'commands') commands = commands.split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) if not module.check_mode and module.params['update'] == 'merge': load_config(module, commands) if module.params['save']: cmd = {'command': 'copy runing-config startup-config', 'prompt': WARNING_PROMPTS_RE, 'answer': 'yes'} run_commands(module, [cmd]) result['saved'] = True result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='path'), lines=dict(aliases=['commands'], type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'none']), running_config=dict(aliases=['config']), intended_config=dict(), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), diff_against=dict(choices=['running', 'intended']), diff_ignore_lines=dict(type='list') ) argument_spec.update(aireos_argument_spec) mutually_exclusive = [('lines', 'src')] required_if = [('diff_against', 'intended', ['intended_config'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) warnings = list() aireos_check_args(module, warnings) result = {'changed': False, 'warnings': warnings} config = None if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'): contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) if module.params['backup']: result['__backup__'] = contents if any((module.params['src'], module.params['lines'])): match = module.params['match'] candidate = get_candidate(module) if match != 'none': config = get_running_config(module, config) configobjs = candidate.difference(config, match=match) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands result['updates'] = commands if not module.check_mode: load_config(module, commands) result['changed'] = True diff_ignore_lines = module.params['diff_ignore_lines'] if module.params['save']: result['changed'] = True if not module.check_mode: command = {"command": "save config", "prompt": "Are you sure you want to save", "answer": "y"} run_commands(module, command) else: module.warn('Skipping command `save config` due to check_mode. Configuration not copied to non-volatile storage') if module._diff: output = run_commands(module, 'show run-config commands') contents = output[0] # recreate the object in order to process diff_ignore_lines running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if module.params['diff_against'] == 'running': if module.check_mode: module.warn("unable to perform diff against running-config due to check mode") contents = None else: contents = config.config_text elif module.params['diff_against'] == 'intended': contents = module.params['intended_config'] if contents is not None: base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if running_config.sha1 != base_config.sha1: result.update({ 'changed': True, 'diff': {'before': str(base_config), 'after': str(running_config)} }) module.exit_json(**result)