From 4cb9b27bc6093950d6a5b358ab77f752f70b5446 Mon Sep 17 00:00:00 2001 From: David Flores Date: Wed, 4 Sep 2019 17:35:17 +0100 Subject: [PATCH 1/5] Adding nodes_inventory module and fixing playbook and imports --- plugins/modules/gns3_project.py | 12 +++---- plugins/modules/gns3_version.py | 57 +++++++++++++++++---------------- test/playbooks/main.yml | 22 +++++++++++-- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/plugins/modules/gns3_project.py b/plugins/modules/gns3_project.py index d7d064a..1d53b52 100644 --- a/plugins/modules/gns3_project.py +++ b/plugins/modules/gns3_project.py @@ -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): @@ -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) diff --git a/plugins/modules/gns3_version.py b/plugins/modules/gns3_version.py index cffd229..e69fcb9 100644 --- a/plugins/modules/gns3_version.py +++ b/plugins/modules/gns3_version.py @@ -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) @@ -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: @@ -38,9 +38,9 @@ register: result - debug: var=result -''' +""" -RETURN = ''' +RETURN = """ local_compute: description: Whether this is a local server or not type: bool @@ -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) diff --git a/test/playbooks/main.yml b/test/playbooks/main.yml index 8b81dee..0527ca6 100644 --- a/test/playbooks/main.yml +++ b/test/playbooks/main.yml @@ -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" From f795cde0e280343490a3c30b82bebfa73e0ff545 Mon Sep 17 00:00:00 2001 From: David Flores Date: Wed, 4 Sep 2019 17:35:59 +0100 Subject: [PATCH 2/5] Adding gns3_nodes_inventory --- plugins/modules/gns3_nodes_inventory.py | 115 ++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 plugins/modules/gns3_nodes_inventory.py diff --git a/plugins/modules/gns3_nodes_inventory.py b/plugins/modules/gns3_nodes_inventory.py new file mode 100644 index 0000000..93d63d4 --- /dev/null +++ b/plugins/modules/gns3_nodes_inventory.py @@ -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() From 293330502bbd710a478a8bb005219feb7378aa29 Mon Sep 17 00:00:00 2001 From: David Flores Date: Wed, 4 Sep 2019 17:40:59 +0100 Subject: [PATCH 3/5] Updating galaxy --- CHANGELOG.md | 15 +++++++++++++++ README.md | 19 +++++++++++++++++++ galaxy.yml | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31e5de9..4927923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Releases +## 1.2.0 + +New features: + +- Modules: + - `gns3_nodes_inventory`: Returns inventory-style dictionary of the nodes. + +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: diff --git a/README.md b/README.md index 5dc2628..dd91c73 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/galaxy.yml b/galaxy.yml index f974be3..d26ad93 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -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: From 7de4fdf3b4620de359cf62b13a3993578624ea1a Mon Sep 17 00:00:00 2001 From: David Flores Date: Wed, 11 Sep 2019 18:37:27 +0100 Subject: [PATCH 4/5] bumping gns3fy to 0.3.0 which no longer the node_type is needed --- README.md | 6 ------ galaxy.yml | 2 +- requirements.txt | 2 +- test/playbooks/group_vars/all.yml | 4 ---- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index dd91c73..fee7c8d 100644 --- a/README.md +++ b/README.md @@ -127,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'] @@ -179,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: diff --git a/galaxy.yml b/galaxy.yml index d26ad93..b7d0ef0 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "davidban77" name: "gns3" -version: "1.2.0" +version: "1.2.1" readme: "README.md" description: "Module to interact with GNS3 server REST API based on gns3fy" authors: diff --git a/requirements.txt b/requirements.txt index 9e086ff..d9f79dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ ansible>=2.8.0 -gns3fy>=0.2.0 +gns3fy>=0.3.0 diff --git a/test/playbooks/group_vars/all.yml b/test/playbooks/group_vars/all.yml index 243a111..2f6ec22 100644 --- a/test/playbooks/group_vars/all.yml +++ b/test/playbooks/group_vars/all.yml @@ -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: From 1b0c2ec4b81f0c5169d3dd5eebfdae41046b0fc6 Mon Sep 17 00:00:00 2001 From: David Flores Date: Wed, 11 Sep 2019 18:44:06 +0100 Subject: [PATCH 5/5] updating changelog --- CHANGELOG.md | 1 + galaxy.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4927923..015f6de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ 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: diff --git a/galaxy.yml b/galaxy.yml index b7d0ef0..d26ad93 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "davidban77" name: "gns3" -version: "1.2.1" +version: "1.2.0" readme: "README.md" description: "Module to interact with GNS3 server REST API based on gns3fy" authors: