def main(): """ main entry point for module execution """ argument_spec = dict( banner=dict(required=True, choices=['exec', 'motd']), text=dict(), state=dict(default='present', choices=['present', 'absent']) ) argument_spec.update(nxos_argument_spec) required_if = [('state', 'present', ('text',))] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings want = map_params_to_obj(module) have = map_config_to_obj(module) commands = map_obj_to_commands(want, have, module) result['commands'] = commands if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( pkg=dict(required=True), file_system=dict(required=False, default='bootflash:'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} pkg = module.params['pkg'] file_system = module.params['file_system'] remote_exists = remote_file_exists(module, pkg, file_system=file_system) if not remote_exists: module.fail_json( msg="The requested package doesn't exist on the device") commands = get_commands(module, pkg, file_system) if commands: results['changed'] = True if not module.check_mode: apply_patch(module, commands) if 'configure' in commands: commands.pop(0) results['commands'] = commands module.exit_json(**results)
def main(): argument_spec = dict( system_mode_maintenance=dict(required=False, type='bool'), system_mode_maintenance_dont_generate_profile=dict(required=False, type='bool'), system_mode_maintenance_timeout=dict(required=False, type='str'), system_mode_maintenance_shutdown=dict(required=False, type='bool'), system_mode_maintenance_on_reload_reset_reason=dict( required=False, choices=[ 'hw_error', 'svc_failure', 'kern_failure', 'wdog_timeout', 'fatal_error', 'lc_failure', 'match_any', 'manual_reload' ]), state=dict(choices=['absent', 'present', 'default'], default='present', required=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[[ 'system_mode_maintenance', 'system_mode_maintenance_dont_generate_profile', 'system_mode_maintenance_timeout', 'system_mode_maintenance_shutdown', 'system_mode_maintenance_on_reload_reset_reason' ]], required_one_of=[[ 'system_mode_maintenance', 'system_mode_maintenance_dont_generate_profile', 'system_mode_maintenance_timeout', 'system_mode_maintenance_shutdown', 'system_mode_maintenance_on_reload_reset_reason' ]], supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] mode = get_system_mode(module) commands = get_commands(module, state, mode) changed = False if commands: if module.check_mode: module.exit_json(changed=True, commands=commands) else: load_config(module, commands) changed = True result = {} result['changed'] = changed if module._verbosity > 0: final_system_mode = get_system_mode(module) result['final_system_mode'] = final_system_mode result['updates'] = commands result['warnings'] = warnings module.exit_json(**result)
def main(): argument_spec = dict( ssm_range=dict(required=True, type='str'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'commands': [], 'warnings': warnings} splitted_ssm_range = module.params['ssm_range'].split('.') if len(splitted_ssm_range) != 4 and module.params['ssm_range'] != 'none': module.fail_json(msg="Valid ssm_range values are multicast addresses " "or the keyword 'none'.") args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed = dict((k, v) for k, v in module.params.items() if k in args and v != existing[k]) candidate = CustomNetworkConfig(indent=3) get_commands(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate result['changed'] = True load_config(module, candidate) module.exit_json(**result)
def main(): argument_spec = dict(ssm_range=dict(required=True, type='str'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'commands': [], 'warnings': warnings} splitted_ssm_range = module.params['ssm_range'].split('.') if len(splitted_ssm_range) != 4 and module.params['ssm_range'] != 'none': module.fail_json(msg="Valid ssm_range values are multicast addresses " "or the keyword 'none'.") args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed = dict((k, v) for k, v in module.params.items() if k in args and v != existing[k]) candidate = CustomNetworkConfig(indent=3) get_commands(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate result['changed'] = True load_config(module, candidate) module.exit_json(**result)
def main(): argument_spec = dict( interface=dict(required=True, type='str'), description=dict(required=False, type='str'), host_reachability=dict(required=False, type='bool'), shutdown=dict(required=False, type='bool'), source_interface=dict(required=False, type='str'), source_interface_hold_down_time=dict(required=False, type='str'), state=dict(choices=['present', 'absent'], default='present', required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() result = {'changed': False, 'commands': [], 'warnings': warnings} check_args(module, warnings) state = module.params['state'] args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if key != 'interface': if str(value).lower() == 'default': value = PARAM_TO_DEFAULT_KEYMAP.get(key) if value is None: if key in BOOL_PARAMS: value = False else: value = 'default' if str(existing.get(key)).lower() != str(value).lower(): proposed[key] = value candidate = CustomNetworkConfig(indent=3) if state == 'present': if not existing: warnings.append("The proposed NVE interface did not exist. " "It's recommended to use nxos_interface to create " "all logical interfaces.") state_present(module, existing, proposed, candidate) elif state == 'absent' and existing: state_absent(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate result['changed'] = True load_config(module, candidate) module.exit_json(**result)
def main(): argument_spec = dict( anycast_gateway_mac=dict(required=True, type='str'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'commands': [], 'warnings': warnings} args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) candidate = CustomNetworkConfig(indent=3) get_commands(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate if not module.check_mode: load_config(module, candidate) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( pkg=dict(required=True), file_system=dict(required=False, default='bootflash:'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} pkg = module.params['pkg'] file_system = module.params['file_system'] remote_exists = remote_file_exists(module, pkg, file_system=file_system) if not remote_exists: module.fail_json( msg="The requested package doesn't exist on the device" ) commands = get_commands(module, pkg, file_system) if commands: results['changed'] = True if not module.check_mode: apply_patch(module, commands) if 'configure' in commands: commands.pop(0) results['commands'] = commands module.exit_json(**results)
def main(): argument_spec = dict( mode=dict(choices=['enabled', 'disabled', 'aggressive'], required=True), interface=dict(type='str', required=True), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) interface = module.params['interface'].lower() mode = module.params['mode'] state = module.params['state'] proposed = dict(mode=mode) existing = get_udld_interface(module, interface) end_state = existing delta = dict(set(proposed.items()).difference(existing.items())) changed = False commands = [] if state == 'present': if delta: command = get_commands_config_udld_interface( delta, interface, module, existing) commands.append(command) elif state == 'absent': common = set(proposed.items()).intersection(existing.items()) if common: command = get_commands_remove_udld_interface( dict(common), interface, module, existing) commands.append(command) cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True load_config(module, cmds) end_state = get_udld_interface(module, interface) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict( system_mode_maintenance=dict(required=False, type='bool'), system_mode_maintenance_dont_generate_profile=dict(required=False, type='bool'), system_mode_maintenance_timeout=dict(required=False, type='str'), system_mode_maintenance_shutdown=dict(required=False, type='bool'), system_mode_maintenance_on_reload_reset_reason=dict(required=False, choices=['hw_error','svc_failure','kern_failure', 'wdog_timeout','fatal_error','lc_failure', 'match_any','manual_reload']), state=dict(choices=['absent', 'present', 'default'], default='present', required=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[[ 'system_mode_maintenance', 'system_mode_maintenance_dont_generate_profile', 'system_mode_maintenance_timeout', 'system_mode_maintenance_shutdown', 'system_mode_maintenance_on_reload_reset_reason' ]], required_one_of=[[ 'system_mode_maintenance', 'system_mode_maintenance_dont_generate_profile', 'system_mode_maintenance_timeout', 'system_mode_maintenance_shutdown', 'system_mode_maintenance_on_reload_reset_reason' ]], supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] mode = get_system_mode(module) commands = get_commands(module, state, mode) changed = False if commands: if module.check_mode: module.exit_json(changed=True, commands=commands) else: load_config(module, commands) changed = True result = {} result['changed'] = changed if module._verbosity > 0: final_system_mode = get_system_mode(module) result['final_system_mode'] = final_system_mode result['updates'] = commands result['warnings'] = warnings module.exit_json(**result)
def main(): argument_spec = dict(rp_address=dict(required=True, type='str'), group_list=dict(required=False, type='str'), prefix_list=dict(required=False, type='str'), route_map=dict(required=False, type='str'), bidir=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[['group_list', 'route_map'], ['group_list', 'prefix_list'], ['route_map', 'prefix_list']], supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] args = ['rp_address', 'group_list', 'prefix_list', 'route_map', 'bidir'] existing = invoke('get_existing', module, args) end_state = existing proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if str(value).lower() == 'true': value = True elif str(value).lower() == 'false': value = False for each in existing: if each.get(key) or (not each.get(key) and value): proposed[key] = value result = {} candidate = CustomNetworkConfig(indent=3) invoke('state_%s' % state, module, existing, proposed, candidate) response = load_config(module, candidate) result.update(response) if module._verbosity > 0: end_state = invoke('get_existing', module, args) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed_args if WARNINGS: result['warnings'] = WARNINGS module.exit_json(**result)
def main(): """entry point for module execution """ argument_spec = dict( # { command: <str>, output: <str>, prompt: <str>, response: <str> } commands=dict(type='list', required=True), wait_for=dict(type='list', aliases=['waitfor']), match=dict(default='all', choices=['any', 'all']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int')) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) commands = parse_commands(module, warnings) result['warnings'] = warnings wait_for = module.params['wait_for'] or list() try: conditionals = [Conditional(c) for c in wait_for] except AttributeError: exc = get_exception() module.fail_json(msg=str(exc)) retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] while retries > 0: responses = run_commands(module, commands) for item in list(conditionals): if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) retries -= 1 if conditionals: failed_conditions = [item.raw for item in conditionals] msg = 'One or more conditional statements have not be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({'stdout': responses, 'stdout_lines': to_lines(responses)}) module.exit_json(**result)
def main(): argument_spec = dict( rp_address=dict(required=True, type='str'), group_list=dict(required=False, type='str'), prefix_list=dict(required=False, type='str'), route_map=dict(required=False, type='str'), bidir=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present', required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[['group_list', 'route_map'], ['group_list', 'prefix_list'], ['route_map', 'prefix_list']], supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'commands': [], 'warnings': warnings} state = module.params['state'] args = [ 'rp_address', 'group_list', 'prefix_list', 'route_map', 'bidir' ] existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if key != 'rp_address': if str(value).lower() == 'true': value = True elif str(value).lower() == 'false': value = False if existing.get(key) != value: proposed[key] = value candidate = CustomNetworkConfig(indent=3) if state == 'present' and (proposed or not existing): state_present(module, existing, proposed, candidate) elif state == 'absent' and existing: state_absent(module, existing, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate result['changed'] = True load_config(module, candidate) module.exit_json(**result)
def main(): argument_spec = dict(local_file=dict(required=True), remote_file=dict(required=False), file_system=dict(required=False, default='bootflash:'), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not HAS_SCP: module.fail_json( msg='library scp is required but does not appear to be ' 'installed. It can be installed using `pip install scp`') warnings = list() check_args(module, warnings) local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] changed = False transfer_status = 'No Transfer' if not os.path.isfile(local_file): module.fail_json(msg="Local file {} not found".format(local_file)) dest = remote_file or os.path.basename(local_file) remote_exists = remote_file_exists(module, dest, file_system=file_system) if not remote_exists: changed = True file_exists = False else: file_exists = True if not module.check_mode and not file_exists: try: transfer_file(module, dest) transfer_status = 'Sent' except ShellError: clie = get_exception() module.fail_json(msg=str(clie)) if remote_file is None: remote_file = os.path.basename(local_file) module.exit_json(changed=changed, transfer_status=transfer_status, local_file=local_file, remote_file=remote_file, warnings=warnings, file_system=file_system)
def main(): """ main entry point for module execution """ argument_spec = dict( users=dict(type='list', no_log=True), name=dict(), password=dict(no_log=True), update_password=dict(default='always', choices=['on_create', 'always']), roles=dict(type='list'), sshkey=dict(), purge=dict(type='bool', default=False), state=dict(default='present', choices=['present', 'absent']) ) argument_spec.update(nxos_argument_spec) mutually_exclusive = [('name', 'users')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) result['warnings'] = warnings want = map_params_to_obj(module) have = map_config_to_obj(module) commands = map_obj_to_commands(update_objects(want, have), module) if module.params['purge']: want_users = [x['name'] for x in want] have_users = [x['name'] for x in have] for item in set(have_users).difference(want_users): if item != 'admin': commands.append('no username %s' % item) result['commands'] = commands # the nxos cli prevents this by rule so capture it and display # a nice failure message if 'no username admin' in commands: module.fail_json(msg='cannot delete the `admin` account') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( interface=dict(required=True, type='str'), description=dict(required=False, type='str'), host_reachability=dict(required=False, type='bool'), shutdown=dict(required=False, type='bool'), source_interface=dict(required=False, type='str'), source_interface_hold_down_time=dict(required=False, type='str'), m_facts=dict(required=False, default=False, type='bool'), state=dict(choices=['present', 'absent'], default='present', required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() result = {'changed': False, 'commands': [], 'warnings': warnings} check_args(module, warnings) state = module.params['state'] args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if key != 'interface': if str(value).lower() == 'default': value = PARAM_TO_DEFAULT_KEYMAP.get(key) if value is None: if key in BOOL_PARAMS: value = False else: value = 'default' if str(existing.get(key)).lower() != str(value).lower(): proposed[key] = value candidate = CustomNetworkConfig(indent=3) if state == 'present': if not existing: warnings.append("The proposed NVE interface did not exist. " "It's recommended to use nxos_interface to create " "all logical interfaces.") state_present(module, existing, proposed, candidate) elif state == 'absent' and existing: state_absent(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() result['commands'] = candidate result['changed'] = True load_config(module, candidate) module.exit_json(**result)
def main(): argument_spec = dict(feature=dict(type='str', required=True), state=dict(choices=['enabled', 'disabled'], default='enabled', required=False), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) feature = validate_feature(module) state = module.params['state'].lower() available_features = get_available_features(feature, module) if feature not in available_features: module.fail_json(msg='Invalid feature name.', features_currently_supported=available_features, invalid_feature=feature) else: existstate = available_features[feature] existing = dict(state=existstate) proposed = dict(state=state) changed = False end_state = existing cmds = get_commands(proposed, existing, state, module) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: load_config(module, cmds) changed = True updated_features = get_available_features(feature, module) existstate = updated_features[feature] end_state = dict(state=existstate) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings results['feature'] = module.params['feature'] module.exit_json(**results)
def main(): argument_spec = dict( action=dict( required=True, choices=['create', 'add', 'compare', 'delete', 'delete_all']), snapshot_name=dict(type='str'), description=dict(type='str'), snapshot1=dict(type='str'), snapshot2=dict(type='str'), compare_option=dict(choices=['summary', 'ipv4routes', 'ipv6routes']), comparison_results_file=dict(type='str'), section=dict(type='str'), show_command=dict(type='str'), row_id=dict(type='str'), element_key1=dict(type='str'), element_key2=dict(type='str'), save_snapshot_locally=dict(type='bool', default=False), path=dict(type='str', default='./')) argument_spec.update(nxos_argument_spec) required_if = [("action", "compare", ["snapshot1", "snapshot2", "comparison_results_file"]), ("action", "create", ["snapshot_name", "description"]), ("action", "add", ["section", "show_command", "row_id", "element_key1"]), ("action", "delete", ["snapshot_name"])] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) warnings = list() check_args(module, warnings) action = module.params['action'] comparison_results_file = module.params['comparison_results_file'] if not os.path.isdir(module.params['path']): module.fail_json(msg='{0} is not a valid directory name.'.format( module.params['path'])) existing_snapshots = invoke('get_existing', module) action_results = invoke('action_%s' % action, module, existing_snapshots) result = {'changed': False, 'commands': []} if not module.check_mode: if action == 'compare': result['commands'] = [] else: if action_results: load_config(module, action_results) result['commands'] = action_results result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( name=dict(required=False, type='str'), interface=dict(required=True), direction=dict(required=True, choices=['egress', 'ingress']), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) state = module.params['state'] name = module.params['name'] interface = module.params['interface'].lower() direction = module.params['direction'].lower() proposed = dict(name=name, interface=interface, direction=direction) existing = check_for_acl_int_present(module, name, interface, direction) cmds = [] commands = [] if state == 'present': if not existing: command = apply_acl(proposed) if command: commands.append(command) elif state == 'absent': if existing: command = remove_acl(proposed) if command: commands.append(command) if commands: cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: load_config(module, cmds) results['changed'] = True if 'configure' in cmds: cmds.pop(0) else: cmds = [] results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict(vrf=dict(required=True, type='str'), safi=dict(required=True, type='str', choices=['unicast', 'multicast']), afi=dict(required=True, type='str', choices=['ipv4', 'ipv6']), route_target_both_auto_evpn=dict(required=False, type='bool'), m_facts=dict(required=False, default=False, type='bool'), state=dict(choices=['present', 'absent'], default='present', required=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = dict(changed=False, warnings=warnings) state = module.params['state'] args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if key != 'interface': if str(value).lower() == 'default': value = PARAM_TO_DEFAULT_KEYMAP.get(key) if value is None: value = 'default' if existing.get(key) != value: proposed[key] = value candidate = CustomNetworkConfig(indent=3) if state == 'present': state_present(module, existing, proposed, candidate) elif state == 'absent' and existing: state_absent(module, existing, proposed, candidate) if candidate: candidate = candidate.items_text() load_config(module, candidate) result['changed'] = True result['commands'] = candidate else: result['commands'] = [] module.exit_json(**result)
def main(): argument_spec = dict( flush_routes=dict(type='bool'), enforce_rtr_alert=dict(type='bool'), restart=dict(type='bool', default=False), state=dict(choices=['present', 'default'], default='present'), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) current = get_current(module) desired = get_desired(module) state = module.params['state'] restart = module.params['restart'] commands = list() if state == 'default': if current['flush_routes']: commands.append('no ip igmp flush-routes') if current['enforce_rtr_alert']: commands.append('no ip igmp enforce-router-alert') elif state == 'present': if desired['flush_routes'] and not current['flush_routes']: commands.append('ip igmp flush-routes') if desired['enforce_rtr_alert'] and not current['enforce_rtr_alert']: commands.append('ip igmp enforce-router-alert') result = {'changed': False, 'updates': commands, 'warnings': warnings} if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True if module.params['restart']: run_commands(module, 'restart igmp') module.exit_json(**result)
def main(): argument_spec = dict( commands=dict(required=False, type='list'), mode=dict(required=True, choices=['maintenance', 'normal']), state=dict(choices=['absent', 'present'], default='present'), include_defaults=dict(default=False), config=dict() ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] commands = module.params['commands'] or [] if state == 'absent' and commands: module.fail_json(msg='when state is absent, no command can be used.') existing = invoke('get_existing', module) end_state = existing changed = False result = {} cmds = [] if state == 'present' or (state == 'absent' and existing): cmds = invoke('state_%s' % state, module, existing, commands) if module.check_mode: module.exit_json(changed=True, commands=cmds) else: load_config(module, cmds) changed = True end_state = invoke('get_existing', module) result['connected'] = module.connected result['changed'] = changed if module._verbosity > 0: end_state = invoke('get_existing', module) result['end_state'] = end_state result['existing'] = existing result['proposed'] = commands result['updates'] = cmds result['warnings'] = warnings module.exit_json(**result)
def main(): argument_spec = dict( prefix=dict(required=True, type='str'), next_hop=dict(required=True, type='str'), vrf=dict(type='str', default='default'), tag=dict(type='str'), route_name=dict(type='str'), pref=dict(type='str'), state=dict(choices=['absent', 'present'], default='present'), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] result = dict(changed=False) warnings = list() prefix = invoke('normalize_prefix', module, module.params['prefix']) existing = invoke('get_existing', module, prefix, warnings) end_state = existing args = ['route_name', 'vrf', 'pref', 'tag', 'next_hop', 'prefix'] proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) if state == 'present' or (state == 'absent' and existing): candidate = CustomNetworkConfig(indent=3) invoke('state_%s' % state, module, candidate, prefix) load_config(module, candidate) else: result['updates'] = [] result['warnings'] = warnings if module._verbosity > 0: end_state = invoke('get_existing', module, prefix, warnings) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed module.exit_json(**result)
def main(): argument_spec = dict( commands=dict(required=False, type='list'), mode=dict(required=True, choices=['maintenance', 'normal']), state=dict(choices=['absent', 'present'], default='present'), include_defaults=dict(default=False), config=dict() ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] commands = module.params['commands'] or [] if state == 'absent' and commands: module.fail_json(msg='when state is absent, no command can be used.') existing = invoke('get_existing', module) end_state = existing changed = False result = {} cmds = [] if state == 'present' or (state == 'absent' and existing): cmds = invoke('state_%s' % state, module, existing, commands) if module.check_mode: module.exit_json(changed=True, commands=cmds) else: if cmds: load_config(module, cmds) changed = True end_state = invoke('get_existing', module) result['changed'] = changed if module._verbosity > 0: end_state = invoke('get_existing', module) result['end_state'] = end_state result['existing'] = existing result['proposed'] = commands result['updates'] = cmds result['warnings'] = warnings module.exit_json(**result)
def main(): argument_spec = dict( contact=dict(required=True, type='str'), state=dict(choices=['absent', 'present'], default='present') ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) contact = module.params['contact'] state = module.params['state'] existing = get_snmp_contact(module) changed = False proposed = dict(contact=contact) end_state = existing commands = [] if state == 'absent': if existing and existing['contact'] == contact: commands.append('no snmp-server contact') elif state == 'present': if not existing or existing['contact'] != contact: commands.append('snmp-server contact {0}'.format(contact)) cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True load_config(module, cmds) end_state = get_snmp_contact(module) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict(ssm_range=dict(required=True, type='str'), m_facts=dict(required=False, default=False, type='bool'), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) splitted_ssm_range = module.params['ssm_range'].split('.') if len(splitted_ssm_range) != 4 and module.params['ssm_range'] != 'none': module.fail_json(msg="Valid ssm_range values are multicast addresses " "or the keyword 'none'.") args = ['ssm_range'] existing = invoke('get_existing', module, args) end_state = existing proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) result = {} candidate = CustomNetworkConfig(indent=3) invoke('get_commands', module, existing, proposed, candidate) try: response = load_config(module, candidate) result.update(response) except ShellError: exc = get_exception() module.fail_json(msg=str(exc)) result['connected'] = module.connected if module._verbosity > 0: end_state = invoke('get_existing', module, args) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed if WARNINGS: result['warnings'] = WARNINGS module.exit_json(**result)
def main(): argument_spec = dict(ospf=dict(required=True, type='str'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] ospf = str(module.params['ospf']) existing = invoke('get_existing', module) end_state = existing proposed = dict(ospf=ospf) if not existing: existing_list = [] else: existing_list = existing['ospf'] result = {} if (state == 'present' or (state == 'absent' and ospf in existing_list)): candidate = CustomNetworkConfig(indent=3) invoke('state_%s' % state, module, proposed, candidate) try: response = load_config(module, candidate) result.update(response) except ShellError: exc = get_exception() module.fail_json(msg=str(exc)) else: result['updates'] = [] result['connected'] = module.connected if module._verbosity > 0: end_state = invoke('get_existing', module) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed module.exit_json(**result)
def main(): argument_spec = dict( local_file=dict(required=True), remote_file=dict(required=False), file_system=dict(required=False, default='bootflash:'), connect_ssh_port=dict(required=False, type='int', default=22), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not HAS_SCP: module.fail_json( msg='library scp is required but does not appear to be ' 'installed. It can be installed using `pip install scp`' ) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] results['transfer_status'] = 'No Transfer' results['local_file'] = local_file results['file_system'] = file_system if not local_file_exists(module): module.fail_json(msg="Local file {} not found".format(local_file)) dest = remote_file or os.path.basename(local_file) remote_exists = remote_file_exists(module, dest, file_system=file_system) if not remote_exists: results['changed'] = True file_exists = False else: file_exists = True if not module.check_mode and not file_exists: transfer_file(module, dest) results['transfer_status'] = 'Sent' if remote_file is None: remote_file = os.path.basename(local_file) results['remote_file'] = remote_file module.exit_json(**results)
def main(): argument_spec = dict( state=dict(choices=['enabled', 'disabled'], default='enabled'), group=dict(choices=[ 'aaa', 'bridge', 'callhome', 'cfs', 'config', 'entity', 'feature-control', 'hsrp', 'license', 'link', 'lldp', 'ospf', 'pim', 'rf', 'rmon', 'snmp', 'storm-control', 'stpx', 'sysmgr', 'system', 'upgrade', 'vtp', 'all' ], required=True), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) group = module.params['group'].lower() state = module.params['state'] existing = get_snmp_traps(group, module) proposed = {'group': group} changed = False end_state = existing commands = get_trap_commands(group, state, existing, module) cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True load_config(module, cmds) end_state = get_snmp_traps(group, module) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict( local_file=dict(required=True), remote_file=dict(required=False), file_system=dict(required=False, default='bootflash:'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not HAS_SCP: module.fail_json( msg='library scp is required but does not appear to be ' 'installed. It can be installed using `pip install scp`' ) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] results['transfer_status'] = 'No Transfer' results['local_file'] = local_file results['file_system'] = file_system if not local_file_exists(module): module.fail_json(msg="Local file {} not found".format(local_file)) dest = remote_file or os.path.basename(local_file) remote_exists = remote_file_exists(module, dest, file_system=file_system) if not remote_exists: results['changed'] = True file_exists = False else: file_exists = True if not module.check_mode and not file_exists: transfer_file(module, dest) results['transfer_status'] = 'Sent' if remote_file is None: remote_file = os.path.basename(local_file) results['remote_file'] = remote_file module.exit_json(**results)
def main(): argument_spec = dict(flush_routes=dict(type='bool'), enforce_rtr_alert=dict(type='bool'), restart=dict(type='bool', default=False), state=dict(choices=['present', 'default'], default='present'), include_defaults=dict(default=False), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) current = get_current(module) desired = get_desired(module) state = module.params['state'] restart = module.params['restart'] commands = list() if state == 'default': if current['flush_routes']: commands.append('no ip igmp flush-routes') if current['enforce_rtr_alert']: commands.append('no ip igmp enforce-router-alert') elif state == 'present': if desired['flush_routes'] and not current['flush_routes']: commands.append('ip igmp flush-routes') if desired['enforce_rtr_alert'] and not current['enforce_rtr_alert']: commands.append('ip igmp enforce-router-alert') result = {'changed': False, 'updates': commands, 'warnings': warnings} if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True if module.params['restart']: run_commands(module, 'restart igmp') module.exit_json(**result)
def main(): argument_spec = {} argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) if not module.check_mode: reboot(module) results['changed'] = True module.exit_json(**results)
def main(): argument_spec = dict(version=dict(type='str', choices=['1', '2'], required=True), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) version = module.params['version'] existing = get_vtp_config(module) end_state = existing args = dict(version=version) changed = False proposed = dict((k, v) for k, v in args.items() if v is not None) delta = dict(set(proposed.items()).difference(existing.items())) commands = [] if delta: commands.append(['vtp version {0}'.format(version)]) cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True load_config(module, cmds) end_state = get_vtp_config(module) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict( version=dict(type='str', choices=['1', '2'], required=True), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) version = module.params['version'] existing = get_vtp_config(module) end_state = existing args = dict(version=version) changed = False proposed = dict((k, v) for k, v in args.items() if v is not None) delta = dict(set(proposed.items()).difference(existing.items())) commands = [] if delta: commands.append(['vtp version {0}'.format(version)]) cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True load_config(module, cmds) end_state = get_vtp_config(module) if 'configure' in cmds: cmds.pop(0) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['updates'] = cmds results['changed'] = changed results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict( dest=dict(required=True), count=dict(required=False, default=2), vrf=dict(required=False), source=dict(required=False), state=dict(required=False, choices=['present', 'absent'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) destination = module.params['dest'] count = module.params['count'] state = module.params['state'] if count and not 1 <= int(count) <= 655350: module.fail_json( msg="'count' must be an integer between 1 and 655350.", count=count) ping_command = 'ping {0}'.format(destination) for command in ['count', 'source', 'vrf']: arg = module.params[command] if arg: ping_command += ' {0} {1}'.format(command, arg) summary, rtt, ping_pass = get_ping_results(ping_command, module) results = summary results['rtt'] = rtt results['commands'] = [ping_command] if ping_pass and state == 'absent': module.fail_json(msg="Ping succeeded unexpectedly") elif not ping_pass and state == 'present': module.fail_json(msg="Ping failed unexpectedly") module.exit_json(**results)
def main(): argument_spec = {} argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) if not module.check_mode: reboot(module) changed = True results = {'changed': True, 'warnings': warnings} module.exit_json(**results)
def main(): argument_spec = dict( nv_overlay_evpn=dict(required=True, type='bool'), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) existing = invoke('get_existing', module) end_state = existing proposed = dict(nv_overlay_evpn=module.params['nv_overlay_evpn']) result = {} candidate = CustomNetworkConfig(indent=3) invoke('get_commands', module, existing, proposed, candidate) if proposed != existing: try: response = load_config(module, candidate) result.update(response) except ShellError: exc = get_exception() module.fail_json(msg=str(exc)) else: result['updates'] = [] result['connected'] = module.connected if module._verbosity > 0: end_state = invoke('get_existing', module) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed result['warnings'] = warnings module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( hostname=dict(), domain_lookup=dict(type='bool'), # { name: <str>, vrf: <str> } domain_name=dict(type='list'), # {name: <str>, vrf: <str> } domain_search=dict(type='list'), # { server: <str>; vrf: <str> } name_servers=dict(type='list'), system_mtu=dict(type='int'), lookup_source=dict(), state=dict(default='present', choices=['present', 'absent']) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings want = map_params_to_obj(module) have = map_config_to_obj(module) commands = map_obj_to_commands(want, have, module) result['commands'] = commands if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( ospf=dict(required=True, type='str'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = dict(changed=False, warnings=warnings) state = module.params['state'] ospf = str(module.params['ospf']) existing = get_existing(module) proposed = dict(ospf=ospf) if not existing: existing_list = [] else: existing_list = existing['ospf'] candidate = CustomNetworkConfig(indent=3) if state == 'present' and ospf not in existing_list: state_present(module, proposed, candidate) if state == 'absent' and ospf in existing_list: state_absent(module, proposed, candidate) if candidate: candidate = candidate.items_text() load_config(module, candidate) result['changed'] = True result['commands'] = candidate else: result['commands'] = [] module.exit_json(**result)
def main(): """ main entry point for module execution """ argument_spec = dict( dest=dict(choices=DEST_GROUP), name=dict(), facility=dict(), dest_level=dict(type='int', aliases=['level']), facility_level=dict(type='int'), state=dict(default='present', choices=['present', 'absent']), aggregate=dict(type='list'), purge=dict(default=False, type='bool') ) argument_spec.update(nxos_argument_spec) required_if = [('dest', 'logfile', ['name'])] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, required_together=[['facility', 'facility_level']], supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings want = map_params_to_obj(module) have = map_config_to_obj(module) commands = map_obj_to_commands((want, have), module) result['commands'] = commands if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True module.exit_json(**result)
def main(): argument_spec = dict( dest=dict(required=True), count=dict(required=False, default=2), vrf=dict(required=False), source=dict(required=False), state=dict(required=False, choices=['present', 'absent'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) destination = module.params['dest'] count = module.params['count'] state = module.params['state'] if count and not 1 <= int(count) <= 655350: module.fail_json(msg="'count' must be an integer between 1 and 655350.", count=count) ping_command = 'ping {0}'.format(destination) for command in ['count', 'source', 'vrf']: arg = module.params[command] if arg: ping_command += ' {0} {1}'.format(command, arg) summary, rtt, ping_pass = get_ping_results(ping_command, module) results = summary results['rtt'] = rtt results['commands'] = [ping_command] if ping_pass and state == 'absent': module.fail_json(msg="Ping succeeded unexpectedly") elif not ping_pass and state == 'present': module.fail_json(msg="Ping failed unexpectedly") module.exit_json(**results)
def main(): argument_spec = dict( system_image_file=dict(required=True), kickstart_image_file=dict(required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) system_image_file = module.params['system_image_file'] kickstart_image_file = module.params['kickstart_image_file'] if kickstart_image_file == 'null': kickstart_image_file = None current_boot_options = get_boot_options(module) changed = False if not already_set(current_boot_options, system_image_file, kickstart_image_file): changed = True install_state = current_boot_options if not module.check_mode and changed is True: set_boot_options(module, system_image_file, kickstart=kickstart_image_file) if not already_set(install_state, system_image_file, kickstart_image_file): module.fail_json(msg='Install not successful', install_state=install_state) module.exit_json(changed=changed, install_state=install_state, warnings=warnings)
def main(): argument_spec = dict( location=dict(required=True, type='str'), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} location = module.params['location'] state = module.params['state'] existing = get_snmp_location(module) commands = [] if state == 'absent': if existing and existing['location'] == location: commands.append('no snmp-server location') elif state == 'present': if not existing or existing['location'] != location: commands.append('snmp-server location {0}'.format(location)) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict( state=dict(choices=['enabled', 'disabled'], default='enabled'), group=dict(choices=['aaa', 'bridge', 'callhome', 'cfs', 'config', 'entity', 'feature-control', 'hsrp', 'license', 'link', 'lldp', 'ospf', 'pim', 'rf', 'rmon', 'snmp', 'storm-control', 'stpx', 'sysmgr', 'system', 'upgrade', 'vtp', 'all'], required=True), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} group = module.params['group'].lower() state = module.params['state'] existing = get_snmp_traps(group, module) commands = get_trap_commands(group, state, existing, module) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict( prefix=dict(required=True, type='str'), next_hop=dict(required=True, type='str'), vrf=dict(type='str', default='default'), tag=dict(type='str'), route_name=dict(type='str'), pref=dict(type='str'), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) warnings = list() check_args(module, warnings) result = dict(changed=False, warnings=warnings) prefix = normalize_prefix(module, module.params['prefix']) candidate = CustomNetworkConfig(indent=3) reconcile_candidate(module, candidate, prefix) if candidate: candidate = candidate.items_text() load_config(module, candidate) result['commands'] = candidate result['changed'] = True else: result['commands'] = [] module.exit_json(**result)
def main(): argument_spec = dict( master=dict(required=False, type='bool'), stratum=dict(required=False, type='str', default='8'), logging=dict(required=False, type='bool'), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) master = module.params['master'] stratum = module.params['stratum'] logging = module.params['logging'] state = module.params['state'] if stratum: try: stratum_int = int(stratum) if stratum_int < 1 or stratum_int > 15: raise ValueError except ValueError: module.fail_json(msg='stratum must be an integer between 1 and 15') desired = {'master': master, 'stratum': stratum, 'logging': logging} current = get_current(module) result = {'changed': False} commands = list() if state == 'absent': if current['master']: commands.append('no ntp master') if current['logging']: commands.append('no ntp logging') elif state == 'present': if desired['master'] and desired['master'] != current['master']: if desired['stratum']: commands.append('ntp master %s' % stratum) else: commands.append('ntp master') elif desired['stratum'] and desired['stratum'] != current['stratum']: commands.append('ntp master %s' % stratum) if desired['logging'] and desired['logging'] != current['logging']: if desired['logging']: commands.append('ntp logging') else: commands.append('no ntp logging') result['commands'] = commands result['updates'] = commands if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['warnings'] = warnings module.exit_json(**result)
def main(): argument_spec = dict( interface=dict(required=True, type='str'), mode=dict(choices=['access', 'trunk'], required=False), access_vlan=dict(type='str', required=False), native_vlan=dict(type='str', required=False), trunk_vlans=dict(type='str', aliases=['trunk_add_vlans'], required=False), trunk_allowed_vlans=dict(type='str', required=False), state=dict(choices=['absent', 'present', 'unconfigured'], default='present') ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[['access_vlan', 'trunk_vlans'], ['access_vlan', 'native_vlan'], ['access_vlan', 'trunk_allowed_vlans']], supports_check_mode=True) warnings = list() commands = [] results = {'changed': False} check_args(module, warnings) interface = module.params['interface'] mode = module.params['mode'] access_vlan = module.params['access_vlan'] state = module.params['state'] trunk_vlans = module.params['trunk_vlans'] native_vlan = module.params['native_vlan'] trunk_allowed_vlans = module.params['trunk_allowed_vlans'] args = dict(interface=interface, mode=mode, access_vlan=access_vlan, native_vlan=native_vlan, trunk_vlans=trunk_vlans, trunk_allowed_vlans=trunk_allowed_vlans) proposed = dict((k, v) for k, v in args.items() if v is not None) interface = interface.lower() if mode == 'access' and state == 'present' and not access_vlan: module.fail_json(msg='access_vlan param is required when mode=access && state=present') if mode == 'trunk' and access_vlan: module.fail_json(msg='access_vlan param not supported when using mode=trunk') current_mode = get_interface_mode(interface, module) # Current mode will return layer3, layer2, or unknown if current_mode == 'unknown' or current_mode == 'layer3': module.fail_json(msg='Ensure interface is configured to be a L2' '\nport first before using this module. You can use' '\nthe nxos_interface module for this.') if interface_is_portchannel(interface, module): module.fail_json(msg='Cannot change L2 config on physical ' '\nport because it is in a portchannel. ' '\nYou should update the portchannel config.') # existing will never be null for Eth intfs as there is always a default existing = get_switchport(interface, module) # Safeguard check # If there isn't an existing, something is wrong per previous comment if not existing: module.fail_json(msg='Make sure you are using the FULL interface name') if trunk_vlans or trunk_allowed_vlans: if trunk_vlans: trunk_vlans_list = vlan_range_to_list(trunk_vlans) elif trunk_allowed_vlans: trunk_vlans_list = vlan_range_to_list(trunk_allowed_vlans) proposed['allowed'] = True existing_trunks_list = vlan_range_to_list((existing['trunk_vlans'])) existing['trunk_vlans_list'] = existing_trunks_list proposed['trunk_vlans_list'] = trunk_vlans_list current_vlans = get_list_of_vlans(module) if state == 'present': if access_vlan and access_vlan not in current_vlans: module.fail_json(msg='You are trying to configure a VLAN' ' on an interface that\ndoes not exist on the ' ' switch yet!', vlan=access_vlan) elif native_vlan and native_vlan not in current_vlans: module.fail_json(msg='You are trying to configure a VLAN' ' on an interface that\ndoes not exist on the ' ' switch yet!', vlan=native_vlan) else: command = get_switchport_config_commands(interface, existing, proposed, module) commands.append(command) elif state == 'unconfigured': is_default = is_switchport_default(existing) if not is_default: command = default_switchport_config(interface) commands.append(command) elif state == 'absent': command = remove_switchport_config_commands(interface, existing, proposed, module) commands.append(command) if trunk_vlans or trunk_allowed_vlans: existing.pop('trunk_vlans_list') proposed.pop('trunk_vlans_list') cmds = flatten_list(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: results['changed'] = True load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds results['warnings'] = warnings module.exit_json(**results)
def main(): argument_spec = dict( snooping=dict(required=False, type='bool'), group_timeout=dict(required=False, type='str'), link_local_grp_supp=dict(required=False, type='bool'), report_supp=dict(required=False, type='bool'), v3_report_supp=dict(required=False, type='bool'), state=dict(choices=['present', 'default'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} snooping = module.params['snooping'] link_local_grp_supp = module.params['link_local_grp_supp'] report_supp = module.params['report_supp'] v3_report_supp = module.params['v3_report_supp'] group_timeout = module.params['group_timeout'] state = module.params['state'] args = dict(snooping=snooping, link_local_grp_supp=link_local_grp_supp, report_supp=report_supp, v3_report_supp=v3_report_supp, group_timeout=group_timeout) proposed = dict((param, value) for (param, value) in args.items() if value is not None) existing = get_igmp_snooping(module) end_state = existing commands = [] if state == 'present': delta = dict( set(proposed.items()).difference(existing.items()) ) if delta: command = config_igmp_snooping(delta, existing) if command: commands.append(command) elif state == 'default': proposed = get_igmp_snooping_defaults() delta = dict( set(proposed.items()).difference(existing.items()) ) if delta: command = config_igmp_snooping(delta, existing, default=True) if command: commands.append(command) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): """entry point for module execution """ argument_spec = dict( # { command: <str>, output: <str>, prompt: <str>, response: <str> } commands=dict(type='list', required=True), wait_for=dict(type='list', aliases=['waitfor']), match=dict(default='all', choices=['any', 'all']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int') ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) commands = parse_commands(module, warnings) result['warnings'] = warnings wait_for = module.params['wait_for'] or list() try: conditionals = [Conditional(c) for c in wait_for] except AttributeError: exc = get_exception() module.fail_json(msg=str(exc)) retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] while retries > 0: responses = run_commands(module, commands) for item in list(conditionals): try: if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) except FailedConditionalError: exc = get_exception() module.fail_json(msg=str(exc)) if not conditionals: break time.sleep(interval) retries -= 1 if conditionals: failed_conditions = [item.raw for item in conditionals] msg = 'One or more conditional statements have not be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({ 'stdout': responses, 'stdout_lines': to_lines(responses) }) module.exit_json(**result)
def main(): argument_spec = dict( interface=dict(required=True), sparse=dict(type='bool', default=True), dr_prio=dict(type='str'), hello_auth_key=dict(type='str'), hello_interval=dict(type='int'), jp_policy_out=dict(type='str'), jp_policy_in=dict(type='str'), jp_type_out=dict(choices=['prefix', 'routemap']), jp_type_in=dict(choices=['prefix', 'routemap']), border=dict(type='bool'), neighbor_policy=dict(type='str'), neighbor_type=dict(choices=['prefix', 'routemap']), state=dict(choices=['present', 'absent', 'default'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} state = module.params['state'] interface = module.params['interface'] jp_type_in = module.params['jp_type_in'] jp_type_out = module.params['jp_type_out'] jp_policy_in = module.params['jp_policy_in'] jp_policy_out = module.params['jp_policy_out'] neighbor_policy = module.params['neighbor_policy'] neighbor_type = module.params['neighbor_type'] hello_interval = module.params['hello_interval'] intf_type = get_interface_type(interface) if get_interface_mode(interface, intf_type, module) == 'layer2': module.fail_json(msg='this module only works on Layer 3 interfaces.') if jp_policy_in: if not jp_type_in: module.fail_json(msg='jp_type_in required when using jp_policy_in.') if jp_policy_out: if not jp_type_out: module.fail_json(msg='jp_type_out required when using jp_policy_out.') if neighbor_policy: if not neighbor_type: module.fail_json(msg='neighbor_type required when using neighbor_policy.') get_existing = get_pim_interface(module, interface) existing, jp_bidir, isauth = local_existing(get_existing) args = PARAM_TO_COMMAND_KEYMAP.keys() proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) if hello_interval: proposed['hello_interval'] = str(proposed['hello_interval'] * 1000) delta = dict(set(proposed.items()).difference(existing.items())) commands = [] if state == 'present': if delta: command = config_pim_interface(delta, existing, jp_bidir, isauth) if command: commands.append(command) elif state == 'default': defaults = config_pim_interface_defaults(existing, jp_bidir, isauth) if defaults: commands.append(defaults) elif state == 'absent': if existing.get('sparse') is True: delta['sparse'] = False # defaults is a list of commands defaults = config_pim_interface_defaults(existing, jp_bidir, isauth) if defaults: commands.append(defaults) command = config_pim_interface(delta, existing, jp_bidir, isauth) commands.append(command) if commands: commands.insert(0, ['interface {0}'.format(interface)]) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict( server_type=dict(type='str', choices=['radius', 'tacacs'], required=True), global_key=dict(type='str'), encrypt_type=dict(type='str', choices=['0', '7']), deadtime=dict(type='str'), server_timeout=dict(type='str'), directed_request=dict(type='str', choices=['enabled', 'disabled', 'default']), state=dict(choices=['default', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} server_type = module.params['server_type'] global_key = module.params['global_key'] encrypt_type = module.params['encrypt_type'] deadtime = module.params['deadtime'] server_timeout = module.params['server_timeout'] directed_request = module.params['directed_request'] state = module.params['state'] if encrypt_type and not global_key: module.fail_json(msg='encrypt_type must be used with global_key.') args = dict(server_type=server_type, global_key=global_key, encrypt_type=encrypt_type, deadtime=deadtime, server_timeout=server_timeout, directed_request=directed_request) proposed = dict((k, v) for k, v in args.items() if v is not None) existing = get_aaa_server_info(server_type, module) commands = [] if state == 'present': if deadtime: try: if int(deadtime) < 0 or int(deadtime) > 1440: raise ValueError except ValueError: module.fail_json( msg='deadtime must be an integer between 0 and 1440') if server_timeout: try: if int(server_timeout) < 1 or int(server_timeout) > 60: raise ValueError except ValueError: module.fail_json( msg='server_timeout must be an integer between 1 and 60') delta = dict(set(proposed.items()).difference( existing.items())) if delta: command = config_aaa_server(delta, server_type) if command: commands.append(command) elif state == 'default': for key, value in proposed.items(): if key != 'server_type' and value != 'default': module.fail_json( msg='Parameters must be set to "default"' 'when state=default') command = default_aaa_server(existing, proposed, server_type) if command: commands.append(command) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict( community=dict(required=True, type='str'), access=dict(choices=['ro', 'rw']), group=dict(type='str'), acl=dict(type='str'), state=dict(choices=['absent', 'present'], default='present'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, required_one_of=[['access', 'group']], mutually_exclusive=[['access', 'group']], supports_check_mode=True) warnings = list() check_args(module, warnings) results = {'changed': False, 'commands': [], 'warnings': warnings} access = module.params['access'] group = module.params['group'] community = module.params['community'] acl = module.params['acl'] state = module.params['state'] if access: if access == 'ro': group = 'network-operator' elif access == 'rw': group = 'network-admin' # group check - ensure group being configured exists on the device configured_groups = get_snmp_groups(module) if group not in configured_groups: module.fail_json(msg="Group not on switch. Please add before moving forward") existing = get_snmp_community(module, community) args = dict(group=group, acl=acl) proposed = dict((k, v) for k, v in args.items() if v is not None) delta = dict(set(proposed.items()).difference(existing.items())) commands = [] if state == 'absent': if existing: command = "no snmp-server community {0}".format(community) commands.append(command) elif state == 'present': if delta: command = config_snmp_community(dict(delta), community) commands.append(command) cmds = flatten_list(commands) if cmds: results['changed'] = True if not module.check_mode: load_config(module, cmds) if 'configure' in cmds: cmds.pop(0) results['commands'] = cmds module.exit_json(**results)
def main(): argument_spec = dict( asn=dict(required=True, type='str'), vrf=dict(required=False, type='str', default='default'), safi=dict(required=True, type='str', choices=['unicast', 'multicast', 'evpn']), afi=dict(required=True, type='str', choices=['ipv4', 'ipv6', 'vpnv4', 'vpnv6', 'l2vpn']), additional_paths_install=dict(required=False, type='bool'), additional_paths_receive=dict(required=False, type='bool'), additional_paths_selection=dict(required=False, type='str'), additional_paths_send=dict(required=False, type='bool'), advertise_l2vpn_evpn=dict(required=False, type='bool'), client_to_client=dict(required=False, type='bool'), dampen_igp_metric=dict(required=False, type='str'), dampening_state=dict(required=False, type='bool'), dampening_half_time=dict(required=False, type='str'), dampening_max_suppress_time=dict(required=False, type='str'), dampening_reuse_time=dict(required=False, type='str'), dampening_routemap=dict(required=False, type='str'), dampening_suppress_time=dict(required=False, type='str'), default_information_originate=dict(required=False, type='bool'), default_metric=dict(required=False, type='str'), distance_ebgp=dict(required=False, type='str'), distance_ibgp=dict(required=False, type='str'), distance_local=dict(required=False, type='str'), inject_map=dict(required=False, type='list'), maximum_paths=dict(required=False, type='str'), maximum_paths_ibgp=dict(required=False, type='str'), networks=dict(required=False, type='list'), next_hop_route_map=dict(required=False, type='str'), redistribute=dict(required=False, type='list'), suppress_inactive=dict(required=False, type='bool'), table_map=dict(required=False, type='str'), table_map_filter=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present', required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule( argument_spec=argument_spec, required_together=[DAMPENING_PARAMS, ['distance_ibgp', 'distance_ebgp', 'distance_local']], supports_check_mode=True, ) warnings = list() check_args(module, warnings) result = dict(changed=False, warnings=warnings) state = module.params['state'] if module.params['dampening_routemap']: for param in DAMPENING_PARAMS: if module.params[param]: module.fail_json(msg='dampening_routemap cannot be used with' ' the {0} param'.format(param)) if module.params['advertise_l2vpn_evpn']: if module.params['vrf'] == 'default': module.fail_json(msg='It is not possible to advertise L2VPN ' 'EVPN in the default VRF. Please specify ' 'another one.', vrf=module.params['vrf']) if module.params['table_map_filter'] and not module.params['table_map']: module.fail_json(msg='table_map param is needed when using' ' table_map_filter filter.') args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args, warnings) if existing.get('asn') and state == 'present': if existing.get('asn') != module.params['asn']: module.fail_json(msg='Another BGP ASN already exists.', proposed_asn=module.params['asn'], existing_asn=existing.get('asn')) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) for arg in ['networks', 'inject_map']: if proposed_args.get(arg): if proposed_args[arg][0] == 'default': proposed_args[arg] = 'default' proposed = {} for key, value in proposed_args.items(): if key not in ['asn', 'vrf']: if str(value).lower() == 'default': value = PARAM_TO_DEFAULT_KEYMAP.get(key, 'default') if existing.get(key) != value: proposed[key] = value candidate = CustomNetworkConfig(indent=3) if state == 'present': state_present(module, existing, proposed, candidate) elif state == 'absent' and existing: state_absent(module, candidate) if candidate: candidate = candidate.items_text() load_config(module, candidate) result['changed'] = True result['commands'] = candidate else: result['commands'] = [] module.exit_json(**result)
def main(): spec = dict( gather_subset=dict(default=['!config'], type='list') ) spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) warnings = list() check_args(module, warnings) gather_subset = module.params['gather_subset'] runable_subsets = set() exclude_subsets = set() for subset in gather_subset: if subset == 'all': runable_subsets.update(VALID_SUBSETS) continue if subset.startswith('!'): subset = subset[1:] if subset == 'all': exclude_subsets.update(VALID_SUBSETS) continue exclude = True else: exclude = False if subset not in VALID_SUBSETS: module.fail_json(msg='Bad subset') if exclude: exclude_subsets.add(subset) else: runable_subsets.add(subset) if not runable_subsets: runable_subsets.update(VALID_SUBSETS) runable_subsets.difference_update(exclude_subsets) runable_subsets.add('default') facts = dict() facts['gather_subset'] = list(runable_subsets) instances = list() for key in runable_subsets: instances.append(FACT_SUBSETS[key](module)) for inst in instances: inst.populate() facts.update(inst.facts) warnings.extend(inst.warnings) ansible_facts = dict() for key, value in iteritems(facts): # this is to maintain capability with nxos_facts 2.1 if key.startswith('_'): ansible_facts[key[1:]] = value else: key = 'ansible_net_%s' % key ansible_facts[key] = value module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
def main(): """ main entry point for module execution """ argument_spec = dict( aggregate=dict(type='list', no_log=True, aliases=['collection', 'users']), name=dict(), configured_password=dict(no_log=True), update_password=dict(default='always', choices=['on_create', 'always']), roles=dict(type='list', aliases=['role']), sshkey=dict(), purge=dict(type='bool', default=False), state=dict(default='present', choices=['present', 'absent']) ) argument_spec.update(nxos_argument_spec) mutually_exclusive = [('name', 'aggregate')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) result = {'changed': False} warnings = list() if module.params['password'] and not module.params['configured_password']: warnings.append( 'The "password" argument is used to authenticate the current connection. ' + 'To set a user password use "configured_password" instead.' ) check_args(module, warnings) result['warnings'] = warnings want = map_params_to_obj(module) have = map_config_to_obj(module) commands = map_obj_to_commands(update_objects(want, have), module) if module.params['purge']: want_users = [x['name'] for x in want] have_users = [x['name'] for x in have] for item in set(have_users).difference(want_users): if item != 'admin': commands.append('no username %s' % item) result['commands'] = commands # the nxos cli prevents this by rule so capture it and display # a nice failure message if 'no username admin' in commands: module.fail_json(msg='cannot delete the `admin` account') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True module.exit_json(**result)