|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2025, Martin Rødvand (@rodvand) <[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_mac_address |
| 13 | +short_description: Create, update or delete MAC addresses within NetBox |
| 14 | +description: |
| 15 | + - Creates, updates or removes MAC addresses from NetBox |
| 16 | +notes: |
| 17 | + - Tags should be defined as a YAML list |
| 18 | + - This should be ran with connection C(local) and hosts C(localhost) |
| 19 | +author: |
| 20 | + - Martin Rødvand (@rodvand) |
| 21 | +requirements: |
| 22 | + - pynetbox |
| 23 | +version_added: "3.21.0" |
| 24 | +extends_documentation_fragment: |
| 25 | + - netbox.netbox.common |
| 26 | +options: |
| 27 | + data: |
| 28 | + type: dict |
| 29 | + description: |
| 30 | + - Defines the MAC address configuration |
| 31 | + suboptions: |
| 32 | + mac_address: |
| 33 | + description: |
| 34 | + - The MAC address |
| 35 | + required: true |
| 36 | + type: str |
| 37 | + assigned_object: |
| 38 | + description: |
| 39 | + - The object to assign this MAC address to |
| 40 | + required: false |
| 41 | + type: dict |
| 42 | + description: |
| 43 | + description: |
| 44 | + - Description of the MAC address |
| 45 | + required: false |
| 46 | + type: str |
| 47 | + comments: |
| 48 | + description: |
| 49 | + - Comments for the MAC address |
| 50 | + required: false |
| 51 | + type: str |
| 52 | + tags: |
| 53 | + description: |
| 54 | + - Any tags that the MAC address may need to be associated with |
| 55 | + required: false |
| 56 | + type: list |
| 57 | + elements: raw |
| 58 | + custom_fields: |
| 59 | + description: |
| 60 | + - Must exist in NetBox and in key/value format |
| 61 | + required: false |
| 62 | + type: dict |
| 63 | + required: true |
| 64 | +""" |
| 65 | + |
| 66 | +EXAMPLES = r""" |
| 67 | +- name: "Test NetBox MAC address module" |
| 68 | + connection: local |
| 69 | + hosts: localhost |
| 70 | + gather_facts: false |
| 71 | +
|
| 72 | + tasks: |
| 73 | + - name: Create MAC address within NetBox with only required information |
| 74 | + netbox.netbox.netbox_mac_address: |
| 75 | + netbox_url: http://netbox.local |
| 76 | + netbox_token: thisIsMyToken |
| 77 | + data: |
| 78 | + mac_address: "00:11:22:33:44:55" |
| 79 | + state: present |
| 80 | +
|
| 81 | + - name: Create MAC address with interface assignment |
| 82 | + netbox.netbox.netbox_mac_address: |
| 83 | + netbox_url: http://netbox.local |
| 84 | + netbox_token: thisIsMyToken |
| 85 | + data: |
| 86 | + mac_address: "AA:BB:CC:DD:EE:FF" |
| 87 | + assigned_object: |
| 88 | + device: Test Nexus One |
| 89 | + name: Ethernet1/1 |
| 90 | + description: "MAC address for eth1/1" |
| 91 | + tags: |
| 92 | + - Network |
| 93 | + state: present |
| 94 | +
|
| 95 | + - name: Delete MAC address within netbox |
| 96 | + netbox.netbox.netbox_mac_address: |
| 97 | + netbox_url: http://netbox.local |
| 98 | + netbox_token: thisIsMyToken |
| 99 | + data: |
| 100 | + mac_address: "00:11:22:33:44:55" |
| 101 | + state: absent |
| 102 | +""" |
| 103 | + |
| 104 | +RETURN = r""" |
| 105 | +mac_address: |
| 106 | + description: Serialized object as created or already existent within NetBox |
| 107 | + returned: success (when I(state=present)) |
| 108 | + type: dict |
| 109 | +msg: |
| 110 | + description: Message indicating failure or info about what has been achieved |
| 111 | + returned: always |
| 112 | + type: str |
| 113 | +""" |
| 114 | + |
| 115 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 116 | + NetboxAnsibleModule, |
| 117 | + NETBOX_ARG_SPEC, |
| 118 | +) |
| 119 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import ( |
| 120 | + NetboxDcimModule, |
| 121 | + NB_MAC_ADDRESSES, |
| 122 | +) |
| 123 | +from copy import deepcopy |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + """ |
| 128 | + Main entry point for module execution |
| 129 | + """ |
| 130 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 131 | + argument_spec.update( |
| 132 | + dict( |
| 133 | + data=dict( |
| 134 | + type="dict", |
| 135 | + required=True, |
| 136 | + options=dict( |
| 137 | + mac_address=dict(required=True, type="str"), |
| 138 | + assigned_object=dict(required=False, type="dict"), |
| 139 | + description=dict(required=False, type="str"), |
| 140 | + comments=dict(required=False, type="str"), |
| 141 | + tags=dict(required=False, type="list", elements="raw"), |
| 142 | + custom_fields=dict(required=False, type="dict"), |
| 143 | + ), |
| 144 | + ), |
| 145 | + ) |
| 146 | + ) |
| 147 | + |
| 148 | + required_if = [ |
| 149 | + ("state", "present", ["mac_address"]), |
| 150 | + ("state", "absent", ["mac_address"]), |
| 151 | + ] |
| 152 | + |
| 153 | + module = NetboxAnsibleModule( |
| 154 | + argument_spec=argument_spec, |
| 155 | + supports_check_mode=True, |
| 156 | + required_if=required_if, |
| 157 | + ) |
| 158 | + |
| 159 | + netbox_mac_address = NetboxDcimModule(module, NB_MAC_ADDRESSES) |
| 160 | + netbox_mac_address.run() |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": # pragma: no cover |
| 164 | + main() |
0 commit comments