def map_params_to_obj(module): obj = [] aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() d["group"] = str(d["group"]) d["min_links"] = str(d["min_links"]) if d["members"]: d["members"] = [normalize_interface(i) for i in d["members"]] obj.append(d) else: members = None if module.params["members"]: members = [ normalize_interface(i) for i in module.params["members"] ] obj.append({ "group": str(module.params["group"]), "mode": module.params["mode"], "min_links": str(module.params["min_links"]), "members": members, "state": module.params["state"], }) return obj
def map_params_to_obj(module): obj = [] aggregate = module.params.get('aggregate') if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() d['group'] = str(d['group']) d['min_links'] = str(d['min_links']) if d['members']: d['members'] = [normalize_interface(i) for i in d['members']] obj.append(d) else: members = None if module.params['members']: members = [ normalize_interface(i) for i in module.params['members'] ] obj.append({ 'group': str(module.params['group']), 'mode': module.params['mode'], 'min_links': str(module.params['min_links']), 'members': members, 'state': module.params['state'] }) return obj
def map_params_to_obj(module): obj = [] aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() name = d["name"] d["name"] = normalize_interface(name) obj.append(d) else: obj.append( { "name": normalize_interface(module.params["name"]), "ipv4": module.params["ipv4"], "ipv6": module.params["ipv6"], "state": module.params["state"], }, ) return obj
def get_members(channel): members = [] if "TABLE_member" in channel.keys(): interfaces = channel["TABLE_member"]["ROW_member"] else: return list() if isinstance(interfaces, dict): members.append(normalize_interface(interfaces.get("port"))) elif isinstance(interfaces, list): for i in interfaces: members.append(normalize_interface(i.get("port"))) return members
def get_members(channel): members = [] if 'TABLE_member' in channel.keys(): interfaces = channel['TABLE_member']['ROW_member'] else: return list() if isinstance(interfaces, dict): members.append(normalize_interface(interfaces.get('port'))) elif isinstance(interfaces, list): for i in interfaces: members.append(normalize_interface(i.get('port'))) return members
def map_params_to_obj(module): obj = [] aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() name = d["name"] d["name"] = normalize_interface(name) obj.append(d) else: obj.append( { "name": normalize_interface(module.params["name"]), "description": module.params["description"], "speed": module.params["speed"], "mode": module.params["mode"], "mtu": module.params["mtu"], "duplex": module.params["duplex"], "ip_forward": module.params["ip_forward"], "fabric_forwarding_anycast_gateway": module.params["fabric_forwarding_anycast_gateway"], "admin_state": module.params["admin_state"], "state": module.params["state"], "interface_type": module.params["interface_type"], "tx_rate": module.params["tx_rate"], "rx_rate": module.params["rx_rate"], "neighbors": module.params["neighbors"], }, ) return obj
def parse_vlan_non_structured(module, netcfg, vlans): objs = list() for vlan in vlans: vlan_match = re.search(r'(\d+)', vlan, re.M) if vlan_match: obj = {} vlan_id = vlan_match.group(1) obj['vlan_id'] = str(vlan_id) name_match = re.search(r'{0}\s*(\S+)'.format(vlan_id), vlan, re.M) if name_match: name = name_match.group(1) obj['name'] = name state_match = re.search( r'{0}\s*{1}\s*(\S+)'.format(vlan_id, re.escape(name)), vlan, re.M) if state_match: vlan_state_match = state_match.group(1) if vlan_state_match == 'suspended': vlan_state = 'suspend' admin_state = 'up' elif vlan_state_match == 'sus/lshut': vlan_state = 'suspend' admin_state = 'down' if vlan_state_match == 'active': vlan_state = 'active' admin_state = 'up' if vlan_state_match == 'act/lshut': vlan_state = 'active' admin_state = 'down' obj['vlan_state'] = vlan_state obj['admin_state'] = admin_state vlan = ','.join(vlan.splitlines()) interfaces = list() intfs_match = re.search( r'{0}\s*{1}\s*{2}\s*(.*)'.format( vlan_id, re.escape(name), vlan_state_match), vlan, re.M) if intfs_match: intfs = intfs_match.group(1) intfs = intfs.split() for i in intfs: intf = normalize_interface(i.strip(',')) interfaces.append(intf) if interfaces: obj['interfaces'] = interfaces else: obj['interfaces'] = None config = parse_vlan_config(netcfg, vlan_id) obj['mode'] = parse_mode(config) obj['mapped_vni'] = parse_vni(config) objs.append(obj) return objs
def map_params_to_obj(module): obj = [] aggregate = module.params.get('aggregate') if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() name = d['name'] d['name'] = normalize_interface(name) obj.append(d) else: obj.append({ 'name': normalize_interface(module.params['name']), 'description': module.params['description'], 'speed': module.params['speed'], 'mode': module.params['mode'], 'mtu': module.params['mtu'], 'duplex': module.params['duplex'], 'ip_forward': module.params['ip_forward'], 'fabric_forwarding_anycast_gateway': module.params['fabric_forwarding_anycast_gateway'], 'admin_state': module.params['admin_state'], 'state': module.params['state'], 'interface_type': module.params['interface_type'], 'tx_rate': module.params['tx_rate'], 'rx_rate': module.params['rx_rate'], 'neighbors': module.params['neighbors'] }) return obj
def map_params_to_obj(module): obj = [] aggregate = module.params.get('aggregate') if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() name = d['name'] d['name'] = normalize_interface(name) obj.append(d) else: obj.append({ 'name': normalize_interface(module.params['name']), 'ipv4': module.params['ipv4'], 'ipv6': module.params['ipv6'], 'state': module.params['state'] }) return obj
def map_config_to_obj(want, module): objs = list() netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) for w in want: parents = ["interface {0}".format(w["name"])] config = netcfg.get_section(parents) obj = dict(name=None, ipv4=None, ipv6=[]) if config: match_name = re.findall(r"interface (\S+)", config, re.M) if match_name: obj["name"] = normalize_interface(match_name[0]) match_ipv4 = re.findall(r"ip address (\S+)", config, re.M) if match_ipv4: obj["ipv4"] = match_ipv4[0] match_ipv6 = re.findall(r"ipv6 address (\S+)", config, re.M) if match_ipv6: obj["ipv6"] = match_ipv6 objs.append(obj) return objs
def map_params_to_obj(module): obj = [] if "aggregate" in module.params and module.params["aggregate"]: args = { "dest": "", "remote_server": "", "use_vrf": "", "name": "", "facility": "", "dest_level": "", "facility_level": "", "interface": "", "facility_link_status": None, "event": None, "file_size": None, "message": None, "timestamp": None, } for c in module.params["aggregate"]: d = c.copy() for key in args: if key not in d: d[key] = None if d["dest_level"] is not None: d["dest_level"] = str(d["dest_level"]) if d["facility_level"] is not None: d["facility_level"] = str(d["facility_level"]) if d["interface"]: d["interface"] = normalize_interface(d["interface"]) if "state" not in d: d["state"] = module.params["state"] if d["file_size"]: d["file_size"] = str(d["file_size"]) obj.append(d) else: dest_level = None facility_level = None file_size = None if module.params["dest_level"] is not None: dest_level = str(module.params["dest_level"]) if module.params["facility_level"] is not None: facility_level = str(module.params["facility_level"]) if module.params["file_size"] is not None: file_size = str(module.params["file_size"]) obj.append( { "dest": module.params["dest"], "remote_server": module.params["remote_server"], "use_vrf": module.params["use_vrf"], "name": module.params["name"], "facility": module.params["facility"], "dest_level": dest_level, "facility_level": facility_level, "interface": normalize_interface(module.params["interface"]), "state": module.params["state"], "facility_link_status": module.params["facility_link_status"], "event": module.params["event"], "message": module.params["interface_message"], "file_size": file_size, "timestamp": module.params["timestamp"], }, ) return obj
def normalize(interfaces): normalized = None if interfaces: normalized = [normalize_interface(i) for i in interfaces] return normalized
def main(): argument_spec = dict( vrf=dict(required=True), interface=dict(type="str", required=True), state=dict(default="present", choices=["present", "absent"], required=False), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() results = {"changed": False, "commands": [], "warnings": warnings} vrf = module.params["vrf"] interface = normalize_interface(module.params["interface"]) state = module.params["state"] device_info = get_capabilities(module) network_api = device_info.get("network_api", "nxapi") current_vrfs = get_vrf_list(module) if vrf not in current_vrfs: warnings.append("The VRF is not present/active on the device. " "Use nxos_vrf to fix this.") intf_type = get_interface_type(interface) if intf_type != "ethernet" and network_api == "cliconf": if is_default(interface, module) == "DNE": module.fail_json(msg="interface does not exist on switch. Verify " "switch platform or create it first with " "nxos_interface if it's a logical interface") mode = get_interface_mode(interface, intf_type, module) if mode == "layer2": module.fail_json(msg="Ensure interface is a Layer 3 port before " "configuring a VRF on an interface. You can " "use nxos_interface") current_vrf = get_interface_info(interface, module) existing = dict(interface=interface, vrf=current_vrf) changed = False if not existing["vrf"]: pass elif vrf != existing["vrf"] and state == "absent": module.fail_json( msg="The VRF you are trying to remove " "from the interface does not exist " "on that interface.", interface=interface, proposed_vrf=vrf, existing_vrf=existing["vrf"], ) commands = [] if existing: if state == "absent": if existing and vrf == existing["vrf"]: command = "no vrf member {0}".format(vrf) commands.append(command) elif state == "present": if existing["vrf"] != vrf: command = "vrf member {0}".format(vrf) commands.append(command) if commands: commands.insert(0, "interface {0}".format(interface)) if commands: if module.check_mode: module.exit_json(changed=True, commands=commands) else: load_config(module, commands) changed = True if "configure" in commands: commands.pop(0) results["commands"] = commands results["changed"] = changed module.exit_json(**results)
def parse_vlan_non_structured(module, netcfg, vlans): objs = list() for vlan in vlans: vlan_match = re.search(r"(\d+)", vlan, re.M) if vlan_match: obj = {} vlan_id = vlan_match.group(1) obj["vlan_id"] = str(vlan_id) name_match = re.search(r"{0}\s*(\S+)".format(vlan_id), vlan, re.M) if name_match: name = name_match.group(1) obj["name"] = name state_match = re.search( r"{0}\s*{1}\s*(\S+)".format(vlan_id, re.escape(name)), vlan, re.M, ) if state_match: vlan_state_match = state_match.group(1) if vlan_state_match == "suspended": vlan_state = "suspend" admin_state = "up" elif vlan_state_match == "sus/lshut": vlan_state = "suspend" admin_state = "down" if vlan_state_match == "active": vlan_state = "active" admin_state = "up" if vlan_state_match == "act/lshut": vlan_state = "active" admin_state = "down" obj["vlan_state"] = vlan_state obj["admin_state"] = admin_state vlan = ",".join(vlan.splitlines()) interfaces = list() intfs_match = re.search( r"{0}\s*{1}\s*{2}\s*(.*)".format( vlan_id, re.escape(name), vlan_state_match), vlan, re.M, ) if intfs_match: intfs = intfs_match.group(1) intfs = intfs.split() for i in intfs: intf = normalize_interface(i.strip(",")) interfaces.append(intf) if interfaces: obj["interfaces"] = interfaces else: obj["interfaces"] = None config = parse_vlan_config(netcfg, vlan_id) obj["mode"] = parse_mode(config) obj["mapped_vni"] = parse_vni(config) objs.append(obj) return objs
def map_config_to_obj(want, module): objs = list() for w in want: obj = dict( name=None, description=None, admin_state=None, speed=None, mtu=None, mode=None, duplex=None, interface_type=None, ip_forward=None, fabric_forwarding_anycast_gateway=None, ) if not w["name"]: return obj command = "show interface {0}".format(w["name"]) try: body = execute_show_command(command, module)[0] except IndexError: return list() if body: try: interface_table = body["TABLE_interface"]["ROW_interface"] except (KeyError, TypeError): return list() if interface_table: if interface_table.get("eth_mode") == "fex-fabric": module.fail_json( msg= 'nxos_interface does not support interfaces with mode "fex-fabric"' ) intf_type = get_interface_type(w["name"]) if intf_type in ["portchannel", "ethernet"]: mode = interface_table.get("eth_mode") if mode in ("access", "trunk", "dot1q-tunnel"): obj["mode"] = "layer2" elif mode in ("routed", "layer3"): obj["mode"] = "layer3" else: obj["mode"] = "layer3" if intf_type == "ethernet": obj["name"] = normalize_interface( interface_table.get("interface")) obj["admin_state"] = interface_table.get("admin_state") obj["description"] = interface_table.get("desc") obj["mtu"] = interface_table.get("eth_mtu") obj["duplex"] = interface_table.get("eth_duplex") command = "show run interface {0}".format(obj["name"]) body = execute_show_command(command, module)[0] speed_match = re.search(r"speed (\d+)", body) if speed_match is None: obj["speed"] = "auto" else: obj["speed"] = speed_match.group(1) duplex_match = re.search(r"duplex (\S+)", body) if duplex_match is None: obj["duplex"] = "auto" else: obj["duplex"] = duplex_match.group(1) if "ip forward" in body: obj["ip_forward"] = "enable" else: obj["ip_forward"] = "disable" elif intf_type == "svi": obj["name"] = normalize_interface( interface_table.get("interface")) attributes = get_vlan_interface_attributes( obj["name"], intf_type, module) obj["admin_state"] = str( attributes.get("admin_state", "nxapibug")) obj["description"] = str( attributes.get("description", "nxapi_bug")) obj["mtu"] = interface_table.get("svi_mtu") command = "show run interface {0}".format(obj["name"]) body = execute_show_command(command, module)[0] if "ip forward" in body: obj["ip_forward"] = "enable" else: obj["ip_forward"] = "disable" if "fabric forwarding mode anycast-gateway" in body: obj["fabric_forwarding_anycast_gateway"] = True else: obj["fabric_forwarding_anycast_gateway"] = False elif intf_type in ("loopback", "management", "nve"): obj["name"] = normalize_interface( interface_table.get("interface")) obj["admin_state"] = interface_table.get("admin_state") if obj["admin_state"] is None and intf_type == "loopback": # Some platforms don't have the 'admin_state' key. # For loopback interfaces it's safe to use the # 'state' key instead. obj["admin_state"] = interface_table.get("state") obj["description"] = interface_table.get("desc") elif intf_type == "portchannel": obj["name"] = normalize_interface( interface_table.get("interface")) obj["admin_state"] = interface_table.get("admin_state") obj["description"] = interface_table.get("desc") obj["mtu"] = interface_table.get("eth_mtu") if obj["admin_state"] is None: # Some nxos platforms do not have the 'admin_state' key. # Use the 'state_rsn_desc' key instead to determine the # admin state of the interface. state_description = interface_table.get("state_rsn_desc") if state_description == "Administratively down": obj["admin_state"] = "down" elif state_description is not None: obj["admin_state"] = "up" objs.append(obj) return objs
def map_obj_to_commands(updates, module): commands = list() commands2 = list() want, have = updates args = ("speed", "description", "duplex", "mtu") for w in want: name = w["name"] mode = w["mode"] ip_forward = w["ip_forward"] fabric_forwarding_anycast_gateway = w[ "fabric_forwarding_anycast_gateway"] admin_state = w["admin_state"] state = w["state"] interface_type = w["interface_type"] del w["state"] if name: w["interface_type"] = None if interface_type: obj_in_have = {} if state in ("present", "default"): module.fail_json( msg= "The interface_type param can be used only with state absent." ) else: obj_in_have = search_obj_in_list(name, have) is_default = is_default_interface(name, module) if name: interface = "interface " + name if state == "absent": if obj_in_have: commands.append("no interface {0}".format(name)) elif interface_type and not obj_in_have: intfs = get_interfaces_dict(module)[interface_type] cmds = get_interface_type_removed_cmds(intfs) commands.extend(cmds) elif state == "present": if obj_in_have: # Don't run switchport command for loopback and svi interfaces if get_interface_type(name) in ("ethernet", "portchannel"): if mode == "layer2" and mode != obj_in_have.get("mode"): add_command_to_interface(interface, "switchport", commands) elif mode == "layer3" and mode != obj_in_have.get("mode"): add_command_to_interface(interface, "no switchport", commands) if admin_state == "up" and admin_state != obj_in_have.get( "admin_state"): add_command_to_interface(interface, "no shutdown", commands) elif admin_state == "down" and admin_state != obj_in_have.get( "admin_state"): add_command_to_interface(interface, "shutdown", commands) if ip_forward == "enable" and ip_forward != obj_in_have.get( "ip_forward"): add_command_to_interface(interface, "ip forward", commands) elif ip_forward == "disable" and ip_forward != obj_in_have.get( "ip forward"): add_command_to_interface(interface, "no ip forward", commands) if (fabric_forwarding_anycast_gateway is True and obj_in_have.get("fabric_forwarding_anycast_gateway") is False): add_command_to_interface( interface, "fabric forwarding mode anycast-gateway", commands, ) elif (fabric_forwarding_anycast_gateway is False and obj_in_have.get("fabric_forwarding_anycast_gateway") is True): add_command_to_interface( interface, "no fabric forwarding mode anycast-gateway", commands, ) for item in args: candidate = w.get(item) if candidate and candidate != obj_in_have.get(item): cmd = item + " " + str(candidate) add_command_to_interface(interface, cmd, commands) if name and get_interface_type(name) == "ethernet": if mode != obj_in_have.get("mode"): admin_state = w.get("admin_state") or obj_in_have.get( "admin_state") if admin_state: c1 = "interface {0}".format( normalize_interface(w["name"])) c2 = get_admin_state(admin_state) commands2.append(c1) commands2.append(c2) else: commands.append(interface) # Don't run switchport command for loopback and svi interfaces if get_interface_type(name) in ("ethernet", "portchannel"): if mode == "layer2": commands.append("switchport") elif mode == "layer3": commands.append("no switchport") if admin_state == "up": commands.append("no shutdown") elif admin_state == "down": commands.append("shutdown") if ip_forward == "enable": commands.append("ip forward") elif ip_forward == "disable": commands.append("no ip forward") if fabric_forwarding_anycast_gateway is True: commands.append("fabric forwarding mode anycast-gateway") elif fabric_forwarding_anycast_gateway is False: commands.append( "no fabric forwarding mode anycast-gateway") for item in args: candidate = w.get(item) if candidate: commands.append(item + " " + str(candidate)) elif state == "default": if is_default is False: commands.append("default interface {0}".format(name)) elif is_default == "DNE": module.exit_json( msg="interface you are trying to default does not exist") return commands, commands2
def map_params_to_obj(module): obj = [] if 'aggregate' in module.params and module.params['aggregate']: args = { 'dest': '', 'remote_server': '', 'use_vrf': '', 'name': '', 'facility': '', 'dest_level': '', 'facility_level': '', 'interface': '', 'facility_link_status': None, 'event': None, 'file_size': None, 'message': None, 'timestamp': None } for c in module.params['aggregate']: d = c.copy() for key in args: if key not in d: d[key] = None if d['dest_level'] is not None: d['dest_level'] = str(d['dest_level']) if d['facility_level'] is not None: d['facility_level'] = str(d['facility_level']) if d['interface']: d['interface'] = normalize_interface(d['interface']) if 'state' not in d: d['state'] = module.params['state'] if d['file_size']: d['file_size'] = str(d['file_size']) obj.append(d) else: dest_level = None facility_level = None file_size = None if module.params['dest_level'] is not None: dest_level = str(module.params['dest_level']) if module.params['facility_level'] is not None: facility_level = str(module.params['facility_level']) if module.params['file_size'] is not None: file_size = str(module.params['file_size']) obj.append({ 'dest': module.params['dest'], 'remote_server': module.params['remote_server'], 'use_vrf': module.params['use_vrf'], 'name': module.params['name'], 'facility': module.params['facility'], 'dest_level': dest_level, 'facility_level': facility_level, 'interface': normalize_interface(module.params['interface']), 'state': module.params['state'], 'facility_link_status': module.params['facility_link_status'], 'event': module.params['event'], 'message': module.params['interface_message'], 'file_size': file_size, 'timestamp': module.params['timestamp'] }) return obj
def map_obj_to_commands(updates, module): commands = list() commands2 = list() want, have = updates args = ('speed', 'description', 'duplex', 'mtu') for w in want: name = w['name'] mode = w['mode'] ip_forward = w['ip_forward'] fabric_forwarding_anycast_gateway = w[ 'fabric_forwarding_anycast_gateway'] admin_state = w['admin_state'] state = w['state'] interface_type = w['interface_type'] del w['state'] if name: w['interface_type'] = None if interface_type: obj_in_have = {} if state in ('present', 'default'): module.fail_json( msg= 'The interface_type param can be used only with state absent.' ) else: obj_in_have = search_obj_in_list(name, have) is_default = is_default_interface(name, module) if name: interface = 'interface ' + name if state == 'absent': if obj_in_have: commands.append('no interface {0}'.format(name)) elif interface_type and not obj_in_have: intfs = get_interfaces_dict(module)[interface_type] cmds = get_interface_type_removed_cmds(intfs) commands.extend(cmds) elif state == 'present': if obj_in_have: # Don't run switchport command for loopback and svi interfaces if get_interface_type(name) in ('ethernet', 'portchannel'): if mode == 'layer2' and mode != obj_in_have.get('mode'): add_command_to_interface(interface, 'switchport', commands) elif mode == 'layer3' and mode != obj_in_have.get('mode'): add_command_to_interface(interface, 'no switchport', commands) if admin_state == 'up' and admin_state != obj_in_have.get( 'admin_state'): add_command_to_interface(interface, 'no shutdown', commands) elif admin_state == 'down' and admin_state != obj_in_have.get( 'admin_state'): add_command_to_interface(interface, 'shutdown', commands) if ip_forward == 'enable' and ip_forward != obj_in_have.get( 'ip_forward'): add_command_to_interface(interface, 'ip forward', commands) elif ip_forward == 'disable' and ip_forward != obj_in_have.get( 'ip forward'): add_command_to_interface(interface, 'no ip forward', commands) if (fabric_forwarding_anycast_gateway is True and obj_in_have.get('fabric_forwarding_anycast_gateway') is False): add_command_to_interface( interface, 'fabric forwarding mode anycast-gateway', commands) elif (fabric_forwarding_anycast_gateway is False and obj_in_have.get('fabric_forwarding_anycast_gateway') is True): add_command_to_interface( interface, 'no fabric forwarding mode anycast-gateway', commands) for item in args: candidate = w.get(item) if candidate and candidate != obj_in_have.get(item): cmd = item + ' ' + str(candidate) add_command_to_interface(interface, cmd, commands) if name and get_interface_type(name) == 'ethernet': if mode != obj_in_have.get('mode'): admin_state = w.get('admin_state') or obj_in_have.get( 'admin_state') if admin_state: c1 = 'interface {0}'.format( normalize_interface(w['name'])) c2 = get_admin_state(admin_state) commands2.append(c1) commands2.append(c2) else: commands.append(interface) # Don't run switchport command for loopback and svi interfaces if get_interface_type(name) in ('ethernet', 'portchannel'): if mode == 'layer2': commands.append('switchport') elif mode == 'layer3': commands.append('no switchport') if admin_state == 'up': commands.append('no shutdown') elif admin_state == 'down': commands.append('shutdown') if ip_forward == 'enable': commands.append('ip forward') elif ip_forward == 'disable': commands.append('no ip forward') if fabric_forwarding_anycast_gateway is True: commands.append('fabric forwarding mode anycast-gateway') elif fabric_forwarding_anycast_gateway is False: commands.append( 'no fabric forwarding mode anycast-gateway') for item in args: candidate = w.get(item) if candidate: commands.append(item + ' ' + str(candidate)) elif state == 'default': if is_default is False: commands.append('default interface {0}'.format(name)) elif is_default == 'DNE': module.exit_json( msg='interface you are trying to default does not exist') return commands, commands2
def map_config_to_obj(want, module): objs = list() for w in want: obj = dict(name=None, description=None, admin_state=None, speed=None, mtu=None, mode=None, duplex=None, interface_type=None, ip_forward=None, fabric_forwarding_anycast_gateway=None) if not w['name']: return obj command = 'show interface {0}'.format(w['name']) try: body = execute_show_command(command, module)[0] except IndexError: return list() if body: try: interface_table = body['TABLE_interface']['ROW_interface'] except (KeyError, TypeError): return list() if interface_table: if interface_table.get('eth_mode') == 'fex-fabric': module.fail_json( msg= 'nxos_interface does not support interfaces with mode "fex-fabric"' ) intf_type = get_interface_type(w['name']) if intf_type in ['portchannel', 'ethernet']: mode = interface_table.get('eth_mode') if mode in ('access', 'trunk', 'dot1q-tunnel'): obj['mode'] = 'layer2' elif mode in ('routed', 'layer3'): obj['mode'] = 'layer3' else: obj['mode'] = 'layer3' if intf_type == 'ethernet': obj['name'] = normalize_interface( interface_table.get('interface')) obj['admin_state'] = interface_table.get('admin_state') obj['description'] = interface_table.get('desc') obj['mtu'] = interface_table.get('eth_mtu') obj['duplex'] = interface_table.get('eth_duplex') command = 'show run interface {0}'.format(obj['name']) body = execute_show_command(command, module)[0] speed_match = re.search(r'speed (\d+)', body) if speed_match is None: obj['speed'] = 'auto' else: obj['speed'] = speed_match.group(1) duplex_match = re.search(r'duplex (\S+)', body) if duplex_match is None: obj['duplex'] = 'auto' else: obj['duplex'] = duplex_match.group(1) if 'ip forward' in body: obj['ip_forward'] = 'enable' else: obj['ip_forward'] = 'disable' elif intf_type == 'svi': obj['name'] = normalize_interface( interface_table.get('interface')) attributes = get_vlan_interface_attributes( obj['name'], intf_type, module) obj['admin_state'] = str( attributes.get('admin_state', 'nxapibug')) obj['description'] = str( attributes.get('description', 'nxapi_bug')) obj['mtu'] = interface_table.get('svi_mtu') command = 'show run interface {0}'.format(obj['name']) body = execute_show_command(command, module)[0] if 'ip forward' in body: obj['ip_forward'] = 'enable' else: obj['ip_forward'] = 'disable' if 'fabric forwarding mode anycast-gateway' in body: obj['fabric_forwarding_anycast_gateway'] = True else: obj['fabric_forwarding_anycast_gateway'] = False elif intf_type in ('loopback', 'management', 'nve'): obj['name'] = normalize_interface( interface_table.get('interface')) obj['admin_state'] = interface_table.get('admin_state') if obj['admin_state'] is None and intf_type == 'loopback': # Some platforms don't have the 'admin_state' key. # For loopback interfaces it's safe to use the # 'state' key instead. obj['admin_state'] = interface_table.get('state') obj['description'] = interface_table.get('desc') elif intf_type == 'portchannel': obj['name'] = normalize_interface( interface_table.get('interface')) obj['admin_state'] = interface_table.get('admin_state') obj['description'] = interface_table.get('desc') obj['mtu'] = interface_table.get('eth_mtu') if obj['admin_state'] is None: # Some nxos platforms do not have the 'admin_state' key. # Use the 'state_rsn_desc' key instead to determine the # admin state of the interface. state_description = interface_table.get('state_rsn_desc') if state_description == 'Administratively down': obj['admin_state'] = 'down' elif state_description is not None: obj['admin_state'] = 'up' objs.append(obj) return objs