| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, Jana Hoch <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: proxmox_zone |
| 10 |
short_description: Manage Proxmox zone configurations. |
| 11 |
description: |
| 12 |
- Create/Update/Delete proxmox sdn zones. |
| 13 |
author: 'Jana Hoch <[email protected]> (!UNKNOWN)' |
| 14 |
version_added: "1.4.0" |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: none |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
options: |
| 21 |
advertise_subnets: |
| 22 |
description: |
| 23 |
- Advertise EVPN subnets if you have silent hosts. |
| 24 |
type: bool |
| 25 |
bridge: |
| 26 |
description: |
| 27 |
- Specify the bridge interface to use. |
| 28 |
type: str |
| 29 |
bridge_disable_mac_learning: |
| 30 |
description: |
| 31 |
- Disable auto MAC address learning on the bridge interface. |
| 32 |
type: bool |
| 33 |
controller: |
| 34 |
description: |
| 35 |
- FRR router name. |
| 36 |
type: str |
| 37 |
dhcp: |
| 38 |
description: |
| 39 |
- Type of the DHCP backend for this zone. |
| 40 |
type: str |
| 41 |
choices: |
| 42 |
- dnsmasq |
| 43 |
disable_arp_nd_suppression: |
| 44 |
description: |
| 45 |
- Disable IPv4 ARP and IPv6 neighbour discovery suppression. |
| 46 |
type: bool |
| 47 |
dns: |
| 48 |
description: |
| 49 |
- DNS API server. |
| 50 |
type: str |
| 51 |
dnszone: |
| 52 |
description: |
| 53 |
- DNS domain zone. |
| 54 |
type: str |
| 55 |
dp_id: |
| 56 |
description: |
| 57 |
- Faucet dataplane ID. |
| 58 |
type: int |
| 59 |
exitnodes: |
| 60 |
description: |
| 61 |
- List of cluster node names. |
| 62 |
type: str |
| 63 |
exitnodes_local_routing: |
| 64 |
description: |
| 65 |
- Allow exitnodes to connect to EVPN guests. |
| 66 |
type: bool |
| 67 |
exitnodes_primary: |
| 68 |
description: |
| 69 |
- Force traffic to this exit node first. |
| 70 |
type: str |
| 71 |
fabric: |
| 72 |
description: |
| 73 |
- SDN fabric to use as underlay for this VXLAN zone. |
| 74 |
- Either O(fabric) or O(peers) is required for VXLAN zones. |
| 75 |
type: str |
| 76 |
ipam: |
| 77 |
description: |
| 78 |
- Use a specific IPAM. |
| 79 |
type: str |
| 80 |
mac: |
| 81 |
description: |
| 82 |
- Anycast logical router MAC address. |
| 83 |
type: str |
| 84 |
mtu: |
| 85 |
description: |
| 86 |
- Set the Maximum Transmission Unit (MTU). |
| 87 |
type: int |
| 88 |
nodes: |
| 89 |
description: |
| 90 |
- List of cluster node names. |
| 91 |
type: str |
| 92 |
peers: |
| 93 |
description: |
| 94 |
- Peers address list (comma-separated IP addresses). |
| 95 |
- Either O(fabric) or O(peers) is required for VXLAN zones. |
| 96 |
- "Example: 192.168.0.1,192.168.0.2,192.168.0.3" |
| 97 |
type: str |
| 98 |
reversedns: |
| 99 |
description: |
| 100 |
- Reverse DNS API server. |
| 101 |
type: str |
| 102 |
rt_import: |
| 103 |
description: |
| 104 |
- Route-Target import. |
| 105 |
type: str |
| 106 |
state: |
| 107 |
description: |
| 108 |
- The desired state of the zone configuration. |
| 109 |
type: str |
| 110 |
choices: |
| 111 |
- present |
| 112 |
- absent |
| 113 |
default: present |
| 114 |
tag: |
| 115 |
description: |
| 116 |
- Service-VLAN tag. |
| 117 |
type: int |
| 118 |
type: |
| 119 |
description: |
| 120 |
- Specify the type of zone. |
| 121 |
type: str |
| 122 |
choices: |
| 123 |
- evpn |
| 124 |
- faucet |
| 125 |
- qinq |
| 126 |
- simple |
| 127 |
- vlan |
| 128 |
- vxlan |
| 129 |
update: |
| 130 |
description: |
| 131 |
- If O(state=present) and zone exists it'll update. |
| 132 |
type: bool |
| 133 |
default: true |
| 134 |
vlan_protocol: |
| 135 |
description: |
| 136 |
- Specify the VLAN protocol to use. |
| 137 |
type: str |
| 138 |
choices: |
| 139 |
- 802.1q |
| 140 |
- 802.1ad |
| 141 |
vrf_vxlan: |
| 142 |
description: |
| 143 |
- Specify the VRF VXLAN identifier. |
| 144 |
type: int |
| 145 |
vxlan_port: |
| 146 |
description: |
| 147 |
- VXLAN tunnel UDP port (default 4789). |
| 148 |
type: int |
| 149 |
zone: |
| 150 |
description: |
| 151 |
- Unique zone name. |
| 152 |
type: str |
| 153 |
extends_documentation_fragment: |
| 154 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 155 |
- community.proxmox.proxmox.documentation |
| 156 |
- community.proxmox.attributes |
| 157 |
""" |
| 158 |
|
| 159 |
EXAMPLES = r""" |
| 160 |
- name: Create a simple zone |
| 161 |
community.proxmox.proxmox_zone: |
| 162 |
type: simple |
| 163 |
zone: ansible |
| 164 |
state: present |
| 165 |
|
| 166 |
- name: Create a vlan zone |
| 167 |
community.proxmox.proxmox_zone: |
| 168 |
type: vlan |
| 169 |
zone: ansible |
| 170 |
state: present |
| 171 |
bridge: vmbr0 |
| 172 |
|
| 173 |
- name: Create a VXLAN zone with fabric |
| 174 |
community.proxmox.proxmox_zone: |
| 175 |
type: vxlan |
| 176 |
zone: myvxlan |
| 177 |
fabric: my_fabric |
| 178 |
state: present |
| 179 |
|
| 180 |
- name: Create a VXLAN zone with peers |
| 181 |
community.proxmox.proxmox_zone: |
| 182 |
type: vxlan |
| 183 |
zone: myvxlan |
| 184 |
peers: "192.168.0.1,192.168.0.2,192.168.0.3" |
| 185 |
state: present |
| 186 |
|
| 187 |
- name: Delete a zone |
| 188 |
community.proxmox.proxmox_zone: |
| 189 |
type: simple |
| 190 |
zone: ansible |
| 191 |
state: absent |
| 192 |
""" |
| 193 |
|
| 194 |
RETURN = r""" |
| 195 |
zone: |
| 196 |
description: |
| 197 |
- Name of the zone which was created/updated/deleted |
| 198 |
returned: on success |
| 199 |
type: str |
| 200 |
sample: |
| 201 |
test |
| 202 |
""" |
| 203 |
|
| 204 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 205 |
ansible_to_proxmox_bool, |
| 206 |
create_proxmox_module, |
| 207 |
) |
| 208 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_sdn import ProxmoxSdnAnsible |
| 209 |
|
| 210 |
|
| 211 |
def module_args(): |
| 212 |
return dict( |
| 213 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 214 |
update=dict(type="bool", default=True), |
| 215 |
type=dict(type="str", choices=["evpn", "faucet", "qinq", "simple", "vlan", "vxlan"], required=False), |
| 216 |
zone=dict(type="str", required=False), |
| 217 |
advertise_subnets=dict(type="bool", required=False), |
| 218 |
bridge=dict(type="str", required=False), |
| 219 |
bridge_disable_mac_learning=dict(type="bool", required=False), |
| 220 |
controller=dict(type="str", required=False), |
| 221 |
dhcp=dict(type="str", choices=["dnsmasq"], required=False), |
| 222 |
disable_arp_nd_suppression=dict(type="bool", required=False), |
| 223 |
dns=dict(type="str", required=False), |
| 224 |
dnszone=dict(type="str", required=False), |
| 225 |
dp_id=dict(type="int", required=False), |
| 226 |
exitnodes=dict(type="str", required=False), |
| 227 |
exitnodes_local_routing=dict(type="bool", required=False), |
| 228 |
exitnodes_primary=dict(type="str", required=False), |
| 229 |
fabric=dict(type="str", required=False), |
| 230 |
ipam=dict(type="str", required=False), |
| 231 |
mac=dict(type="str", required=False), |
| 232 |
mtu=dict(type="int", required=False), |
| 233 |
nodes=dict(type="str", required=False), |
| 234 |
peers=dict(type="str", required=False), |
| 235 |
reversedns=dict(type="str", required=False), |
| 236 |
rt_import=dict(type="str", required=False), |
| 237 |
tag=dict(type="int", required=False), |
| 238 |
vlan_protocol=dict(type="str", choices=["802.1q", "802.1ad"], required=False), |
| 239 |
vrf_vxlan=dict(type="int", required=False), |
| 240 |
vxlan_port=dict(type="int", required=False), |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
def module_options(): |
| 245 |
return dict( |
| 246 |
supports_check_mode=False, |
| 247 |
required_if=[("state", "present", ["type", "zone"]), ("state", "absent", ["zone"])], |
| 248 |
) |
| 249 |
|
| 250 |
|
| 251 |
class ProxmoxZoneAnsible(ProxmoxSdnAnsible): |
| 252 |
def __init__(self, module): |
| 253 |
super().__init__(module) |
| 254 |
self.params = module.params |
| 255 |
|
| 256 |
def validate_params(self): |
| 257 |
zone_type = self.params.get("type") |
| 258 |
if self.params.get("state") == "present": |
| 259 |
if zone_type == "vlan": |
| 260 |
return self.params.get("bridge") |
| 261 |
elif zone_type == "qinq": |
| 262 |
return self.params.get("tag") and self.params.get("vlan_protocol") |
| 263 |
elif zone_type == "vxlan": |
| 264 |
return self.params.get("fabric") or self.params.get("peers") |
| 265 |
elif zone_type == "evpn": |
| 266 |
return self.params.get("controller") and self.params.get("vrf_vxlan") |
| 267 |
else: |
| 268 |
return True |
| 269 |
else: |
| 270 |
return True |
| 271 |
|
| 272 |
def run(self): |
| 273 |
state = self.params.get("state") |
| 274 |
update = self.params.get("update") |
| 275 |
zone_type = self.params.get("type") |
| 276 |
|
| 277 |
if not self.validate_params(): |
| 278 |
required_params = { |
| 279 |
"vlan": ["bridge"], |
| 280 |
"qinq": ["bridge", "tag", "vlan_protocol"], |
| 281 |
"vxlan": ["fabric or peers"], |
| 282 |
"evpn": ["controller", "vrf_vxlan"], |
| 283 |
} |
| 284 |
self.module.fail_json(msg=f"to create zone of type {zone_type} it needs - {required_params[zone_type]}") |
| 285 |
|
| 286 |
zone_params = { |
| 287 |
"type": self.params.get("type"), |
| 288 |
"zone": self.params.get("zone"), |
| 289 |
"advertise-subnets": ansible_to_proxmox_bool(self.params.get("advertise_subnets")), |
| 290 |
"bridge": self.params.get("bridge"), |
| 291 |
"bridge-disable-mac-learning": ansible_to_proxmox_bool(self.params.get("bridge_disable_mac_learning")), |
| 292 |
"controller": self.params.get("controller"), |
| 293 |
"dhcp": self.params.get("dhcp"), |
| 294 |
"disable-arp-nd-suppression": ansible_to_proxmox_bool(self.params.get("disable_arp_nd_suppression")), |
| 295 |
"dns": self.params.get("dns"), |
| 296 |
"dnszone": self.params.get("dnszone"), |
| 297 |
"dp-id": self.params.get("dp_id"), |
| 298 |
"exitnodes": self.params.get("exitnodes"), |
| 299 |
"exitnodes-local-routing": ansible_to_proxmox_bool(self.params.get("exitnodes_local_routing")), |
| 300 |
"exitnodes-primary": self.params.get("exitnodes_primary"), |
| 301 |
"fabric": self.params.get("fabric"), |
| 302 |
"ipam": self.params.get("ipam"), |
| 303 |
"lock-token": None, |
| 304 |
"mac": self.params.get("mac"), |
| 305 |
"mtu": self.params.get("mtu"), |
| 306 |
"nodes": self.params.get("nodes"), |
| 307 |
"peers": self.params.get("peers"), |
| 308 |
"reversedns": self.params.get("reversedns"), |
| 309 |
"rt-import": self.params.get("rt_import"), |
| 310 |
"tag": self.params.get("tag"), |
| 311 |
"vlan-protocol": self.params.get("vlan_protocol"), |
| 312 |
"vrf-vxlan": self.params.get("vrf_vxlan"), |
| 313 |
"vxlan-port": self.params.get("vxlan_port"), |
| 314 |
} |
| 315 |
|
| 316 |
if state == "present": |
| 317 |
self.zone_present(update, **zone_params) |
| 318 |
|
| 319 |
elif state == "absent": |
| 320 |
self.zone_absent(zone_name=zone_params.get("zone"), lock=zone_params.get("lock-token")) |
| 321 |
|
| 322 |
def zone_present(self, update, **kwargs): |
| 323 |
available_zones = {x.get("zone"): {"type": x.get("type"), "digest": x.get("digest")} for x in self.get_zones()} |
| 324 |
zone_name = kwargs.get("zone") |
| 325 |
zone_type = kwargs.get("type") |
| 326 |
|
| 327 |
# Check if zone already exists |
| 328 |
if zone_name in available_zones and not update: |
| 329 |
self.module.exit_json( |
| 330 |
changed=False, zone=zone_name, msg=f"Zone {zone_name} already exists and update is false!" |
| 331 |
) |
| 332 |
if zone_name in available_zones and update: |
| 333 |
if zone_type != available_zones[zone_name]["type"]: |
| 334 |
self.module.fail_json( |
| 335 |
msg=f"zone {zone_name} exists with different type and we cannot change type post fact." |
| 336 |
) |
| 337 |
try: |
| 338 |
kwargs["lock-token"] = self.get_global_sdn_lock() |
| 339 |
kwargs["digest"] = available_zones[zone_name]["digest"] |
| 340 |
del kwargs["zone"] |
| 341 |
del kwargs["type"] |
| 342 |
|
| 343 |
if not self.is_lock_and_rollback_supported: |
| 344 |
del kwargs["lock-token"] |
| 345 |
self.proxmox_api.cluster().sdn().zones(zone_name).put(**kwargs) |
| 346 |
self.apply_sdn_changes_and_release_lock(kwargs.get("lock-token")) |
| 347 |
self.module.exit_json(changed=True, zone=zone_name, msg=f"Updated zone - {zone_name}") |
| 348 |
except Exception as e: |
| 349 |
if self.is_lock_and_rollback_supported: |
| 350 |
self.rollback_sdn_changes_and_release_lock(kwargs["lock-token"]) |
| 351 |
self.module.fail_json(msg=f"Failed to update zone {zone_name} - {e}") |
| 352 |
# Zone does not exist and gets created |
| 353 |
else: |
| 354 |
try: |
| 355 |
kwargs["lock-token"] = self.get_global_sdn_lock() |
| 356 |
|
| 357 |
if not self.is_lock_and_rollback_supported: |
| 358 |
del kwargs["lock-token"] |
| 359 |
self.proxmox_api.cluster().sdn().zones().post(**kwargs) |
| 360 |
self.apply_sdn_changes_and_release_lock(kwargs.get("lock-token")) |
| 361 |
self.module.exit_json(changed=True, zone=zone_name, msg=f"Created new Zone - {zone_name}") |
| 362 |
except Exception as e: |
| 363 |
if self.is_lock_and_rollback_supported: |
| 364 |
self.rollback_sdn_changes_and_release_lock(kwargs["lock-token"]) |
| 365 |
self.module.fail_json(msg=f"Failed to create zone {zone_name} - {e}") |
| 366 |
|
| 367 |
def zone_absent(self, zone_name, lock=None): |
| 368 |
available_zones = [x.get("zone") for x in self.get_zones()] |
| 369 |
params = {"lock-token": lock} |
| 370 |
|
| 371 |
if zone_name not in available_zones: |
| 372 |
self.module.exit_json(changed=False, zone=zone_name, msg=f"zone {zone_name} is absent.") |
| 373 |
try: |
| 374 |
params["lock-token"] = self.get_global_sdn_lock() |
| 375 |
if not self.is_lock_and_rollback_supported: |
| 376 |
del params["lock-token"] |
| 377 |
self.proxmox_api.cluster().sdn().zones(zone_name).delete(**params) |
| 378 |
self.apply_sdn_changes_and_release_lock(params.get("lock-token")) |
| 379 |
self.module.exit_json(changed=True, zone=zone_name, msg=f"Successfully deleted zone {zone_name}") |
| 380 |
except Exception as e: |
| 381 |
if self.is_lock_and_rollback_supported: |
| 382 |
self.rollback_sdn_changes_and_release_lock(params["lock-token"]) |
| 383 |
self.module.fail_json(msg=f"Failed to delete zone {zone_name} {e}. Rolling back all pending changes.") |
| 384 |
else: |
| 385 |
self.module.fail_json(msg=f"Failed to delete zone {zone_name} {e}. Rollback not supported.") |
| 386 |
|
| 387 |
|
| 388 |
def main(): |
| 389 |
module = create_proxmox_module(module_args(), **module_options()) |
| 390 |
proxmox = ProxmoxZoneAnsible(module) |
| 391 |
|
| 392 |
try: |
| 393 |
proxmox.run() |
| 394 |
except Exception as e: |
| 395 |
module.fail_json(msg=f"An error occurred: {e}") |
| 396 |
|
| 397 |
|
| 398 |
if __name__ == "__main__": |
| 399 |
main() |