Coverage for lib/ansible/modules/network/nxos/nxos_interface.py : 52%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. #
'status': ['preview'], 'supported_by': 'network'}
--- module: nxos_interface extends_documentation_fragment: nxos version_added: "2.1" short_description: Manages physical attributes of interfaces. description: - Manages physical attributes of interfaces of NX-OS switches. author: - Jason Edelman (@jedelman8) - Trishna Guha (@trishnaguha) notes: - Tested against NXOSv 7.3.(0)D1(1) on VIRL - This module is also used to create logical interfaces such as svis and loopbacks. - Be cautious of platform specific idiosyncrasies. For example, when you default a loopback interface, the admin state toggles on certain versions of NX-OS. - The M(nxos_overlay_global) C(anycast_gateway_mac) attribute must be set before setting the C(fabric_forwarding_anycast_gateway) property. options: name: description: - Full name of interface, i.e. Ethernet1/1, port-channel10. required: true default: null aliases: [interface] interface_type: description: - Interface type to be unconfigured from the device. required: false default: null choices: ['loopback', 'portchannel', 'svi', 'nve'] version_added: 2.2 speed: description: - Interface link speed. version_added: 2.5 admin_state: description: - Administrative state of the interface. required: false default: up choices: ['up','down'] description: description: - Interface description. required: false default: null mode: description: - Manage Layer 2 or Layer 3 state of the interface. This option is supported for ethernet and portchannel interface. required: false default: null choices: ['layer2','layer3'] mtu: description: - MTU for a specific interface. Must be an even number between 576 and 9216. required: false version_added: 2.5 ip_forward: description: - Enable/Disable ip forward feature on SVIs. required: false default: null choices: ['enable','disable'] version_added: 2.2 fabric_forwarding_anycast_gateway: description: - Associate SVI with anycast gateway under VLAN configuration mode. required: false default: null choices: ['true','false'] version_added: 2.2 duplex: description: - Interface link status default: auto choices: ['full', 'half', 'auto'] version_added: 2.5 tx_rate: description: - Transmit rate in bits per second (bps). version_added: 2.5 rx_rate: description: - Receiver rate in bits per second (bps). version_added: 2.5 neighbors: description: - Check the operational state of given interface C(name) for LLDP neighbor. - The following suboptions are available. suboptions: host: description: - "LLDP neighbor host for given interface C(name)." port: description: - "LLDP neighbor port to which given interface C(name) is connected." version_added: 2.5 aggregate: description: List of Interfaces definitions. version_added: 2.5 state: description: - Specify desired state of the resource. required: true default: present choices: ['present','absent','default'] delay: description: - Time in seconds to wait before checking for the operational state on remote device. This wait is applicable for operational state arguments. default: 10 """
- name: Ensure an interface is a Layer 3 port and that it has the proper description nxos_interface: name: Ethernet1/1 description: 'Configured by Ansible' mode: layer3
- name: Admin down an interface nxos_interface: name: Ethernet2/1 admin_state: down
- name: Remove all loopback interfaces nxos_interface: name: loopback state: absent
- name: Remove all logical interfaces nxos_interface: interface_type: "{{ item }} " state: absent loop: - loopback - portchannel - svi - nve
- name: Admin up all loopback interfaces nxos_interface: name: loopback 0-1023 admin_state: up
- name: Admin down all loopback interfaces nxos_interface: name: looback 0-1023 admin_state: down
- name: Check neighbors intent arguments nxos_interface: name: Ethernet2/3 neighbors: - port: Ethernet2/3 host: abc.mycompany.com
- name: Add interface using aggregate nxos_interface: aggregate: - { name: Ethernet0/1, mtu: 256, description: test-interface-1 } - { name: Ethernet0/2, mtu: 516, description: test-interface-2 } duplex: full speed: 100 state: present
- name: Delete interface using aggregate nxos_interface: aggregate: - name: Loopback9 - name: Loopback10 state: absent
- name: Check intent arguments nxos_interface: name: Ethernet0/2 state: up tx_rate: ge(0) rx_rate: le(0) """
commands: description: command list sent to the device returned: always type: list sample: - interface Ethernet2/3 - mtu 1500 - speed 10 """
else: 'command': command, 'output': output, }] return [] else:
return None
"""Gets the type of interface """ elif interface.upper().startswith('VL'): return 'svi' elif interface.upper().startswith('LO'): return 'loopback' elif interface.upper().startswith('MG'): return 'management' elif interface.upper().startswith('MA'): return 'management' elif interface.upper().startswith('PO'): return 'portchannel' elif interface.upper().startswith('NV'): return 'nve' else: return 'unknown'
"""Gets all active interfaces on a given switch """ try: body = execute_show_command('show interface', module)[0] except IndexError: return {}
interfaces = { 'ethernet': [], 'svi': [], 'loopback': [], 'management': [], 'portchannel': [], 'nve': [], 'unknown': [] }
if body: interface_list = body['TABLE_interface']['ROW_interface'] for index in interface_list: intf = index['interface'] intf_type = get_interface_type(intf) interfaces[intf_type].append(intf)
return interfaces
"""Return the normalized interface name """ return None
elif name.lower().startswith('vl'): if_type = 'Vlan' elif name.lower().startswith('lo'): if_type = 'loopback' elif name.lower().startswith('po'): if_type = 'port-channel' elif name.lower().startswith('nv'): if_type = 'nve' else: if_type = None
number = number_list[-1].strip() else:
else: proper_interface = name
""" Returns dictionary that has two k/v pairs: admin_state & description if not an svi, returns None """ command = 'show run interface {0} all'.format(name) try: body = execute_show_command(command, module)[0] except IndexError: return None if body: command_list = body.split('\n') desc = None admin_state = 'down' for each in command_list: if 'description' in each: desc = each.lstrip().split("description")[1].lstrip() elif 'no shutdown' in each: admin_state = 'up' return dict(description=desc, admin_state=admin_state) else: return None
commands = []
for interface in interfaces: if interface != 'Vlan1': commands.append('no interface {0}'.format(interface))
return commands
"""Checks to see if interface exists and if it is a default config """
except (IndexError, TypeError) as e: body = ''
return True
else: return 'DNE'
module.fail_json(msg='The interface_type param can be used only with state absent.')
if obj_in_have: commands.append('no interface {0}'.format(name)) elif interface_type: intfs = get_interfaces_dict(module)[interface_type] cmds = get_interface_type_removed_cmds(intfs) commands.extend(cmds)
add_command_to_interface(interface, 'switchport', commands) add_command_to_interface(interface, 'no switchport', commands)
add_command_to_interface(interface, 'ip forward', commands) add_command_to_interface(interface, 'no ip forward', commands)
obj_in_have.get('fabric_forwarding_anycast_gateway') is True): add_command_to_interface(interface, 'fabric forwarding mode anycast-gateway', commands)
obj_in_have.get('fabric_forwarding_anycast_gateway') is False): add_command_to_interface(interface, 'no fabric forwarding mode anycast-gateway', commands)
else: commands.append(interface) for item in args: candidate = w.get(item) if candidate: commands.append(item + ' ' + str(candidate))
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')
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')
else: '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'] })
mtu=None, mode=None, duplex=None, interface_type=None, ip_forward=None, fabric_forwarding_anycast_gateway=None)
except IndexError: return list() except KeyError: return list()
module.fail_json(msg='nxos_interface does not support interfaces with mode "fex-fabric"')
obj['mode'] = 'layer3'
obj['speed'] = speed else: obj['mode'] = 'layer2'
obj['ip_forward'] = 'enable' else: obj['fabric_forwarding_anycast_gateway'] = True else:
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'))
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') 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['mode'] = interface_table.get('eth_mode')
else: out = ''
have_host = [] have_port = [] if have_neighbors is None: cmd = [{'command': 'show lldp neighbors interface {0} detail'.format(w['name']), 'output': 'text'}] output = run_commands(module, cmd, check_rc=False) if output: have_neighbors = output[0] else: have_neighbors = '' if have_neighbors and 'Total entries displayed: 0' not in have_neighbors: for line in have_neighbors.strip().split('\n'): if line.startswith('Port Description'): have_port.append(line.split(': ')[1]) if line.startswith('System Name'): have_host.append(line.split(': ')[1])
for item in want_neighbors: host = item.get('host') port = item.get('port') if host and host not in have_host: failed_conditions.append('host ' + host) if port and port not in have_port: failed_conditions.append('port ' + port)
""" main entry point for module execution """ host=dict(), port=dict() )
name=dict(aliases=['interface']), admin_state=dict(default='up', choices=['up', 'down']), description=dict(), speed=dict(), mode=dict(choices=['layer2', 'layer3']), mtu=dict(), duplex=dict(choices=['full', 'half', 'auto']), interface_type=dict(choices=['loopback', 'portchannel', 'svi', 'nve']), ip_forward=dict(choices=['enable', 'disable']), fabric_forwarding_anycast_gateway=dict(type='bool'), tx_rate=dict(), rx_rate=dict(), neighbors=dict(type='list', elements='dict', options=neighbors_spec), delay=dict(default=10, type='int'), state=dict(choices=['absent', 'present', 'default'], default='present') )
# remove default in aggregate spec, to handle common arguments
aggregate=dict(type='list', elements='dict', options=aggregate_spec, mutually_exclusive=[['name', 'interface_type']]) )
['name', 'interface_type']]
required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True)
result['warnings'] = warnings
# if the mode changes from L2 to L3, the admin state # seems to change after the API call, so adding a second API # call to ensure it's in the desired state.
|