8000 [nxos_interfaces] resource module POC by trishnaguha · Pull Request #2 · ansible/network · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.

[nxos_interfaces] resource module POC #2

Closed
wants to merge 21 commits into from
Closed
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
118 changes: 118 additions & 0 deletions library/nxos_facts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2019, 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: nxos_facts
extends_documentation_fragment: nxos
version_added: "2.9"
short_description: Gets facts about NX-OS switches
description:
- Collects facts from Cisco Nexus devices running the NX-OS operating
system. Fact collection is supported over both Cli and Nxapi
transports. This module prepends all of the base network fact keys
with C(ansible_net_<fact>). The facts module will always collect a
base set of facts from the device and can enable or disable
collection of additional facts.
author:
- Trishna Guha (@trishnaguha)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
options:
gather_subset:
description:
- When supplied, this argument will restrict the facts collected
to a given subset. Possible values for this argument include
all, min, hardware, config, legacy, and interfaces. Can specify a
list of values to include a larger subset.Values can also be used
with an initial C(M(!)) to specify that a specific subset should
not be collected.
required: false
default: '!config'
version_added: "2.2"
gather_network_resources:
description:
10000 - When supplied, this argument will restrict the facts collected
to a given subset. Possible values for this argument include
all and the resources like interfaces, vlans etc.
Can specify a list of values to include a larger subset. Values
can also be used with an initial C(M(!)) to specify that a
specific subset should not be collected.
required: false
version_added: "2.9"
"""

EXAMPLES = """
# Gather all facts
- nxos_facts:
gather_subset: all
gather_network_resources: all

# Collect only the interfaces facts
- nxos_facts:
gather_subset:
- !all
- !min
gather_network_resources:
- interfaces

# Do not collect interfaces facts
- nxos_facts:
gather_network_resources:
- "!interfaces"

# Collect interfaces and default facts
- nxos_facts:
gather_subset: min
gather_network_resources: interfaces
"""

RETURN = """
ansible_net_gather_subset:
description: The list of fact subsets collected from the device
returned: always
type: list
ansible_gather_network_resources:
description: The list of fact resource subsets collected from the device
returned: always
type: list
"""


from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import Connection
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.nxos.facts.facts import Facts


def main():
argument_spec = Facts.argument_spec
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
warnings = ['default value for `gather_subset` will be changed to `min` from `!config` v2.11 onwards']

connection = Connection(module._socket_path)
gather_subset = module.params['gather_subset']
gather_network_resources = module.params['gather_network_resources']
result = Facts().get_facts(module, connection, gather_subset, gather_network_resources)

ansible_facts, additional_warnings = result
warnings.extend(additional_warnings)

module.exit_json(ansible_facts=ansible_facts, warnings=warnings)


if __name__ == '__main__':
main()
63 changes: 63 additions & 0 deletions library/nxos_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/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: Manages physical attributes of interfaces on Cisco NX-OS devices.
description:
- Manages physical attributes of interfaces of NX-OS switches.
author:
- Trishna Guha (@trishnaguha)
options: {}
"""

EXAMPLES = """
- name: Configure interfaces
nxos_interfaces:
config:
- name: Ethernet1/1
description: 'Configured by Ansible'
enable: True
- name: Ethernet1/2
description: 'Configured by Ansible'
enable: False
operation: merge

"""

RETURN = """
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.nxos.config.interfaces.interfaces import Interfaces


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

result = Interfaces(module).execute_module()
module.exit_json(**result)


if __name__ == '__main__':
main()
Empty file added module_utils/__init__.py
Empty file.
Empty file added module_utils/nxos/__init__.py
Empty file.
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions module_utils/nxos/argspec/facts/facts.py
9E12
Original file line numberDiff line number Diff line change
@@ -0,0 +1,14 @@
class FactsArgs(object):

def __init__(self, **kwargs):
pass

choices = [
'all',
'interfaces',
]

argument_spec = {
'gather_subset': dict(default=['!config'], type='list'),
'gather_network_resources': dict(default=['all'], choices=choices, type='list'),
}
Empty file.
21 changes: 21 additions & 0 deletions module_utils/nxos/argspec/interfaces/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class InterfaceArgs(object):

def __init__(self, **kwargs):
pass

config_spec = {
'name': dict(type='str', required=True),
'description': dict(),
'enable': dict(default=True, type=bool),
'speed': dict(),
'mode': dict(choices=['layer2', 'layer3']),
'mtu': dict(),
'duplex': dict(choices=['full', 'half', 'auto']),
'ip_forward': dict(choices=['enable', 'disable']),
'fabric_forwarding_anycast_gateway': dict(type='bool'),
}

argument_spec = {
'state': dict(default='merged', choices=['merged', 'replaced', 'overridden', 'deleted']),
'config': dict(type='list', elements='dict', options=config_spec)
}
Empty file.
16 changes: 16 additions & 0 deletions module_utils/nxos/config/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from ansible.module_utils.connection import Connection


class ConfigBase(object):

_connection = None

def __init__(self, module):
self._module = module
self._connection = self._get_connection()

def _get_connection(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API can be moved to the base class?

Copy link
Member Author
@trishnaguha trishnaguha Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ganeshrn which base class are you referring to?

if self._connection:
return self._connection
self._connection = Connection(self._module._socket_path)
return self._connection
Empty file.
Loading
0