|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2021, Andrew Simmons (@andybro19) <[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 | +ANSIBLE_METADATA = { |
| 11 | + "metadata_version": "1.1", |
| 12 | + "status": ["preview"], |
| 13 | + "supported_by": "community", |
| 14 | +} |
| 15 | + |
| 16 | +DOCUMENTATION = r""" |
| 17 | +--- |
| 18 | +module: netbox_location |
| 19 | +short_description: Create, update or delete locations within NetBox |
| 20 | +description: |
| 21 | + - Creates, updates or removes locations from NetBox |
| 22 | +notes: |
| 23 | + - Tags should be defined as a YAML list |
| 24 | + - This should be ran with connection C(local) and hosts C(localhost) |
| 25 | +author: |
| 26 | + - Andrew Simmons (@andybro19) |
| 27 | +requirements: |
| 28 | + - pynetbox |
| 29 | +version_added: '4.0.0' |
| 30 | +options: |
| 31 | + netbox_url: |
| 32 | + description: |
| 33 | + - URL of the NetBox instance resolvable by Ansible control host |
| 34 | + required: true |
| 35 | + type: str |
| 36 | + netbox_token: |
| 37 | + description: |
| 38 | + - The token created within NetBox to authorize API access |
| 39 | + required: true |
| 40 | + type: str |
| 41 | + cert: |
| 42 | + description: |
| 43 | + - Certificate path |
| 44 | + required: false |
| 45 | + type: raw |
| 46 | + data: |
| 47 | + type: dict |
| 48 | + description: |
| 49 | + - Defines the location configuration |
| 50 | + suboptions: |
| 51 | + name: |
| 52 | + description: |
| 53 | + - The name of the location |
| 54 | + required: true |
| 55 | + type: str |
| 56 | + slug: |
| 57 | + description: |
| 58 | + - The slugified version of the name or custom slug. |
| 59 | + - This is auto-generated following NetBox rules if not provided |
| 60 | + required: false |
| 61 | + type: str |
| 62 | + site: |
| 63 | + description: |
| 64 | + - Required if I(state=present) and the location does not exist yet |
| 65 | + required: false |
| 66 | + type: raw |
| 67 | + parent_location: |
| 68 | + description: |
| 69 | + - The parent location the location will be associated with |
| 70 | + required: false |
| 71 | + type: raw |
| 72 | + description: |
| 73 | + description: |
| 74 | + - The description of the location |
| 75 | + required: false |
| 76 | + type: str |
| 77 | + required: true |
| 78 | + state: |
| 79 | + description: |
| 80 | + - Use C(present) or C(absent) for adding or removing. |
| 81 | + choices: [ absent, present ] |
| 82 | + default: present |
| 83 | + type: str |
| 84 | + query_params: |
| 85 | + description: |
| 86 | + - This can be used to override the specified values in ALLOWED_QUERY_PARAMS that is defined |
| 87 | + - in plugins/module_utils/netbox_utils.py and provides control to users on what may make |
| 88 | + - an object unique in their environment. |
| 89 | + required: false |
| 90 | + type: list |
| 91 | + elements: str |
| 92 | + validate_certs: |
| 93 | + description: |
| 94 | + - If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates. |
| 95 | + default: true |
| 96 | + type: raw |
| 97 | +""" |
| 98 | + |
| 99 | +EXAMPLES = r""" |
| 100 | +- name: "Test NetBox modules" |
| 101 | + connection: local |
| 102 | + hosts: localhost |
| 103 | + gather_facts: False |
| 104 | +
|
| 105 | + tasks: |
| 106 | + - name: Create location within NetBox with only required information |
| 107 | + netbox.netbox.netbox_location: |
| 108 | + netbox_url: http://netbox.local |
| 109 | + netbox_token: thisIsMyToken |
| 110 | + data: |
| 111 | + name: Test location |
| 112 | + site: Test Site |
| 113 | + state: present |
| 114 | +
|
| 115 | + - name: Create location within NetBox with a parent location |
| 116 | + netbox.netbox.netbox_location: |
| 117 | + netbox_url: http://netbox.local |
| 118 | + netbox_token: thisIsMyToken |
| 119 | + data: |
| 120 | + name: Child location |
| 121 | + site: Test Site |
| 122 | + parent_location: Test location |
| 123 | + state: present |
| 124 | +
|
| 125 | + - name: Delete location within NetBox |
| 126 | + netbox.netbox.netbox_location: |
| 127 | + netbox_url: http://netbox.local |
| 128 | + netbox_token: thisIsMyToken |
| 129 | + data: |
| 130 | + name: Test location |
| 131 | + state: absent |
| 132 | +""" |
| 133 | + |
| 134 | +RETURN = r""" |
| 135 | +location: |
| 136 | + description: Serialized object as created or already existent within NetBox |
| 137 | + returned: success (when I(state=present)) |
| 138 | + type: dict |
| 139 | +msg: |
| 140 | + description: Message indicating failure or info about what has been achieved |
| 141 | + returned: always |
| 142 | + type: str |
| 143 | +""" |
| 144 | + |
| 145 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 146 | + NetboxAnsibleModule, |
| 147 | + NETBOX_ARG_SPEC, |
| 148 | +) |
| 149 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import ( |
| 150 | + NetboxDcimModule, |
| 151 | + NB_LOCATIONS, |
| 152 | +) |
| 153 | +from copy import deepcopy |
| 154 | + |
| 155 | + |
| 156 | +def main(): |
| 157 | + """ |
| 158 | + Main entry point for module execution |
| 159 | + """ |
| 160 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 161 | + argument_spec.update( |
| 162 | + dict( |
| 163 | + data=dict( |
| 164 | + type="dict", |
| 165 | + required=True, |
| 166 | + options=dict( |
| 167 | + name=dict(required=True, type="str"), |
| 168 | + slug=dict(required=False, type="str"), |
| 169 | + site=dict(required=False, type="raw"), |
| 170 | + parent_location=dict(required=False, type="raw"), |
| 171 | + description=dict(required=False, type="str"), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ) |
| 175 | + ) |
| 176 | + |
| 177 | + required_if = [("state", "present", ["name"]), ("state", "absent", ["name"])] |
| 178 | + |
| 179 | + module = NetboxAnsibleModule( |
| 180 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 181 | + ) |
| 182 | + |
| 183 | + netbox_location = NetboxDcimModule(module, NB_LOCATIONS) |
| 184 | + netbox_location.run() |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": # pragma: no cover |
| 188 | + main() |
0 commit comments