-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding gns3_nodes_inventory and bumping gns3fy usage to 0.3.0
- Loading branch information
Showing
9 changed files
with
207 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |