|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# © 2020 Nokia |
| 4 | +# Licensed under the GNU General Public License v3.0 only |
| 5 | +# SPDX-License-Identifier: GPL-3.0-only |
| 6 | + |
| 7 | +from __future__ import absolute_import, division, print_function |
| 8 | + |
| 9 | +__metaclass__ = type |
| 10 | + |
| 11 | +ANSIBLE_METADATA = { |
| 12 | + "metadata_version": "1.1", |
| 13 | + "status": ["preview"], |
| 14 | + "supported_by": "community", |
| 15 | +} |
| 16 | + |
| 17 | +DOCUMENTATION = r""" |
| 18 | +--- |
| 19 | +module: netbox_console_port |
| 20 | +short_description: Create, update or delete console ports within Netbox |
| 21 | +description: |
| 22 | + - Creates, updates or removes console ports from Netbox |
| 23 | +notes: |
| 24 | + - Tags should be defined as a YAML list |
| 25 | + - This should be ran with connection C(local) and hosts C(localhost) |
| 26 | +author: |
| 27 | + - Tobias Groß (@toerb) |
| 28 | +requirements: |
| 29 | + - pynetbox |
| 30 | +version_added: '0.2.3' |
| 31 | +options: |
| 32 | + netbox_url: |
| 33 | + description: |
| 34 | + - URL of the Netbox instance resolvable by Ansible control host |
| 35 | + required: true |
| 36 | + type: str |
| 37 | + netbox_token: |
| 38 | + description: |
| 39 | + - The token created within Netbox to authorize API access |
| 40 | + required: true |
| 41 | + type: str |
| 42 | + data: |
| 43 | + type: dict |
| 44 | + required: true |
| 45 | + description: |
| 46 | + - Defines the console port configuration |
| 47 | + suboptions: |
| 48 | + device: |
| 49 | + description: |
| 50 | + - The device the console port is attached to |
| 51 | + required: true |
| 52 | + type: raw |
| 53 | + name: |
| 54 | + description: |
| 55 | + - The name of the console port |
| 56 | + required: true |
| 57 | + type: str |
| 58 | + type: |
| 59 | + description: |
| 60 | + - The type of the console port |
| 61 | + choices: |
| 62 | + - de-9 |
| 63 | + - db-25 |
| 64 | + - rj-11 |
| 65 | + - rj-12 |
| 66 | + - rj-45 |
| 67 | + - usb-a |
| 68 | + - usb-b |
| 69 | + - usb-c |
| 70 | + - usb-mini-a |
| 71 | + - usb-mini-b |
| 72 | + - usb-micro-a |
| 73 | + - usb-micro-b |
| 74 | + - other |
| 75 | + required: false |
| 76 | + type: str |
| 77 | + description: |
| 78 | + description: |
| 79 | + - Description of the console port |
| 80 | + required: false |
| 81 | + type: str |
| 82 | + tags: |
| 83 | + description: |
| 84 | + - Any tags that the console port may need to be associated with |
| 85 | + required: false |
| 86 | + type: list |
| 87 | + state: |
| 88 | + description: |
| 89 | + - Use C(present) or C(absent) for adding or removing. |
| 90 | + choices: [ absent, present ] |
| 91 | + default: present |
| 92 | + type: str |
| 93 | + query_params: |
| 94 | + description: |
| 95 | + - This can be used to override the specified values in ALLOWED_QUERY_PARAMS that is defined |
| 96 | + - in plugins/module_utils/netbox_utils.py and provides control to users on what may make |
| 97 | + - an object unique in their environment. |
| 98 | + required: false |
| 99 | + type: list |
| 100 | + validate_certs: |
| 101 | + description: |
| 102 | + - If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates. |
| 103 | + default: true |
| 104 | + type: raw |
| 105 | +""" |
| 106 | + |
| 107 | +EXAMPLES = r""" |
| 108 | +- name: "Test Netbox modules" |
| 109 | + connection: local |
| 110 | + hosts: localhost |
| 111 | + gather_facts: False |
| 112 | +
|
| 113 | + tasks: |
| 114 | + - name: Create console port within Netbox with only required information |
| 115 | + netbox_console_port: |
| 116 | + netbox_url: http://netbox.local |
| 117 | + netbox_token: thisIsMyToken |
| 118 | + data: |
| 119 | + name: Test Console Port |
| 120 | + device: Test Device |
| 121 | + state: present |
| 122 | +
|
| 123 | + - name: Update console port with other fields |
| 124 | + netbox_console_port: |
| 125 | + netbox_url: http://netbox.local |
| 126 | + netbox_token: thisIsMyToken |
| 127 | + data: |
| 128 | + name: Test Console Port |
| 129 | + device: Test Device |
| 130 | + type: usb-a |
| 131 | + description: console port description |
| 132 | + state: present |
| 133 | +
|
| 134 | + - name: Delete console port within netbox |
| 135 | + netbox_console_port: |
| 136 | + netbox_url: http://netbox.local |
| 137 | + netbox_token: thisIsMyToken |
| 138 | + data: |
| 139 | + name: Test Console Port |
| 140 | + device: Test Device |
| 141 | + state: absent |
| 142 | +""" |
| 143 | + |
| 144 | +RETURN = r""" |
| 145 | +console_port: |
| 146 | + description: Serialized object as created or already existent within Netbox |
| 147 | + returned: success (when I(state=present)) |
| 148 | + type: dict |
| 149 | +msg: |
| 150 | + description: Message indicating failure or info about what has been achieved |
| 151 | + returned: always |
| 152 | + type: str |
| 153 | +""" |
| 154 | + |
| 155 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 156 | + NetboxAnsibleModule, |
| 157 | + NETBOX_ARG_SPEC, |
| 158 | +) |
| 159 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import ( |
| 160 | + NetboxDcimModule, |
| 161 | + NB_CONSOLE_PORTS, |
| 162 | +) |
| 163 | +from copy import deepcopy |
| 164 | + |
| 165 | + |
| 166 | +def main(): |
| 167 | + """ |
| 168 | + Main entry point for module execution |
| 169 | + """ |
| 170 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 171 | + argument_spec.update( |
| 172 | + dict( |
| 173 | + data=dict( |
| 174 | + type="dict", |
| 175 | + required=True, |
| 176 | + options=dict( |
| 177 | + device=dict(required=True, type="raw"), |
| 178 | + name=dict(required=True, type="str"), |
| 179 | + type=dict( |
| 180 | + required=False, |
| 181 | + choices=[ |
| 182 | + "de-9", |
| 183 | + "db-25", |
| 184 | + "rj-11", |
| 185 | + "rj-12", |
| 186 | + "rj-45", |
| 187 | + "usb-a", |
| 188 | + "usb-b", |
| 189 | + "usb-c", |
| 190 | + "usb-mini-a", |
| 191 | + "usb-mini-b", |
| 192 | + "usb-micro-a", |
| 193 | + "usb-micro-b", |
| 194 | + "other", |
| 195 | + ], |
| 196 | + type="str", |
| 197 | + ), |
| 198 | + description=dict(required=False, type="str"), |
| 199 | + tags=dict(required=False, type="list"), |
| 200 | + ), |
| 201 | + ), |
| 202 | + ) |
| 203 | + ) |
| 204 | + |
| 205 | + required_if = [ |
| 206 | + ("state", "present", ["device", "name"]), |
| 207 | + ("state", "absent", ["device", "name"]), |
| 208 | + ] |
| 209 | + |
| 210 | + module = NetboxAnsibleModule( |
| 211 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 212 | + ) |
| 213 | + |
| 214 | + netbox_console_port = NetboxDcimModule(module, NB_CONSOLE_PORTS) |
| 215 | + netbox_console_port.run() |
| 216 | + |
| 217 | + |
| 218 | +if __name__ == "__main__": # pragma: no cover |
| 219 | + main() |
0 commit comments