Skip to content

Commit

Permalink
Merge pull request #14 from davidban77/develop
Browse files Browse the repository at this point in the history
Bumping to 1.5.0
  • Loading branch information
davidban77 authored Oct 1, 2019
2 parents 30e44cc + 92c911b commit 66938bb
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 60 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Releases

## 1.5.0

**Enhancements:**

- Added `gns3_facts` to retrieve the compute information like: console ports, server version, available emulators, available images, etc..
- Added `gns3_snapshot` to manipulate the snapshot creation/deletion and restoration of a project.

**Fixes:**

- Added the needed `user` and `password` arguments to all the modules, when interacting with a GNS3 server with authentication.
- Refactored the modules a little to be more standard with each other.

## 1.4.0

**Enhacements:**
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.4.0
VERSION=1.5.0

build:
rm -rf releases/
Expand All @@ -16,12 +16,15 @@ test-node-interaction:
test-create-files:
cd test/playbooks; ansible-playbook create_files.yml

test-snapshots:
cd test/playbooks; ansible-playbook snapshots.yml

test-delete-files:
cd test/playbooks; ansible-playbook delete_files.yml

test-delete-lab:
cd test/playbooks; ansible-playbook main.yml -e execute=delete

test-create-all: test-create-lab test-create-files test-node-interaction
test-create-all: test-create-lab test-create-files test-node-interaction test-snapshots

test-delete-all: test-delete-files test-delete-lab
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ mazer install davidban77.gns3
- Starts/stops all nodes inside a project, or it can be done sequentially with a delay factor.
- Creates/Updates projects with nodes and links specified as variables in a playbook.
- Deletes projects safely by stopping nodes, if there are any, then closing the project and finally deleting it.
- Creates/Deletes/Restores snapshots of projects.
- Retrieves information about available emulators on the GNS3 server compute, as well as available images, console ports, version, etc..
- Idempotency is present in all actions. An example could be reflected in a playbook that creates a project with nodes and links, these settings will not be executed again on a rerun (and by settings I mean projects settings, nodes and links)/


## Modules

These are the modules provided with this collection:

- `gns3_version`: Retrieves GNS3 server version
- `gns3_version`: Retrieves GNS3 server version. (**TO BE DEPRECATED** with the `gns3_facts` module)
- `gns3_facts`: Retrieves the compute(s) information of a GNS3 server
- `gns3_project`: Module to interact with GNS3 server projects
- It opens/closes projects and performs basic turnup/teradown operations on nodes.
- It creates/updates or deletes projects, with the respective nodes and links specified
- `gns3_project_file`: Updates/creates a file on a project directory.
- `gns3_snapshot`: Module that interacts with snapshots of a project on GNS3 server.
- `gns3_node`: Module to operate a node in a GNS3 server project.
- `gns3_node_file`: Updates/creates a file on a node directory.
- `gns3_nodes_inventory`: Retrieves GNS3 a project nodes console information.

## Examples: using the module

Expand All @@ -48,10 +56,12 @@ Here are some examples of how to use the module.
vars:
gns3_url: http://localhost
tasks:
- name: Get the server version
gns3_version:
- name: Get the server facts
gns3_facts:
url: "{{ gns3_url }}"
port: 3080
get_images: all
get_compute_ports: yes
register: result

- debug: var=result
Expand Down Expand Up @@ -191,3 +201,7 @@ gns3_links_spec:
- ["veos-2", "Ethernet2", "ios-2", "Ethernet1/0"]
- ["ios-1", "Ethernet1/2", "ios-2", "Ethernet1/2"]
```

### More examples

For more examples like create an `/etc/network/interfaces` file for an `alpine` docker node to configure its network interfaces, or restore a project to an specific snapshot, you can go to the `test/playbooks` directory.
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.4.0"
version: "1.5.0"
readme: "README.md"
description: "Module to interact with GNS3 server REST API based on gns3fy"
authors:
Expand Down
187 changes: 187 additions & 0 deletions plugins/modules/gns3_facts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/usr/bin/env python

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

DOCUMENTATION = """
---
module: gns3_facts
short_description: Module that retrieves the compute(s) information of a GNS3 server
version_added: '2.8'
description:
- Module that retrieves the compute(s) information of a GNS3 server
requirements: [ gns3fy ]
author:
- David Flores (@davidban77)
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
user:
description:
- User to connect to GNS3 server
type: str
password:
description:
- Password to connect to GNS3 server
type: str
get_images:
description:
- If set it will also retrieve the images of the specified emulator unless
- is set to 'all', in which case will retrieve from all emulators. For a
- list of available emulators, visit the GNS3 API information
type: str
get_compute_ports:
description:
- If set it will retrieve the console_ports and udp_ports of the compute
type: bool
"""

EXAMPLES = """
# Retrieves all the information from the computes of GNS3 server
- name: Retrieve all the facts of a GNS3 server computes
gns3_facts:
url: http://localhost
get_images: all
get_compute_ports: yes
register: computes_info
- debug: var=computes_info
# Retrieves only basic facts data of the GNS3 server computes
- gns3_facts:
url: http://localhost
register: computes_info
- debug: var=computes_info
"""

RETURN = """
compute_id:
description: Server identifier
type: str
name:
description: Server name
type: str
host:
description: Server host
type: str
capabilities:
description: Object that describes what the server supports
type: dict
connected:
description: Whether the controller is connected to the compute or not
type: bool
cpu_usage_percent:
description: CPU usage of the compute
type: float
memory_usage_percent:
description: RAM usage of the compute
type: int
port:
description: Server port
type: int
protocol:
description: Protocol used (http, https)
type: str
user:
description: User for authentication
type: str
last_error:
description: Last error on the compute
type: str
images:
description: Images configured on the compute depending on the emulator (optional)
type: dict
compute_ports:
description: Ports used by the compute (console and udp ports) (optional)
type: dict
"""

import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib

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 = AnsibleModule(
argument_spec=dict(
url=dict(type="str", required=True),
port=dict(type="int", default=3080),
user=dict(type="str", default=None),
password=dict(type="str", default=None, no_log=True),
get_images=dict(type="str", default=None),
get_compute_ports=dict(type="bool", default=False),
)
)
result = dict(changed=False)
if not HAS_GNS3FY:
module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR)

server_url = module.params["url"]
server_port = module.params["port"]
server_user = module.params["user"]
server_password = module.params["password"]
get_images = module.params["get_images"]
get_compute_ports = module.params["get_compute_ports"]

try:
# Create server session
server = Gns3Connector(
url=f"{server_url}:{server_port}", user=server_user, cred=server_password
)

computes = server.get_computes()
for compute in computes:

# Images
if get_images:
compute["images"] = dict()

if get_images == "all":
for emulator in compute["capabilities"]["node_types"]:
try:
compute["images"][emulator] = server.get_compute_images(
emulator=emulator, compute_id=compute["compute_id"]
)
except Exception as err:
if "404" in str(err):
# Contine if no image dir is set for that emulator
continue
else:
compute["images"][get_images] = server.get_compute_images(
emulator=get_images, compute_id=compute["compute_id"]
)

# Compute ports
if get_compute_ports:
compute["compute_ports"] = server.get_compute_ports(
compute_id=compute["compute_id"]
)

result["facts"] = computes
module.exit_json(**result)
except Exception as err:
module.fail_json(msg=str(err), **result)


if __name__ == "__main__":
main()
20 changes: 17 additions & 3 deletions plugins/modules/gns3_node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

ANSIBLE_METADATA = {
"metadata_version": "1.1",
"metadata_version": "1.2",
"status": ["preview"],
"supported_by": "community",
}
Expand All @@ -16,7 +16,7 @@
- It starts/stops/suspend/reloads a node.
requirements: [ gns3fy ]
author:
- David Flores (@netpanda)
- David Flores (@davidban77)
options:
url:
description:
Expand All @@ -28,6 +28,14 @@
- TCP port to connect to server REST API
type: int
default: 3080
user:
description:
- User to connect to GNS3 server
type: str
password:
description:
- Password to connect to GNS3 server
type: str
project_name:
description:
- Project name
Expand Down Expand Up @@ -189,6 +197,8 @@ def main():
argument_spec=dict(
url=dict(type="str", required=True),
port=dict(type="int", default=3080),
user=dict(type="str", default=None),
password=dict(type="str", default=None, no_log=True),
state=dict(
type="str",
required=True,
Expand All @@ -210,6 +220,8 @@ def main():

server_url = module.params["url"]
server_port = module.params["port"]
server_user = module.params["user"]
server_password = module.params["password"]
state = module.params["state"]
project_name = module.params["project_name"]
project_id = module.params["project_id"]
Expand All @@ -221,7 +233,9 @@ def main():

try:
# Create server session
server = Gns3Connector(url=f"{server_url}:{server_port}")
server = Gns3Connector(
url=f"{server_url}:{server_port}", user=server_user, cred=server_password
)
# Define the project
if project_name is not None:
project = Project(name=project_name, connector=server)
Expand Down
Loading

0 comments on commit 66938bb

Please sign in to comment.