-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from davidban77/develop
Bumping to version 1.3.0
- Loading branch information
Showing
8 changed files
with
398 additions
and
1 deletion.
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,178 @@ | ||
#!/usr/bin/env python | ||
|
||
ANSIBLE_METADATA = { | ||
"metadata_version": "1.1", | ||
"status": ["preview"], | ||
"supported_by": "community", | ||
} | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: gns3_node_file | ||
short_description: Updates/creates a file on a node directory | ||
version_added: '2.8' | ||
description: | ||
- "Updates/creates a file on a node directory of 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 | ||
node_name: | ||
description: | ||
- Node name | ||
type: str | ||
node_id: | ||
description: | ||
- Node ID | ||
type: str | ||
data: | ||
description: | ||
- The text to insert. | ||
type: str | ||
dest: | ||
description: | ||
- Node destination path. Like 'etc/network/interfaces' | ||
type: str | ||
required: true | ||
state: | ||
description: | ||
- If the file should present or absent | ||
type: str | ||
choices: ['present', 'absent'] | ||
default: present | ||
""" | ||
|
||
EXAMPLES = """ | ||
# Retrieve the GNS3 server version | ||
- name: Get the server version | ||
gns3_node_file: | ||
url: http://localhost | ||
port: 3080 | ||
project_name: test_lab | ||
node_name: alpine-1 | ||
data: | | ||
auto eth0 | ||
iface eth0 inet dhcp | ||
dest: /etc/network/interfaces | ||
""" | ||
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 node_write_file(module, node, path, data): | ||
"Writes text data into specified path of the node" | ||
try: | ||
node.write_file(path=path, data=data) | ||
except Exception as err: | ||
module.fail_json(msg=str(err), exception=traceback.format_exc()) | ||
|
||
|
||
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), | ||
node_name=dict(type="str", default=None), | ||
node_id=dict(type="str", default=None), | ||
data=dict(type="str", default=None), | ||
dest=dict(type="str", required=True), | ||
state=dict(type="str", choices=["present", "absent"], default="present"), | ||
), | ||
required_one_of=[["project_name", "project_id"], ["node_name", "node_id"]], | ||
) | ||
if not HAS_GNS3FY: | ||
module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR) | ||
result = dict(changed=False) | ||
|
||
server_url = module.params['url'] | ||
server_port = module.params['port'] | ||
project_name = module.params['project_name'] | ||
project_id = module.params['project_id'] | ||
node_name = module.params['node_name'] | ||
node_id = module.params['node_id'] | ||
data = module.params['data'] | ||
dest = module.params['dest'] | ||
state = module.params['state'] | ||
if state == "present" and data is None: | ||
module.fail_json(msg="Parameter needs to be passed: data", **result) | ||
|
||
# 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) | ||
if project is None: | ||
module.fail_json(msg="Could not retrieve project. Check name", **result) | ||
|
||
# Retrieve project info | ||
project.get() | ||
|
||
# Define the Node | ||
if node_name is not None: | ||
node = project.get_node(name=node_name) | ||
elif node_id is not None: | ||
node = project.get_node(node_id=node_id) | ||
if node is None: | ||
module.fail_json(msg="Could not retrieve node. Check name", **result) | ||
|
||
# Try to get file data | ||
try: | ||
file_data = node.get_file(path=dest) | ||
except Exception as err: | ||
if "not found" in str(err): | ||
file_data = None | ||
else: | ||
module.fail_json(msg=str(err), exception=traceback.format_exc()) | ||
|
||
if state == "absent": | ||
if file_data is None or file_data == "": | ||
result.update(changed=False) | ||
else: | ||
# Delete node file data | ||
# As of now (GNS3 v2.2.rc5) the DELETE method is not allowed | ||
node_write_file(module, node, dest, "") | ||
result.update(changed=True) | ||
elif state == "present": | ||
if file_data == data: | ||
result.update(changed=False) | ||
else: | ||
node_write_file(module, node, dest, data) | ||
result.update(changed=True) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#!/usr/bin/env python | ||
|
||
ANSIBLE_METADATA = { | ||
"metadata_version": "1.1", | ||
"status": ["preview"], | ||
"supported_by": "community", | ||
} | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: gns3_project_file | ||
short_description: Updates/creates a file on a project directory | ||
version_added: '2.8' | ||
description: | ||
- "Updates/creates a file on a project directory on the GNS3 server" | ||
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 | ||
data: | ||
description: | ||
- The text to insert. | ||
type: str | ||
dest: | ||
description: | ||
- Node destination path. Like 'etc/network/interfaces' | ||
type: str | ||
required: true | ||
state: | ||
description: | ||
- If the file should present or absent | ||
type: str | ||
choices: ['present', 'absent'] | ||
default: present | ||
""" | ||
|
||
EXAMPLES = """ | ||
# Retrieve the GNS3 server version | ||
- name: Get the server version | ||
gns3_project_file: | ||
url: http://localhost | ||
port: 3080 | ||
project_name: test_lab | ||
data: | | ||
Hello this is a README! | ||
dest: README.txt | ||
""" | ||
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 project_write_file(module, project, path, data): | ||
"Writes text data into specified path of the project" | ||
try: | ||
project.write_file(path=path, data=data) | ||
except Exception as err: | ||
module.fail_json(msg=str(err), exception=traceback.format_exc()) | ||
|
||
|
||
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), | ||
data=dict(type="str", default=None), | ||
dest=dict(type="str", required=True), | ||
state=dict(type="str", choices=["present", "absent"], default="present"), | ||
), | ||
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) | ||
|
||
server_url = module.params['url'] | ||
server_port = module.params['port'] | ||
project_name = module.params['project_name'] | ||
project_id = module.params['project_id'] | ||
data = module.params['data'] | ||
dest = module.params['dest'] | ||
state = module.params['state'] | ||
if state == "present" and data is None: | ||
module.fail_json(msg="Parameter needs to be passed: data", **result) | ||
|
||
# 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) | ||
if project is None: | ||
module.fail_json(msg="Could not retrieve project. Check name", **result) | ||
|
||
# Retrieve project info | ||
project.get() | ||
|
||
# Try to get file data | ||
try: | ||
file_data = project.get_file(path=dest) | ||
except Exception as err: | ||
if "Not Found" in str(err): | ||
file_data = None | ||
else: | ||
module.fail_json(msg=str(err), exception=traceback.format_exc()) | ||
|
||
if state == "absent": | ||
if file_data is None or file_data == "": | ||
result.update(changed=False) | ||
else: | ||
# Delete project file data | ||
# As of now (GNS3 v2.2.rc5) the DELETE method is not allowed | ||
project_write_file(module, project, dest, "") | ||
result.update(changed=True) | ||
elif state == "present": | ||
if file_data == data: | ||
result.update(changed=False) | ||
else: | ||
project_write_file(module, project, dest, data) | ||
result.update(changed=True) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- hosts: localhost | ||
tasks: | ||
- name: Play with a dummy file to node | ||
gns3_node_file: | ||
url: "{{ gns3_url }}" | ||
project_name: "{{ gns3_project_name }}" | ||
node_name: alpine-1 | ||
state: present | ||
dest: /etc/network/dummy_file | ||
data: | | ||
# Some data to insert on the file | ||
auto eth0 | ||
iface eth0 inect dhcp | ||
- name: Play with a dummy file on project | ||
gns3_project_file: | ||
url: "{{ gns3_url }}" | ||
project_name: "{{ gns3_project_name }}" | ||
state: present | ||
dest: README.txt | ||
data: | | ||
Hello! this is a README |
Oops, something went wrong.