def main(): module = AnsibleModule(argument_spec=dict( path=dict(default='startup-config'), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(type='str'), ), supports_check_mode=False) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) path = module.params['path'] device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) if path != 'startup-config': if ':' not in path: msg = ('invalid format for path. Requires ":" ' + 'Example- bootflash:config.cfg' + 'or bootflash:configs/test.cfg') module.fail_json(msg=msg) complete_save, changed = save_config(device, path, module) results = {} results['path'] = path results['status'] = complete_save results['changed'] = changed module.exit_json(**results)
def get_lldp_neighbor(ip, username, password): """ get_lldp_neighbor - retreives lldp neighbor information from pycsco library Inputs: ip (str) The dotted ip address represented as a string username (str) The username for ip password (str) The password for this username Outputs: neigh_count (int) The number of neightbors list_dict (list of neigh_count) The info dictionary list config_list (list of list) The config info (Device, local_interface, remote_interface) """ switch = Device(ip=ip, username=username, password=password) # Show neighbors, return json lldp_info = switch.show('show lldp neighbors', fmat='json') # load data dict lldp_dict = json.loads(lldp_info[1]) # Get count neigh_count = lldp_dict['ins_api']['outputs']['output']['body'][ 'neigh_count'] list_dict = lldp_dict['ins_api']['outputs']['output']['body'][ 'TABLE_nbor']['ROW_nbor'] # get lldp_config info in json format lldp_config = switch.config('show lldp neighbors', fmat='json') # Load a json dictionary config_dict = json.loads(lldp_config[1]) #print 'Config ip %s \n= %s' %(ip,config_dict) config_list = parse_config( config_dict['ins_api']['outputs']['output']['body'], neigh_count) # Return it return (neigh_count, list_dict, config_list)
def main(): module = AnsibleModule(argument_spec=dict( type=dict(choices=['cdp', 'lldp'], required=True), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(type='str'), ), supports_check_mode=False) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) neighbor_type = module.params['type'].lower() device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) if neighbor_type == 'lldp': neighbors = get_lldp_neighbors(device, module) elif neighbor_type == 'cdp': neighbors = get_cdp_neighbors(device, module) results = {} results['neighbors'] = neighbors module.exit_json(ansible_facts=results)
def main(): module = AnsibleModule(argument_spec=dict( vlan_id=dict(required=False, type='str'), vlan_range=dict(required=False), name=dict(required=False), vlan_state=dict(choices=['active', 'suspend'], required=False), state=dict(choices=['present', 'absent'], default='present'), admin_state=dict(choices=['up', 'down'], required=False), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(no_log=True, type='str'), ), mutually_exclusive=[['vlan_range', 'name'], ['vlan_id', 'vlan_range']], supports_check_mode=True) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) vlan_range = module.params['vlan_range'] vlan_id = module.params['vlan_id'] name = module.params['name'] vlan_state = module.params['vlan_state'] admin_state = module.params['admin_state'] state = module.params['state'] device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) changed = False if vlan_id: if not vlan_id.isdigit(): module.fail_json(msg='vlan_id must be a valid VLAN ID') args = dict(name=name, vlan_state=vlan_state, admin_state=admin_state) proposed = dict((k, v) for k, v in args.iteritems() if v is not None) proposed_vlans_list = numerical_sort( vlan_range_to_list(vlan_id or vlan_range)) existing_vlans_list = numerical_sort(get_list_of_vlans(device, module)) commands = [] existing = None if vlan_range: if state == 'present': # These are all of the VLANs being proposed that don't # already exist on the switch vlans_delta = list( set(proposed_vlans_list).difference(existing_vlans_list)) commands = build_commands(vlans_delta, state) elif state == 'absent': # VLANs that are common between what is being proposed and # what is on the switch vlans_common = list( set(proposed_vlans_list).intersection(existing_vlans_list)) commands = build_commands(vlans_common, state) else: existing = get_vlan(device, vlan_id, module) if state == 'absent': if existing: commands = ['no vlan ' + vlan_id] elif state == 'present': delta = dict( set(proposed.iteritems()).difference(existing.iteritems())) if delta or not existing: commands = get_vlan_config_commands(delta, vlan_id) end_state = existing end_state_vlans_list = existing_vlans_list cmds = command_list_to_string(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: try: device.config(cmds) except CLIError as clie: module.fail_json(msg='Error sending CLI commands', error=str(clie), commands=cmds) changed = True end_state_vlans_list = numerical_sort( get_list_of_vlans(device, module)) if vlan_id: end_state = get_vlan(device, vlan_id, module) results = {} results['proposed_vlans_list'] = proposed_vlans_list results['existing_vlans_list'] = existing_vlans_list results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['end_state_vlans_list'] = end_state_vlans_list results['state'] = state results['commands'] = cmds results['changed'] = changed module.exit_json(**results)
def main(): module = AnsibleModule(argument_spec=dict( interface=dict(required=True), admin_state=dict(default='up', choices=['up', 'down']), duplex=dict(default=None), state=dict(default='present', choices=['present', 'absent', 'default']), speed=dict(default=None), description=dict(default=None), mode=dict(choices=['layer2', 'layer3']), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(no_log=True, type='str'), ), supports_check_mode=True) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) interface = module.params['interface'].lower() duplex = module.params['duplex'] admin_state = module.params['admin_state'] speed = module.params['speed'] description = module.params['description'] mode = module.params['mode'] state = module.params['state'] device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) changed = False args = dict(interface=interface, admin_state=admin_state, description=description, duplex=duplex, speed=speed, mode=mode) intf_type = get_interface_type(interface) normalized_interface = normalize_interface(interface) if normalized_interface == 'Vlan1' and state == 'absent': module.fail_json(msg='CANNOT REMOVE VLAN1. Doh!') elif intf_type in ['management']: if state in ['absent', 'default']: module.fail_json(msg='CANNOT DEFAULT MGMT0- USED BY NXAPI') if intf_type == 'svi': feature = 'interface-vlan' available_features = get_available_features(device, feature, module) svi_state = available_features[feature] if svi_state == 'disabled': module.fail_json( msg='SVI (interface-vlan) feature needs to be enabled first', ) if intf_type == 'unknown': module.fail_json(msg='unknown interface type found-1', interface=interface) existing = get_existing(device, normalized_interface, module) proposed = get_proposed(existing, normalized_interface, args) delta = dict() commands = [] is_default = is_default_interface(device, normalized_interface, module) if state == 'absent': if intf_type in ['svi', 'loopback', 'portchannel']: if is_default != 'DNE': cmds = ['no interface {0}'.format(normalized_interface)] commands.append(cmds) elif intf_type in ['ethernet']: if is_default is False: cmds = ['default interface {0}'.format(normalized_interface)] commands.append(cmds) elif state == 'present': if not existing: cmds = get_interface_config_commands(device, proposed, normalized_interface, existing) commands.append(cmds) else: delta = dict( set(proposed.iteritems()).difference(existing.iteritems())) if delta: cmds = get_interface_config_commands(device, delta, normalized_interface, existing) commands.append(cmds) elif state == 'default': if is_default is False: cmds = ['default interface {0}'.format(normalized_interface)] commands.append(cmds) elif is_default == 'DNE': module.exit_json(msg='interface you are trying to default does' ' not exist') cmds = nested_command_list_to_string(commands) end_state = existing if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: device.config(cmds) if delta.get('mode'): # or delta.get('admin_state'): # if the mode changes from L2 to L3, the admin state # changes after the API call, so adding a second API # call just for admin state and using it for a change # in admin state or mode. admin_state = delta.get('admin_state') or admin_state command = get_admin_state(delta, normalized_interface, admin_state) device.config('interface {0} ; {1} ;'.format( normalized_interface, command)) cmds += command changed = True end_state = get_existing(device, normalized_interface, module) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['state'] = state results['commands'] = cmds results['changed'] = changed module.exit_json(**results)
def main(): module = AnsibleModule(argument_spec=dict( detail=dict(choices=BOOLEANS, type='bool'), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(type='str'), ), supports_check_mode=False) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) detail = module.params['detail'] device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) show_version_command = 'show version' interface_command = 'show interface status' show_module_command = 'show module' show_environment_command = 'show environment' show_vlan_command = 'show vlan brief' # Get 'show version' facts. show_version_body = parsed_data_from_device(device, show_version_command, module) show_version = get_show_version_facts(show_version_body) # Get interfaces facts. interface_body = parsed_data_from_device(device, interface_command, module) detailed_list, interface_list = get_interface_facts(interface_body, detail) # Get module facts. show_module_body = parsed_data_from_device(device, show_module_command, module) show_module = get_show_module_facts(show_module_body) # Get environment facts. show_environment_body = parsed_data_from_device(device, show_environment_command, module) powersupply = get_powersupply_facts(show_environment_body) fan = get_fan_facts(show_environment_body) # Get vlans facts. show_vlan_body = parsed_data_from_device(device, show_vlan_command, module) vlan = get_vlan_facts(show_vlan_body) facts = dict(interfaces=interface_list, module=show_module, power_supply_info=powersupply, fan_info=fan, vlan_list=vlan) facts.update(show_version) if detail: facts['detailed_interface'] = detailed_list module.exit_json(ansible_facts=facts)
def main(): module = AnsibleModule(argument_spec=dict( command=dict(required=False), command_list=dict(required=False), text=dict(choices=BOOLEANS, type='bool'), type=dict(choices=['show', 'config'], required=True), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(type='str')), required_one_of=[['command', 'command_list']], mutually_exclusive=[['command', 'command_list']], supports_check_mode=False) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) command = module.params['command'] command_list = module.params['command_list'] text = module.params['text'] or None cmd_type = module.params['type'].lower() device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) changed = False cmds = '' if command: if isinstance(command, str): cmds = command_list_to_string([command]) else: module.fail_json(msg='Only strings are supported with "command"' '\nIf you want to use a list, use the param' '" command_list" instead.') elif command_list: if isinstance(command_list, list): cmds = command_list_to_string(command_list) else: module.fail_json(msg='Only Lists are supported with "command_list"' '\nIf you want to send a single command,' 'use the param "command" instead.') proposed = dict(commands=cmds, text=text, cmd_type=cmd_type) if cmds: if cmd_type == 'show': response = send_show_command(device, cmds, module, text) elif cmd_type == 'config': changed = True response = send_config_command(device, cmds, module) else: module.fail_json(msg='no commands to send. check format') results = {} results['changed'] = changed results['proposed'] = proposed results['commands'] = cmds results['response'] = response module.exit_json(**results)
def main(): module = AnsibleModule(argument_spec=dict( interface=dict(required=True, type='str'), mode=dict(choices=['access', 'trunk']), access_vlan=dict(type='str', required=False), native_vlan=dict(type='str', required=False), trunk_vlans=dict(type='str', required=False), state=dict(choices=['absent', 'present', 'unconfigured'], default='present'), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(required=False), password=dict(required=False), ), mutually_exclusive=[['access_vlan', 'trunk_vlans'], ['access_vlan', 'native_vlan']], supports_check_mode=True) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) 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'] device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) args = dict(interface=interface, mode=mode, access_vlan=access_vlan, native_vlan=native_vlan, trunk_vlans=trunk_vlans) proposed = dict((k, v) for k, v in args.iteritems() 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(device, 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(device, 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(device, 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') current_vlans = get_list_of_vlans(device, 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) if trunk_vlans: trunk_vlans_list = vlan_range_to_list(trunk_vlans) existing_trunks_list = vlan_range_to_list((existing['trunk_vlans'])) existing['trunk_vlans_list'] = existing_trunks_list proposed['trunk_vlans_list'] = trunk_vlans_list changed = False commands = [] if state == 'present': command = get_switchport_config_commands(interface, existing, proposed) 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) commands.append(command) if trunk_vlans: existing.pop('trunk_vlans_list') proposed.pop('trunk_vlans_list') end_state = existing cmds = nested_command_list_to_string(commands) if cmds: if module.check_mode: module.exit_json(changed=True, commands=cmds) else: changed = True device.config(cmds) end_state = get_switchport(device, interface, module) results = {} results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['state'] = state results['commands'] = cmds results['changed'] = changed module.exit_json(**results)
direction = proposed.get("direction") if direction == "egress": cmd = "no ip access-group {0} {1}".format(proposed.get("name"), "out") elif direction == "ingress": cmd = "no ip access-group {0} {1}".format(proposed.get("name"), "in") commands.append(cmd) return commands if __name__ == "__main__": import json from pycsco.nxos.device import Device device = Device(ip="n9396-2", username="******", password="******", protocol="http") seq_number = "40" data = get_acl_interface(device, "MYACL") print(json.dumps(data, indent=4)) # print json.dumps(ace, indent=4) """ core = config_core_acl(ace, {}) if ace.get('options'): print core + ' ' + config_acl_options(ace.get('options')) else: print core """
def main(): results = {} module = AnsibleModule(argument_spec=dict( feature=dict(type='str', required=True), state=dict(choices=['enabled', 'disabled'], required=True), protocol=dict(choices=['http', 'https'], default='http'), port=dict(required=False, type='int', default=None), host=dict(required=True), username=dict(type='str'), password=dict(no_log=True, type='str'), ), supports_check_mode=True) if not HAS_PYCSCO: module.fail_json(msg='There was a problem loading pycsco') auth = Auth(vendor='cisco', model='nexus') username = module.params['username'] or auth.username password = module.params['password'] or auth.password protocol = module.params['protocol'] port = module.params['port'] host = socket.gethostbyname(module.params['host']) feature = module.params['feature'].lower() state = module.params['state'].lower() device = Device(ip=host, username=username, password=password, protocol=protocol, port=port) available_features = get_available_features(device, feature, module) if feature not in available_features.keys(): 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, feature) if cmds: changed = True device.config(cmds) updated_features = get_available_features(device, feature, module) existstate = updated_features[feature] end_state = dict(state=existstate) results['proposed'] = proposed results['existing'] = existing results['end_state'] = end_state results['state'] = state results['commands'] = cmds results['changed'] = changed results['feature'] = feature module.exit_json(**results)
def get_commands_remove_vrrp(group): """Gets commands to remove an hsrp on an interface Args: group (str): hsrp group Returns: list: ordered list of commands to remove the hsrp group Note: Specific for Ansible module(s). Not to be called otherwise. """ commands = [] commands.append('no vrrp ' + group) return commands if __name__ == "__main__": device = Device(ip='n9396-2', username='******', password='******', protocol='http') interface = 'vlan100' test = get_vrrp_existing(device, interface) import json print json.dumps(test, indent=4)
print chr(0x1b) + '[2J' # clear the screen print "Enter your credentials:" username = raw_input('Username: '******''' Ask the user which function they want to launch. ''' print chr(0x1b) + '[2J' # clear the screen print "\ Make a selection:\n \ [1] Device Info\n \ [2] IP Arp Table\n \