|
| 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