-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
0 deletions.
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
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() |
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
82 changes: 82 additions & 0 deletions
82
tests/integration/targets/v3.6/tasks/netbox_config_template.yml
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,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" |