Skip to content

Commit 1529e6c

Browse files
authored
feat: Add config-template support (#1090)
1 parent 63b00ca commit 1529e6c

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-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",
@@ -310,6 +313,7 @@
310313
"cluster_groups": "cluster_group",
311314
"cluster_types": "cluster_type",
312315
"config_contexts": "config_context",
316+
"config_templates": "config_template",
313317
"console_ports": "console_port",
314318
"console_port_templates": "console_port_template",
315319
"console_server_ports": "console_server_port",
@@ -408,6 +412,7 @@
408412
"tags",
409413
]
410414
),
415+
"config_template": set(["name"]),
411416
"console_port": set(["name", "device"]),
412417
"console_port_template": set(["name", "device_type"]),
413418
"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) <15737031+MinDBreaK@users.noreply.github.com>
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.15.0"
23+
extends_documentation_fragment:
24+
- netbox.netbox.common
25+
options:
26+
data:
27+
required: true
28+
type: dict
29+
description:
30+
- Defines the config template 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 config template
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()

tests/integration/targets/v3.6/tasks/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,8 @@
305305
- netbox_fhrp_group_assignmen
306306
tags:
307307
- netbox_fhrp_group_assignmen
308+
309+
- name: "NETBOX_CONFIG_TEMPLATE"
310+
include_tasks: "netbox_config_template.yml"
311+
tags:
312+
- netbox_config_template
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
##
3+
##
4+
### NETBOX_CONFIG_TEMPLATES
5+
##
6+
##
7+
- name: "CONFIG_TEMPLATES 1: Necessary info creation"
8+
netbox.netbox.netbox_config_template:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
name: "test_template"
13+
description: "Test template"
14+
template_code: "test template"
15+
state: present
16+
register: test_one
17+
18+
- name: "CONFIG_TEMPLATES 1: ASSERT - Necessary info creation"
19+
assert:
20+
that:
21+
- test_one is changed
22+
- test_one['diff']['before']['state'] == "absent"
23+
- test_one['diff']['after']['state'] == "present"
24+
- test_one['config_template']['name'] == "test_template"
25+
- test_one['config_template']['description'] == "Test template"
26+
- test_one['config_template']['template_code'] == "test template"
27+
- test_one['msg'] == "config_template test_template created"
28+
29+
- name: "CONFIG_TEMPLATES 2: Create duplicate"
30+
netbox.netbox.netbox_config_template:
31+
netbox_url: http://localhost:32768
32+
netbox_token: 0123456789abcdef0123456789abcdef01234567
33+
data:
34+
name: "test_template"
35+
description: "Test template"
36+
template_code: "test template"
37+
state: present
38+
register: test_two
39+
40+
- name: "CONFIG_TEMPLATES 2: ASSERT - Create duplicate"
41+
assert:
42+
that:
43+
- not test_two['changed']
44+
- test_two['config_template']['name'] == "test_template"
45+
- test_two['msg'] == "config_template test_template already exists"
46+
47+
- name: "CONFIG_TEMPLATES 3: Update data"
48+
netbox.netbox.netbox_config_template:
49+
netbox_url: http://localhost:32768
50+
netbox_token: 0123456789abcdef0123456789abcdef01234567
51+
data:
52+
name: "test_template"
53+
description: "Updated test template"
54+
template_code: "updated test template"
55+
state: present
56+
register: test_three
57+
58+
- name: "CONFIG_TEMPLATES 3: ASSERT - Updated"
59+
assert:
60+
that:
61+
- test_three is changed
62+
- test_three['diff']['after']['template_code'] == "updated test template"
63+
- test_three['diff']['after']['description'] == "Updated test template"
64+
- test_three['config_template']['name'] == "test_template"
65+
- test_three['msg'] == "config_template test_template updated"
66+
67+
- name: "CONFIG_TEMPLATES 4: Delete"
68+
netbox.netbox.netbox_config_template:
69+
netbox_url: http://localhost:32768
70+
netbox_token: 0123456789abcdef0123456789abcdef01234567
71+
data:
72+
name: "test_template"
73+
state: absent
74+
register: test_four
75+
76+
- name: "CONFIG_TEMPLATES 4: ASSERT - Deleted"
77+
assert:
78+
that:
79+
- test_four is changed
80+
- test_four['diff']['after']['state'] == "absent"
81+
- test_four['config_template']['name'] == "test_template"
82+
- test_four['msg'] == "config_template test_template deleted"

0 commit comments

Comments
 (0)