Skip to content

Commit

Permalink
Merging v1.2.0
Browse files Browse the repository at this point in the history
Adding gns3_nodes_inventory and bumping gns3fy usage to 0.3.0
  • Loading branch information
davidban77 authored Sep 11, 2019
2 parents 4216fde + 1b0c2ec commit 4030710
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 48 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Releases

## 1.2.0

New features:

- Modules:
- `gns3_nodes_inventory`: Returns inventory-style dictionary of the nodes.
- No more `node_type` needed when creating nodes in a project.

Fixes:

- Modules:
- Error when using the `gns3_version` module when `gns3fy` is not installed

- Tests:
- Added check for `gns3_version`

## 1.1.0

New features:
Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ Here are some examples of how to use the module.

- debug: var=result
```
#### Get nodes_inventory information from a project
```yaml
---
- host: localhost
# Call the collections to use the respective modules
collections:
- davidban77.gns3
vars:
gns3_url: http://localhost
tasks:
- name: Get the server version
gns3_nodes_inventory:
url: "{{ gns3_url }}"
project_name: lab_example
register: result

- debug: var=result
```
#### Manipulate GNS3 projects
Expand Down Expand Up @@ -108,10 +127,8 @@ Here are some examples of how to use the module.
project_name: new_lab
nodes_spec:
- name: alpine-1
node_type: docker
template: alpine
- name: alpine-2
node_type: docker
template: alpine
links_spec:
- ['alpine-1', 'eth0', 'alpine-2', 'eth1']
Expand Down Expand Up @@ -160,16 +177,12 @@ gns3_url: "http://dev_gns3server"
gns3_project_name: test_ansible
gns3_nodes_spec:
- name: veos-1
node_type: qemu
template: "vEOS-4.21.5F"
- name: veos-2
node_type: qemu
template: "vEOS-4.21.5F"
- name: ios-1
node_type: iou
template: "IOU-15.4"
- name: ios-2
node_type: iou
template: "IOU-15.4"
gns3_nodes_strategy: one_by_one
gns3_links_spec:
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: "davidban77"
name: "gns3"
version: "1.1.0"
version: "1.2.0"
readme: "README.md"
description: "Module to interact with GNS3 server REST API based on gns3fy"
authors:
Expand Down
115 changes: 115 additions & 0 deletions plugins/modules/gns3_nodes_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python

ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}

DOCUMENTATION = """
---
module: gns3_nodes_inventory
short_description: Retrieves GNS3 a project nodes console information
version_added: '2.8'
description:
- "Retrieves nodes inventory information from a GNS3 project"
requirements: [ gns3fy ]
author:
- David Flores (@netpanda)
options:
url:
description:
- URL target of the GNS3 server
required: true
type: str
port:
description:
- TCP port to connect to server REST API
type: int
default: 3080
project_name:
description:
- Project name
type: str
project_id:
description:
- Project ID
type: str
"""

EXAMPLES = """
# Retrieve the GNS3 server version
- name: Get the server version
gns3_nodes_inventory:
url: http://localhost
port: 3080
project_name: test_lab
register: nodes_inventory
- debug: var=nodes_inventory
"""

RETURN = """
nodes_inventory:
description: Dictionary that contain: name, server, console_port, console_type,
type and template of each node
type: dict
total_nodes:
description: Total number of nodes
type: int
"""
import traceback

GNS3FY_IMP_ERR = None
try:
import gns3fy

HAS_GNS3FY = True
except ImportError:
GNS3FY_IMP_ERR = traceback.format_exc()
HAS_GNS3FY = False

from ansible.module_utils.basic import AnsibleModule, missing_required_lib


def main():
module = AnsibleModule(
argument_spec=dict(
url=dict(type="str", required=True),
port=dict(type="int", default=3080),
project_name=dict(type="str", default=None),
project_id=dict(type="str", default=None),
),
required_one_of=[["project_name", "project_id"]],
)
if not HAS_GNS3FY:
module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR)
result = dict(changed=False, nodes_inventory=None, total_nodes=None)

server_url = module.params['url']
server_port = module.params['port']
project_name = module.params['project_name']
project_id = module.params['project_id']

# Create server session
server = gns3fy.Gns3Connector(url=f"{server_url}:{server_port}")
# Define the project
if project_name is not None:
project = gns3fy.Project(name=project_name, connector=server)
elif project_id is not None:
project = gns3fy.Project(project_id=project_id, connector=server)

# Retrieve project info
project.get()

nodes_inventory = project.nodes_inventory()
result.update(
nodes_inventory=nodes_inventory,
total_nodes=len(nodes_inventory.keys())
)

module.exit_json(**result)


if __name__ == "__main__":
main()
12 changes: 6 additions & 6 deletions plugins/modules/gns3_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@
import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib

LIB_IMP_ERR = None
GNS3FY_IMP_ERR = None
try:
from gns3fy import Gns3Connector, Project

HAS_LIB = True
HAS_GNS3FY = True
except Exception:
HAS_LIB = False
LIB_IMP_ERR = traceback.format_exc()
HAS_GNS3FY = False
GNS3FY_IMP_ERR = traceback.format_exc()


def return_project_data(project):
Expand Down Expand Up @@ -291,8 +291,8 @@ def main():
required_one_of=[["project_name", "project_id"]],
required_if=[["nodes_strategy", "one_by_one", ["nodes_delay"]]],
)
if not HAS_LIB:
module.fail_json(msg=missing_required_lib("gns3fy"), exception=LIB_IMP_ERR)
if not HAS_GNS3FY:
module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR)
if module.check_mode:
module.exit_json(**result)

Expand Down
57 changes: 29 additions & 28 deletions plugins/modules/gns3_version.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env python

ANSIBLE_METADATA = {
'metadata_version': '1.2',
'status': ['preview'],
'supported_by': 'community'
"metadata_version": "1.2",
"status": ["preview"],
"supported_by": "community",
}

DOCUMENTATION = '''
DOCUMENTATION = """
---
module: gns3_version
short_description: Retrieves GNS3 server version
version_added: "2.8"
version_added: '2.8'
description:
- "Retrieves GNS3 server version using gns3fy"
- 'Retrieves GNS3 server version using gns3fy'
requirements: [ gns3fy ]
author:
- David Flores (@netpanda)
Expand All @@ -27,9 +27,9 @@
- TCP port to connect to server REST API
type: int
default: 3080
'''
"""

EXAMPLES = '''
EXAMPLES = """
# Retrieve the GNS3 server version
- name: Get the server version
gns3_version:
Expand All @@ -38,9 +38,9 @@
register: result
- debug: var=result
'''
"""

RETURN = '''
RETURN = """
local_compute:
description: Whether this is a local server or not
type: bool
Expand All @@ -49,32 +49,33 @@
description: Version number of the server
type: str
returned: always
'''
"""
import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib

from ansible.module_utils.basic import AnsibleModule
from gns3fy import Gns3Connector
GNS3FY_IMP_ERR = None
try:
from gns3fy import Gns3Connector

HAS_GNS3FY = True
except Exception:
HAS_GNS3FY = False
GNS3FY_IMP_ERR = traceback.format_exc()


def main():
module_args = dict(
url=dict(type="str", required=True),
port=dict(type="int", default=3080),
)
result = dict(
changed=False,
local_compute=None,
version=None
module = AnsibleModule(
argument_spec=dict(
url=dict(type="str", required=True), port=dict(type="int", default=3080)
)
)
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
if module.check_mode:
module.exit_json(**result)
if not HAS_GNS3FY:
module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR)
result = dict(changed=False, local_compute=None, version=None)

server = Gns3Connector(url=f"{module.params['url']}:{module.params['port']}")
_version = server.get_version()
result.update(
local_compute=_version["local"],
version=_version["version"]
)
result.update(local_compute=_version["local"], version=_version["version"])
module.exit_json(**result)


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ansible>=2.8.0
gns3fy>=0.2.0
gns3fy>=0.3.0
4 changes: 0 additions & 4 deletions test/playbooks/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ gns3_url: "http://dev_gns3server"
gns3_project_name: test_ansible
gns3_nodes_spec:
- name: veos-1
node_type: qemu
template: "vEOS-4.21.5F"
- name: veos-2
node_type: qemu
template: "vEOS-4.21.5F"
- name: ios-1
node_type: iou
template: "IOU-15.4"
- name: ios-2
node_type: iou
template: "IOU-15.4"
gns3_nodes_strategy: one_by_one
gns3_links_spec:
Expand Down
22 changes: 20 additions & 2 deletions test/playbooks/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
- hosts: localhost
tasks:
- import_role:
name: create_lab
- name: Get server version
gns3_version:
url: "{{ gns3_url }}"
register: version

- debug: var=version

- name: Creation/Active section
when: execute == "create"
block:
- import_role:
name: create_lab

- name: Get nodes inventory
gns3_nodes_inventory:
url: "{{ gns3_url }}"
project_name: "{{ gns3_project_name }}"
register: nodes_inventory

- debug: var=nodes_inventory

- import_role:
name: delete_lab
when: execute == "delete"

0 comments on commit 4030710

Please sign in to comment.