Skip to content

Commit

Permalink
feat: Add config-template support
Browse files Browse the repository at this point in the history
  • Loading branch information
MinDBreaK committed Oct 18, 2023
1 parent 775f871 commit ac1b847
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/module_utils/netbox_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -37,6 +38,7 @@ def run(self):
to create/update/delete the endpoint objects
Supported endpoints:
- config_contexts
- config_templates
- tags
- journal entries
"""
Expand Down
5 changes: 5 additions & 0 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
],
extras=[
"config_contexts",
"config_templates",
"tags",
"custom_fields",
"custom_links",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"]),
Expand Down
138 changes: 138 additions & 0 deletions plugins/modules/netbox_config_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, Antoine Dunn (@MinDBreaK) <[email protected]>
# 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()
5 changes: 5 additions & 0 deletions tests/integration/targets/v3.6/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
82 changes: 82 additions & 0 deletions tests/integration/targets/v3.6/tasks/netbox_config_template.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit ac1b847

Please sign in to comment.