Skip to content

Commit 6e1939f

Browse files
committed
feat: Add config-template support
1 parent 775f871 commit 6e1939f

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

plugins/module_utils/netbox_extras.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
NB_EXPORT_TEMPLATES = "export_templates"
1919
NB_JOURNAL_ENTRIES = "journal_entries"
2020
NB_WEBHOOKS = "webhooks"
21+
NB_CONFIG_TEMPLATES = "config_templates"
2122

2223

2324
class NetboxExtrasModule(NetboxModule):
@@ -37,6 +38,7 @@ def run(self):
3738
to create/update/delete the endpoint objects
3839
Supported endpoints:
3940
- config_contexts
41+
- config_templates
4042
- tags
4143
- journal entries
4244
"""

plugins/module_utils/netbox_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
],
8080
extras=[
8181
"config_contexts",
82+
"config_templates",
8283
"tags",
8384
"custom_fields",
8485
"custom_links",
@@ -125,6 +126,7 @@
125126
cluster_group="slug",
126127
cluster_type="slug",
127128
config_context="name",
129+
config_template="name",
128130
contact_group="name",
129131
contact_role="name",
130132
custom_field="name",
@@ -207,6 +209,7 @@
207209
"cluster_types": "cluster_types",
208210
"component": "interfaces",
209211
"config_context": "config_contexts",
212+
"config_template": "config_templates",
210213
"contact_groups": "contact_groups",
211214
"dcim.consoleport": "console_ports",
212215
"dcim.consoleserverport": "console_server_ports",
@@ -309,6 +312,7 @@
309312
"cluster_groups": "cluster_group",
310313
"cluster_types": "cluster_type",
311314
"config_contexts": "config_context",
315+
"config_templates": "config_template",
312316
"console_ports": "console_port",
313317
"console_port_templates": "console_port_template",
314318
"console_server_ports": "console_server_port",
@@ -407,6 +411,7 @@
407411
"tags",
408412
]
409413
),
414+
"config_template": set(["name"]),
410415
"console_port": set(["name", "device"]),
411416
"console_port_template": set(["name", "device_type"]),
412417
"console_server_port": set(["name", "device"]),
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2023, Antoine Dunn (@MinDBreaK) <[email protected]>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
from __future__ import absolute_import, division, print_function
7+
8+
__metaclass__ = type
9+
10+
DOCUMENTATION = r"""
11+
---
12+
module: netbox_config_template
13+
short_description: Creates or removes config templates from NetBox
14+
description:
15+
- Creates or removes config templates from NetBox
16+
notes:
17+
- Tags should be defined as a YAML list
18+
author:
19+
- Antoine Dunn (@mindbreak)
20+
requirements:
21+
- pynetbox
22+
version_added: "3.14.0"
23+
extends_documentation_fragment:
24+
- netbox.netbox.common
25+
options:
26+
data:
27+
required: true
28+
type: dict
29+
description:
30+
- Defines the route target configuration
31+
suboptions:
32+
name:
33+
description:
34+
- Config template name
35+
required: true
36+
type: str
37+
description:
38+
description:
39+
- Template description. Max length 200 characters
40+
required: false
41+
type: str
42+
tags:
43+
description:
44+
- Any tags that the device may need to be associated with
45+
required: false
46+
type: list
47+
elements: raw
48+
environment_params:
49+
description:
50+
- Any additional parameters to pass when constructing the Jinja2 environment
51+
required: false
52+
type: dict
53+
template_code:
54+
description:
55+
- The template code to be rendered.
56+
required: false
57+
type: str
58+
"""
59+
60+
EXAMPLES = r"""
61+
- name: "Test config template creation/deletion"
62+
connection: local
63+
hosts: localhost
64+
gather_facts: False
65+
tasks:
66+
- name: Create config template
67+
netbox.netbox.netbox_route_target:
68+
netbox_url: http://netbox.local
69+
netbox_token: thisIsMyToken
70+
data:
71+
name: "thisIsMyTemplateName"
72+
tags:
73+
- Cloud
74+
template_code: |
75+
#cloud-config
76+
packages:
77+
- ansible
78+
79+
- name: Delete Route Targets
80+
netbox.netbox.netbox_route_target:
81+
netbox_url: http://netbox.local
82+
netbox_token: thisIsMyToken
83+
data:
84+
name: "thisIsMyTemplateName"
85+
state: absent
86+
"""
87+
88+
RETURN = r"""
89+
config_templates:
90+
description: Serialized object as created/existent/updated/deleted within NetBox
91+
returned: always
92+
type: dict
93+
msg:
94+
description: Message indicating failure or info about what has been achieved
95+
returned: always
96+
type: str
97+
"""
98+
99+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
100+
NetboxAnsibleModule,
101+
NETBOX_ARG_SPEC,
102+
)
103+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_extras import (
104+
NetboxExtrasModule,
105+
NB_CONFIG_TEMPLATES,
106+
)
107+
from copy import deepcopy
108+
109+
110+
def main():
111+
"""
112+
Main entry point for module execution
113+
"""
114+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
115+
argument_spec.update(
116+
dict(
117+
data=dict(
118+
type="dict",
119+
required=True,
120+
options=dict(
121+
name=dict(required=True, type="str"),
122+
description=dict(required=False, type="str"),
123+
template_code=dict(required=False, type="str"),
124+
tags=dict(required=False, type="list", elements="raw"),
125+
environment_params=dict(required=False, type="dict"),
126+
),
127+
),
128+
)
129+
)
130+
131+
module = NetboxAnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
132+
133+
netbox_config_template = NetboxExtrasModule(module, NB_CONFIG_TEMPLATES)
134+
netbox_config_template.run()
135+
136+
137+
if __name__ == "__main__": # pragma: no cover
138+
main()

0 commit comments

Comments
 (0)