Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions library/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2018, Red Hat, Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}


DOCUMENTATION = """
---
module: interfaces
version_added: "2.8"
short_description: Collect device capabilities from Network devices
description:
- Collect basic fact capabilities from Network devices and return
the capabilities as Ansible facts.
author:
- Trishna Guha (@trishnaguha)
options: {}
"""

EXAMPLES = """
- interfaces:
"""

RETURN = """
"""


from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import Connection, ConnectionError
from ansible.module_utils.cisco_nxos.config.interfaces.interfaces import Interface


def main():
""" main entry point for module execution
"""
module = AnsibleModule(argument_spec=Interface.argument_spec,
supports_check_mode=True)

connection = Connection(module._socket_path)
try:
config = connection.get('show running-config all | section ^interface')
except ConnectionError:
config = None

result = {'changed': False}
commands = list()

intf = Interface(**module.params)

resp = intf.set_config(module, config)
if resp:
commands.extend(resp)

if commands:
if not module.check_mode:
connection.edit_config(commands)
result['changed'] = True

result['commands'] = commands
if result['changed']:
failed_conditions = intf.set_state(module)

if failed_conditions:
msg = 'One or more conditional statements have not been satisfied'
module.fail_json(msg=msg, failed_conditions=failed_conditions)

module.exit_json(**result)


if __name__ == '__main__':
main()
Empty file added module_utils/__init__.py
Empty file.
Empty file.
111 changes: 111 additions & 0 deletions module_utils/cisco_nxos/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from itertools import chain

from ansible.module_utils.six import iteritems
from ansible.module_utils.network.common.utils import to_list, sort_list
from ansible.module_utils.network.common.config import NetworkConfig


class ConfigBase(object):

argument_spec = {}
identifier = ()

def __init__(self, **kwargs):
self.values = {}

for item in self.identifier:
self.values[item] = kwargs.pop(item)

for key, value in iteritems(kwargs):
if key in self.argument_spec:
setattr(self, key, value)

def __getattr__(self, key):
if key in self.argument_spec:
return self.values.get(key)

def __setattr__(self, key, value):
if key in self.argument_spec:
if key in self.identifier:
raise TypeError('cannot set value')
elif value is not None:
self.values[key] = value
else:
super(ConfigBase, self).__setattr__(key, value)

def render(self, config=None):
raise NotImplementedError

def get_section(self, config, section):
if config is not None:
netcfg = NetworkConfig(indent=2, contents=config)
try:
config = netcfg.get_block_config(to_list(section))
except ValueError:
config = None
return config

def search_obj_in_list(self, name, lst):
for o in lst:
if o['name'] == name:
return o
return None

def normalize_interface(self, name):
"""Return the normalized interface name
"""
if not name:
return

def _get_number(name):
digits = ''
for char in name:
if char.isdigit() or char in '/.':
digits += char
return digits

if name.lower().startswith('et'):
if_type = 'Ethernet'
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_list = name.split(' ')
if len(number_list) == 2:
number = number_list[-1].strip()
else:
number = _get_number(name)

if if_type:
proper_interface = if_type + number
else:
proper_interface = name

return proper_interface

def get_interface_type(self, interface):
"""Gets the type of interface
"""
if interface.upper().startswith('ET'):
return 'ethernet'
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'
Empty file.
Loading