diff --git a/plugins/module_utils/netbox_extras.py b/plugins/module_utils/netbox_extras.py index 4a8439279..36f4bf864 100644 --- a/plugins/module_utils/netbox_extras.py +++ b/plugins/module_utils/netbox_extras.py @@ -18,6 +18,7 @@ NB_EXPORT_TEMPLATES = "export_templates" NB_JOURNAL_ENTRIES = "journal_entries" NB_WEBHOOKS = "webhooks" +NB_CONFIG_TEMPLATES = "config_templates" class NetboxExtrasModule(NetboxModule): @@ -37,6 +38,7 @@ def run(self): to create/update/delete the endpoint objects Supported endpoints: - config_contexts + - config_templates - tags - journal entries """ diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index e42a8ed10..627205546 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -79,6 +79,7 @@ ], extras=[ "config_contexts", + "config_templates", "tags", "custom_fields", "custom_links", @@ -125,6 +126,7 @@ cluster_group="slug", cluster_type="slug", config_context="name", + config_template="name", contact_group="name", contact_role="name", custom_field="name", @@ -207,6 +209,7 @@ "cluster_types": "cluster_types", "component": "interfaces", "config_context": "config_contexts", + "config_template": "config_templates", "contact_groups": "contact_groups", "dcim.consoleport": "console_ports", "dcim.consoleserverport": "console_server_ports", @@ -309,6 +312,7 @@ "cluster_groups": "cluster_group", "cluster_types": "cluster_type", "config_contexts": "config_context", + "config_templates": "config_template", "console_ports": "console_port", "console_port_templates": "console_port_template", "console_server_ports": "console_server_port", @@ -407,6 +411,7 @@ "tags", ] ), + "config_template": set(["name"]), "console_port": set(["name", "device"]), "console_port_template": set(["name", "device_type"]), "console_server_port": set(["name", "device"]), diff --git a/plugins/modules/netbox_config_template.py b/plugins/modules/netbox_config_template.py new file mode 100644 index 000000000..bf821cdb2 --- /dev/null +++ b/plugins/modules/netbox_config_template.py @@ -0,0 +1,138 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: (c) 2023, Antoine Dunn (@MinDBreaK) <15737031+MinDBreaK@users.noreply.github.com> +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +DOCUMENTATION = r""" +--- +module: netbox_config_template +short_description: Creates or removes config templates from NetBox +description: + - Creates or removes config templates from NetBox +notes: + - Tags should be defined as a YAML list +author: + - Antoine Dunn (@mindbreak) +requirements: + - pynetbox +version_added: "3.15.0" +extends_documentation_fragment: + - netbox.netbox.common +options: + data: + required: true + type: dict + description: + - Defines the config template configuration + suboptions: + name: + description: + - Config template name + required: true + type: str + description: + description: + - Template description. Max length 200 characters + required: false + type: str + tags: + description: + - Any tags that the device may need to be associated with + required: false + type: list + elements: raw + environment_params: + description: + - Any additional parameters to pass when constructing the Jinja2 environment + required: false + type: dict + template_code: + description: + - The template code to be rendered. + required: false + type: str +""" + +EXAMPLES = r""" +- name: "Test config template creation/deletion" + connection: local + hosts: localhost + gather_facts: False + tasks: + - name: Create config template + netbox.netbox.netbox_route_target: + netbox_url: http://netbox.local + netbox_token: thisIsMyToken + data: + name: "thisIsMyTemplateName" + tags: + - Cloud + template_code: | + #cloud-config + packages: + - ansible + + - name: Delete config template + netbox.netbox.netbox_route_target: + netbox_url: http://netbox.local + netbox_token: thisIsMyToken + data: + name: "thisIsMyTemplateName" + state: absent +""" + +RETURN = r""" +config_templates: + description: Serialized object as created/existent/updated/deleted within NetBox + returned: always + type: dict +msg: + description: Message indicating failure or info about what has been achieved + returned: always + type: str +""" + +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( + NetboxAnsibleModule, + NETBOX_ARG_SPEC, +) +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_extras import ( + NetboxExtrasModule, + NB_CONFIG_TEMPLATES, +) +from copy import deepcopy + + +def main(): + """ + Main entry point for module execution + """ + argument_spec = deepcopy(NETBOX_ARG_SPEC) + argument_spec.update( + dict( + data=dict( + type="dict", + required=True, + options=dict( + name=dict(required=True, type="str"), + description=dict(required=False, type="str"), + template_code=dict(required=False, type="str"), + tags=dict(required=False, type="list", elements="raw"), + environment_params=dict(required=False, type="dict"), + ), + ), + ) + ) + + module = NetboxAnsibleModule(argument_spec=argument_spec, supports_check_mode=True) + + netbox_config_template = NetboxExtrasModule(module, NB_CONFIG_TEMPLATES) + netbox_config_template.run() + + +if __name__ == "__main__": # pragma: no cover + main() diff --git a/tests/integration/targets/v3.6/tasks/main.yml b/tests/integration/targets/v3.6/tasks/main.yml index 0e44b6cca..1b232374e 100644 --- a/tests/integration/targets/v3.6/tasks/main.yml +++ b/tests/integration/targets/v3.6/tasks/main.yml @@ -305,3 +305,8 @@ - netbox_fhrp_group_assignmen tags: - netbox_fhrp_group_assignmen + +- name: "NETBOX_CONFIG_TEMPLATE" + include_tasks: "netbox_config_template.yml" + tags: + - netbox_config_template diff --git a/tests/integration/targets/v3.6/tasks/netbox_config_template.yml b/tests/integration/targets/v3.6/tasks/netbox_config_template.yml new file mode 100644 index 000000000..9abf3ea05 --- /dev/null +++ b/tests/integration/targets/v3.6/tasks/netbox_config_template.yml @@ -0,0 +1,82 @@ +--- +## +## +### NETBOX_CONFIG_TEMPLATES +## +## +- name: "CONFIG_TEMPLATES 1: Necessary info creation" + netbox.netbox.netbox_config_template: + netbox_url: http://localhost:32768 + netbox_token: 0123456789abcdef0123456789abcdef01234567 + data: + name: "test_template" + description: "Test template" + template_code: "test template" + state: present + register: test_one + +- name: "CONFIG_TEMPLATES 1: ASSERT - Necessary info creation" + assert: + that: + - test_one is changed + - test_one['diff']['before']['state'] == "absent" + - test_one['diff']['after']['state'] == "present" + - test_one['config_template']['name'] == "test_template" + - test_one['config_template']['description'] == "Test template" + - test_one['config_template']['template_code'] == "test template" + - test_one['msg'] == "config_template test_template created" + +- name: "CONFIG_TEMPLATES 2: Create duplicate" + netbox.netbox.netbox_config_template: + netbox_url: http://localhost:32768 + netbox_token: 0123456789abcdef0123456789abcdef01234567 + data: + name: "test_template" + description: "Test template" + template_code: "test template" + state: present + register: test_two + +- name: "CONFIG_TEMPLATES 2: ASSERT - Create duplicate" + assert: + that: + - not test_two['changed'] + - test_two['config_template']['name'] == "test_template" + - test_two['msg'] == "config_template test_template already exists" + +- name: "CONFIG_TEMPLATES 3: Update data" + netbox.netbox.netbox_config_template: + netbox_url: http://localhost:32768 + netbox_token: 0123456789abcdef0123456789abcdef01234567 + data: + name: "test_template" + description: "Updated test template" + template_code: "updated test template" + state: present + register: test_three + +- name: "CONFIG_TEMPLATES 3: ASSERT - Updated" + assert: + that: + - test_three is changed + - test_three['diff']['after']['template_code'] == "updated test template" + - test_three['diff']['after']['description'] == "Updated test template" + - test_three['config_template']['name'] == "test_template" + - test_three['msg'] == "config_template test_template updated" + +- name: "CONFIG_TEMPLATES 4: Delete" + netbox.netbox.netbox_config_template: + netbox_url: http://localhost:32768 + netbox_token: 0123456789abcdef0123456789abcdef01234567 + data: + name: "test_template" + state: absent + register: test_four + +- name: "CONFIG_TEMPLATES 4: ASSERT - Deleted" + assert: + that: + - test_four is changed + - test_four['diff']['after']['state'] == "absent" + - test_four['config_template']['name'] == "test_template" + - test_four['msg'] == "config_template test_template deleted"