| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2025, aleskxyz <[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_node_network |
| 10 |
version_added: "1.4.0" |
| 11 |
short_description: Manage network interfaces on Proxmox nodes |
| 12 |
requirements: |
| 13 |
- ipaddress |
| 14 |
description: |
| 15 |
- This module allows you to manage network interfaces on Proxmox nodes. |
| 16 |
- Supports various interface types including bridge, bond, eth, vlan, and OVS interfaces. |
| 17 |
- Network configuration changes are staged and must be explicitly applied using C(state=apply) to take effect. |
| 18 |
- Changes made with C(state=present) or C(state=absent) are only staged and will not affect the running network configuration until applied. |
| 19 |
author: "aleskxyz (@aleskxyz)" |
| 20 |
notes: |
| 21 |
- Network configuration changes are staged and must be explicitly applied using C(state=apply) to take effect. |
| 22 |
- Changes made with C(state=present) or C(state=absent) are only staged and will not affect the running network configuration until applied. |
| 23 |
- Interface type cannot be changed after creation. Delete and recreate the interface to change its type. |
| 24 |
- "Ethernet interfaces (C(eth) type) represent physical hardware and cannot be created via API. Only existing physical interfaces can be configured." |
| 25 |
- "Bond and OVS bond interfaces must follow naming format C(bondX) where X is a number between 0 and 9999 (e.g., C(bond0), C(bond1), C(bond9999))." |
| 26 |
- "VLAN interfaces support two naming formats: C(vlanXY) (e.g., C(vlan100)) and C(iface_name.vlan_id) (e.g., C(eth0.100))." |
| 27 |
- For C(vlanXY) format, C(vlan_raw_device) parameter is required. |
| 28 |
- For C(iface_name.vlan_id) format, C(vlan_raw_device) should not be specified. |
| 29 |
- "Parameter deletion is supported for specific parameters. Set string parameters to empty string C('') or integer parameters to C(-1) to delete them." |
| 30 |
- "Deletable parameters: C(cidr), C(gateway), C(cidr6), C(gateway6), C(comments), C(mtu), |
| 31 |
C(bridge_ports), C(bridge_vids), C(ovs_ports), C(ovs_options), C(ovs_tag)." |
| 32 |
- "When C(state=apply) or C(state=revert), only the C(node) parameter is accepted. All other parameters are not allowed." |
| 33 |
seealso: |
| 34 |
- module: community.proxmox.proxmox_node_network_info |
| 35 |
description: Retrieve information about network interfaces on Proxmox nodes. |
| 36 |
- name: Proxmox Network Configuration |
| 37 |
description: Proxmox VE network configuration documentation. |
| 38 |
link: https://pve.proxmox.com/wiki/Network_Configuration |
| 39 |
attributes: |
| 40 |
check_mode: |
| 41 |
support: full |
| 42 |
details: Check mode is fully supported for all states. |
| 43 |
diff_mode: |
| 44 |
support: full |
| 45 |
details: Check mode is fully supported for all states. |
| 46 |
options: |
| 47 |
node: |
| 48 |
description: |
| 49 |
- The Proxmox node to manage network interfaces on. |
| 50 |
type: str |
| 51 |
required: true |
| 52 |
state: |
| 53 |
description: |
| 54 |
- The desired state of the network interface. |
| 55 |
- C(present) and C(absent) stage changes but do not apply them to the running configuration. |
| 56 |
- C(apply) applies all staged network configuration changes to the running configuration. |
| 57 |
- C(revert) discards all staged network configuration changes. |
| 58 |
type: str |
| 59 |
choices: |
| 60 |
- present |
| 61 |
- absent |
| 62 |
- apply |
| 63 |
- revert |
| 64 |
default: present |
| 65 |
iface: |
| 66 |
description: |
| 67 |
- Network interface name. |
| 68 |
- Required when C(state=present) or C(state=absent). |
| 69 |
- For C(vlan) interface type, should be in format C(vlanXY) (e.g., C(vlan100)) or C(iface_name.vlan_id) (e.g., C(eth0.100)). |
| 70 |
type: str |
| 71 |
required: false |
| 72 |
iface_type: |
| 73 |
description: |
| 74 |
- Type of network interface. |
| 75 |
- Required when C(state=present). |
| 76 |
- Cannot be changed after interface creation. |
| 77 |
type: str |
| 78 |
choices: |
| 79 |
- bridge |
| 80 |
- bond |
| 81 |
- eth |
| 82 |
- vlan |
| 83 |
- OVSBridge |
| 84 |
- OVSBond |
| 85 |
- OVSIntPort |
| 86 |
required: false |
| 87 |
cidr: |
| 88 |
description: |
| 89 |
- IPv4 host address with prefix length (e.g., '192.168.1.10/24'). |
| 90 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), and C(OVSIntPort) interface types. |
| 91 |
- Can be deleted by setting to empty string C(''). |
| 92 |
type: str |
| 93 |
required: false |
| 94 |
gateway: |
| 95 |
description: |
| 96 |
- Default IPv4 gateway address. |
| 97 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), and C(OVSIntPort) interface types. |
| 98 |
- Can be deleted by setting to empty string C(''). |
| 99 |
type: str |
| 100 |
required: false |
| 101 |
cidr6: |
| 102 |
description: |
| 103 |
- IPv6 host address with prefix length (e.g., '2001:db8::10/64'). |
| 104 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), and C(OVSIntPort) interface types. |
| 105 |
- Can be deleted by setting to empty string C(''). |
| 106 |
type: str |
| 107 |
required: false |
| 108 |
gateway6: |
| 109 |
description: |
| 110 |
- Default IPv6 gateway address. |
| 111 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), and C(OVSIntPort) interface types. |
| 112 |
- Can be deleted by setting to empty string C(''). |
| 113 |
type: str |
| 114 |
required: false |
| 115 |
autostart: |
| 116 |
description: |
| 117 |
- Automatically start interface on boot. |
| 118 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), and C(OVSBridge) interface types. |
| 119 |
type: bool |
| 120 |
required: false |
| 121 |
comments: |
| 122 |
description: |
| 123 |
- Comments for the interface configuration. |
| 124 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), C(OVSBond), and C(OVSIntPort) interface types. |
| 125 |
- Can be deleted by setting to empty string C(''). |
| 126 |
type: str |
| 127 |
required: false |
| 128 |
mtu: |
| 129 |
description: |
| 130 |
- Maximum Transmission Unit (1280 - 65520). |
| 131 |
- Supported for C(eth), C(bridge), C(bond), C(vlan), C(OVSBridge), C(OVSBond), and C(OVSIntPort) interface types. |
| 132 |
- Can be deleted by setting to C(-1). |
| 133 |
type: int |
| 134 |
required: false |
| 135 |
# Bridge specific options |
| 136 |
bridge_ports: |
| 137 |
description: |
| 138 |
- Specify the interfaces you want to add to your bridge. |
| 139 |
- Supported for C(bridge) interface type only. |
| 140 |
- Can be deleted by setting to empty string C(''). |
| 141 |
type: str |
| 142 |
required: false |
| 143 |
bridge_vids: |
| 144 |
description: |
| 145 |
- Specify the allowed VLANs (e.g., '2 4 100-200'). |
| 146 |
- Only used if C(bridge_vlan_aware) is enabled. |
| 147 |
- Supported for C(bridge) interface type only. |
| 148 |
- Can be deleted by setting to empty string C(''). |
| 149 |
type: str |
| 150 |
required: false |
| 151 |
bridge_vlan_aware: |
| 152 |
description: |
| 153 |
- Enable bridge VLAN support. |
| 154 |
- Supported for C(bridge) interface type only. |
| 155 |
type: bool |
| 156 |
required: false |
| 157 |
# Bond specific options |
| 158 |
bond_primary: |
| 159 |
description: |
| 160 |
- Primary interface for active-backup bond. |
| 161 |
- Required for C(active-backup) bonding mode. |
| 162 |
- Supported for C(bond) interface type only. |
| 163 |
- Primary interface should be specified in C(slaves) parameter as well. |
| 164 |
type: str |
| 165 |
required: false |
| 166 |
bond_mode: |
| 167 |
description: |
| 168 |
- Bonding mode for C(bond) or C(OVSBond) interface types. |
| 169 |
- "Valid values for C(bond) interface type: C(balance-rr), C(active-backup), C(balance-xor), C(broadcast), C(802.3ad), C(balance-tlb), C(balance-alb)" |
| 170 |
- "Valid values for C(OVSBond) interface type: C(active-backup), C(balance-slb), C(lacp-balance-slb), C(lacp-balance-tcp)" |
| 171 |
type: str |
| 172 |
choices: |
| 173 |
- balance-rr |
| 174 |
- active-backup |
| 175 |
- balance-xor |
| 176 |
- broadcast |
| 177 |
- 802.3ad |
| 178 |
- balance-tlb |
| 179 |
- balance-alb |
| 180 |
- balance-slb |
| 181 |
- lacp-balance-slb |
| 182 |
- lacp-balance-tcp |
| 183 |
required: false |
| 184 |
bond_xmit_hash_policy: |
| 185 |
description: |
| 186 |
- Transmit hash policy for bond type. |
| 187 |
- Required for C(balance-xor) and C(802.3ad) bonding modes. |
| 188 |
type: str |
| 189 |
choices: |
| 190 |
- layer2 |
| 191 |
- layer2+3 |
| 192 |
- layer3+4 |
| 193 |
required: false |
| 194 |
slaves: |
| 195 |
description: |
| 196 |
- Interfaces used by the bonding device (space separated). |
| 197 |
- Required for C(bond) interface type. |
| 198 |
type: str |
| 199 |
required: false |
| 200 |
# VLAN specific options |
| 201 |
vlan_raw_device: |
| 202 |
description: |
| 203 |
- Raw device for VLAN interface. |
| 204 |
- Required if iface is in format 'vlanXY'. |
| 205 |
- Supported for C(vlan) interface type only. |
| 206 |
type: str |
| 207 |
required: false |
| 208 |
# OVS specific options |
| 209 |
ovs_ports: |
| 210 |
description: |
| 211 |
- Interfaces to add to OVS bridge. |
| 212 |
- Supported for C(OVSBridge) interface type only. |
| 213 |
- Can be deleted by setting to empty string C(''). |
| 214 |
type: str |
| 215 |
required: false |
| 216 |
ovs_options: |
| 217 |
description: |
| 218 |
- OVS interface options. |
| 219 |
- Supported for C(OVSBridge), C(OVSBond), and C(OVSIntPort) interface types. |
| 220 |
- Can be deleted by setting to empty string C(''). |
| 221 |
type: str |
| 222 |
required: false |
| 223 |
ovs_bonds: |
| 224 |
description: |
| 225 |
- Interfaces used by OVS bonding device. (space separated) |
| 226 |
- Required for C(OVSBond) interface type. |
| 227 |
type: str |
| 228 |
required: false |
| 229 |
ovs_bridge: |
| 230 |
description: |
| 231 |
- OVS bridge name. |
| 232 |
- Required for C(OVSBond) and C(OVSIntPort) interface types. |
| 233 |
type: str |
| 234 |
required: false |
| 235 |
ovs_tag: |
| 236 |
description: |
| 237 |
- VLAN tag (1 - 4094). |
| 238 |
- Supported for C(OVSBond) and C(OVSIntPort) interface types. |
| 239 |
- Can be deleted by setting to C(-1). |
| 240 |
type: int |
| 241 |
required: false |
| 242 |
extends_documentation_fragment: |
| 243 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 244 |
- community.proxmox.proxmox.documentation |
| 245 |
- community.proxmox.attributes |
| 246 |
""" |
| 247 |
|
| 248 |
EXAMPLES = r""" |
| 249 |
# Configure a network interface |
| 250 |
- name: Configure network interface |
| 251 |
community.proxmox.proxmox_node_network: |
| 252 |
node: pve01 |
| 253 |
state: present |
| 254 |
iface: eth0 |
| 255 |
iface_type: eth |
| 256 |
cidr: 192.168.1.10/24 |
| 257 |
gateway: 192.168.1.1 |
| 258 |
cidr6: 2001:db8::10/64 |
| 259 |
gateway6: 2001:db8::1 |
| 260 |
autostart: true |
| 261 |
mtu: 1500 |
| 262 |
comments: "Management network" |
| 263 |
|
| 264 |
# Create a simple bridge interface |
| 265 |
- name: Create bridge interface |
| 266 |
community.proxmox.proxmox_node_network: |
| 267 |
node: pve01 |
| 268 |
state: present |
| 269 |
iface: vmbr0 |
| 270 |
iface_type: bridge |
| 271 |
cidr: 192.168.1.10/24 |
| 272 |
gateway: 192.168.1.1 |
| 273 |
autostart: true |
| 274 |
|
| 275 |
# Create a bond interface |
| 276 |
- name: Create bond interface |
| 277 |
community.proxmox.proxmox_node_network: |
| 278 |
node: pve01 |
| 279 |
state: present |
| 280 |
iface: bond0 |
| 281 |
iface_type: bond |
| 282 |
bond_mode: active-backup |
| 283 |
bond_primary: eth0 |
| 284 |
slaves: eth0 eth1 |
| 285 |
cidr: 192.168.1.0/24 |
| 286 |
gateway: 192.168.1.1 |
| 287 |
|
| 288 |
# Create a VLAN interface |
| 289 |
- name: Create VLAN interface |
| 290 |
community.proxmox.proxmox_node_network: |
| 291 |
node: pve01 |
| 292 |
state: present |
| 293 |
iface: eth0.100 |
| 294 |
iface_type: vlan |
| 295 |
cidr: 192.168.100.10/24 |
| 296 |
|
| 297 |
# Create a VLAN interface with vlanXY format |
| 298 |
- name: Create VLAN interface |
| 299 |
community.proxmox.proxmox_node_network: |
| 300 |
node: pve01 |
| 301 |
state: present |
| 302 |
iface: vlan100 |
| 303 |
iface_type: vlan |
| 304 |
vlan_raw_device: eth0 |
| 305 |
cidr: 192.168.100.0/24 |
| 306 |
|
| 307 |
# Create a complex bridge with VLAN awareness |
| 308 |
- name: Create VLAN-aware bridge |
| 309 |
community.proxmox.proxmox_node_network: |
| 310 |
node: pve01 |
| 311 |
state: present |
| 312 |
iface: vmbr1 |
| 313 |
iface_type: bridge |
| 314 |
bridge_ports: eth1 eth2 |
| 315 |
bridge_vlan_aware: true |
| 316 |
bridge_vids: "2 4 100-200" |
| 317 |
cidr: 192.168.2.0/24 |
| 318 |
gateway: 192.168.2.1 |
| 319 |
mtu: 9000 |
| 320 |
comments: "VLAN-aware bridge for trunking" |
| 321 |
|
| 322 |
# Create an OVS bridge |
| 323 |
- name: Create OVS bridge |
| 324 |
community.proxmox.proxmox_node_network: |
| 325 |
node: pve01 |
| 326 |
state: present |
| 327 |
iface: ovsbr0 |
| 328 |
iface_type: OVSBridge |
| 329 |
ovs_ports: eth3 eth4 |
| 330 |
ovs_options: "updelay=5000" |
| 331 |
cidr: 192.168.3.10/24 |
| 332 |
gateway: 192.168.3.1 |
| 333 |
|
| 334 |
# Create an OVS bond |
| 335 |
- name: Create OVS bond |
| 336 |
community.proxmox.proxmox_node_network: |
| 337 |
node: pve01 |
| 338 |
state: present |
| 339 |
iface: bond1 # Must follow bondX format where X is 0-9999 |
| 340 |
iface_type: OVSBond |
| 341 |
bond_mode: active-backup |
| 342 |
ovs_bonds: eth5 eth6 |
| 343 |
ovs_bridge: ovsbr0 |
| 344 |
ovs_tag: 10 |
| 345 |
ovs_options: "updelay=5000" |
| 346 |
|
| 347 |
# Create an OVS internal port |
| 348 |
- name: Create OVS internal port |
| 349 |
community.proxmox.proxmox_node_network: |
| 350 |
node: pve01 |
| 351 |
state: present |
| 352 |
iface: ovsint0 |
| 353 |
iface_type: OVSIntPort |
| 354 |
ovs_bridge: ovsbr0 |
| 355 |
ovs_tag: 20 |
| 356 |
ovs_options: "tag=20" |
| 357 |
cidr: 192.168.20.10/24 |
| 358 |
gateway: 192.168.20.1 |
| 359 |
|
| 360 |
# Create interface with IPv6 |
| 361 |
- name: Create dual-stack interface |
| 362 |
community.proxmox.proxmox_node_network: |
| 363 |
node: pve01 |
| 364 |
state: present |
| 365 |
iface: vmbr2 |
| 366 |
iface_type: bridge |
| 367 |
bridge_ports: eth7 |
| 368 |
cidr: 192.168.4.0/24 |
| 369 |
gateway: 192.168.4.1 |
| 370 |
cidr6: 2001:db8::/64 |
| 371 |
gateway6: 2001:db8::1 |
| 372 |
autostart: true |
| 373 |
|
| 374 |
# Remove an interface |
| 375 |
- name: Remove interface |
| 376 |
community.proxmox.proxmox_node_network: |
| 377 |
node: pve01 |
| 378 |
state: absent |
| 379 |
iface: vmbr0 |
| 380 |
|
| 381 |
# Apply network configuration |
| 382 |
- name: Apply network |
| 383 |
community.proxmox.proxmox_node_network: |
| 384 |
node: pve01 |
| 385 |
state: apply |
| 386 |
|
| 387 |
# Complete workflow example |
| 388 |
- name: Create interface and apply changes |
| 389 |
community.proxmox.proxmox_node_network: |
| 390 |
node: pve01 |
| 391 |
state: present |
| 392 |
iface: vmbr1 |
| 393 |
iface_type: bridge |
| 394 |
cidr: 192.168.2.10/24 |
| 395 |
gateway: 192.168.2.1 |
| 396 |
|
| 397 |
- name: Apply staged network changes |
| 398 |
community.proxmox.proxmox_node_network: |
| 399 |
node: pve01 |
| 400 |
state: apply |
| 401 |
|
| 402 |
# Revert network configuration changes |
| 403 |
- name: Revert network changes |
| 404 |
community.proxmox.proxmox_node_network: |
| 405 |
node: pve01 |
| 406 |
state: revert |
| 407 |
|
| 408 |
# Using API token authentication |
| 409 |
- name: Create interface with API token |
| 410 |
community.proxmox.proxmox_node_network: |
| 411 |
node: pve01 |
| 412 |
state: present |
| 413 |
iface: vmbr3 |
| 414 |
iface_type: bridge |
| 415 |
cidr: 192.168.5.10/24 |
| 416 |
gateway: 192.168.5.1 |
| 417 |
|
| 418 |
# Delete specific parameters from an interface |
| 419 |
- name: Remove IP configuration from bridge |
| 420 |
community.proxmox.proxmox_node_network: |
| 421 |
node: pve01 |
| 422 |
state: present |
| 423 |
iface: vmbr0 |
| 424 |
iface_type: bridge |
| 425 |
cidr: "" # Delete IPv4 CIDR |
| 426 |
gateway: "" # Delete IPv4 gateway |
| 427 |
cidr6: "" # Delete IPv6 CIDR |
| 428 |
gateway6: "" # Delete IPv6 gateway |
| 429 |
comments: "" # Delete comments |
| 430 |
mtu: -1 # Delete MTU (use -1 for integer parameters) |
| 431 |
|
| 432 |
# Remove bridge ports and VLAN configuration |
| 433 |
- name: Remove bridge ports and VLAN settings |
| 434 |
community.proxmox.proxmox_node_network: |
| 435 |
node: pve01 |
| 436 |
state: present |
| 437 |
iface: vmbr1 |
| 438 |
iface_type: bridge |
| 439 |
bridge_ports: "" # Remove all bridge ports |
| 440 |
bridge_vlan_aware: true |
| 441 |
bridge_vids: "" # Remove VLAN IDs (requires bridge_vlan_aware: true) |
| 442 |
|
| 443 |
# Remove OVS-specific parameters |
| 444 |
- name: Remove OVS options and ports |
| 445 |
community.proxmox.proxmox_node_network: |
| 446 |
node: pve01 |
| 447 |
state: present |
| 448 |
iface: ovsbr0 |
| 449 |
iface_type: OVSBridge |
| 450 |
ovs_ports: "" # Remove OVS ports |
| 451 |
ovs_options: "" # Remove OVS options |
| 452 |
ovs_tag: -1 # Remove VLAN tag (use -1 for integer parameters) |
| 453 |
|
| 454 |
# Configure existing physical Ethernet interface |
| 455 |
- name: Configure physical Ethernet interface |
| 456 |
community.proxmox.proxmox_node_network: |
| 457 |
node: pve01 |
| 458 |
state: present |
| 459 |
iface: eth0 |
| 460 |
iface_type: eth |
| 461 |
cidr: 192.168.1.10/24 |
| 462 |
gateway: 192.168.1.1 |
| 463 |
mtu: 9000 |
| 464 |
comments: "Management interface" |
| 465 |
# Note: eth interfaces represent physical hardware and cannot be created via API |
| 466 |
# Only existing physical interfaces can be configured |
| 467 |
|
| 468 |
# Create bond interface with proper naming format |
| 469 |
- name: Create bond interface |
| 470 |
community.proxmox.proxmox_node_network: |
| 471 |
node: pve01 |
| 472 |
state: present |
| 473 |
iface: bond0 # Must follow bondX format where X is 0-9999 |
| 474 |
iface_type: bond |
| 475 |
bond_mode: active-backup |
| 476 |
bond_primary: eth0 |
| 477 |
slaves: eth0 eth1 |
| 478 |
cidr: 192.168.10.0/24 |
| 479 |
gateway: 192.168.10.1 |
| 480 |
|
| 481 |
|
| 482 |
""" |
| 483 |
|
| 484 |
RETURN = r""" |
| 485 |
changed: |
| 486 |
description: Whether the module made changes. |
| 487 |
returned: always |
| 488 |
type: bool |
| 489 |
sample: true |
| 490 |
msg: |
| 491 |
description: A message describing what happened. |
| 492 |
returned: always |
| 493 |
type: str |
| 494 |
sample: "Interface vmbr0 created successfully" |
| 495 |
interface: |
| 496 |
description: The interface configuration that was applied. |
| 497 |
returned: when state is present |
| 498 |
type: dict |
| 499 |
sample: |
| 500 |
iface: vmbr0 |
| 501 |
iface_type: bridge |
| 502 |
cidr: 192.168.1.0/24 |
| 503 |
gateway: 192.168.1.1 |
| 504 |
autostart: true |
| 505 |
mtu: 1500 |
| 506 |
comments: "Management network" |
| 507 |
diff: |
| 508 |
description: Differences between configurations in YAML format with before/after states. |
| 509 |
returned: when changes were made (create, update, delete) |
| 510 |
type: list |
| 511 |
""" |
| 512 |
|
| 513 |
|
| 514 |
import re |
| 515 |
import traceback |
| 516 |
from ipaddress import ip_address, ip_interface |
| 517 |
|
| 518 |
from ansible.module_utils.common.text.converters import to_native |
| 519 |
from ansible.module_utils.common.yaml import yaml_dump |
| 520 |
|
| 521 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 522 |
ProxmoxAnsible, |
| 523 |
ansible_to_proxmox_bool, |
| 524 |
create_proxmox_module, |
| 525 |
proxmox_auth_argument_spec, |
| 526 |
proxmox_to_ansible_bool, |
| 527 |
) |
| 528 |
|
| 529 |
MIN_VLAN_ID = 1 |
| 530 |
MAX_VLAN_ID = 4094 |
| 531 |
MAX_BOND_NUMBER = 9999 |
| 532 |
MIN_MTU = 1280 |
| 533 |
MAX_MTU = 65520 |
| 534 |
|
| 535 |
# Single source of truth for all parameter definitions |
| 536 |
PARAMETER_DEFINITIONS = { |
| 537 |
"node": { |
| 538 |
"type": "str", |
| 539 |
"required": True, |
| 540 |
}, |
| 541 |
"state": { |
| 542 |
"type": "str", |
| 543 |
"choices": ["present", "absent", "apply", "revert"], |
| 544 |
"default": "present", |
| 545 |
}, |
| 546 |
"iface": { |
| 547 |
"type": "str", |
| 548 |
}, |
| 549 |
"iface_type": { |
| 550 |
"type": "str", |
| 551 |
"choices": [ |
| 552 |
"bridge", |
| 553 |
"bond", |
| 554 |
"eth", |
| 555 |
"vlan", |
| 556 |
"OVSBridge", |
| 557 |
"OVSBond", |
| 558 |
"OVSIntPort", |
| 559 |
], |
| 560 |
"api_name": "type", |
| 561 |
}, |
| 562 |
"cidr": { |
| 563 |
"type": "str", |
| 564 |
"deletable": True, |
| 565 |
"iface_types": ["eth", "bridge", "bond", "vlan", "OVSBridge", "OVSIntPort"], |
| 566 |
}, |
| 567 |
"gateway": { |
| 568 |
"type": "str", |
| 569 |
"deletable": True, |
| 570 |
"iface_types": ["eth", "bridge", "bond", "vlan", "OVSBridge", "OVSIntPort"], |
| 571 |
}, |
| 572 |
"cidr6": { |
| 573 |
"type": "str", |
| 574 |
"deletable": True, |
| 575 |
"iface_types": ["eth", "bridge", "bond", "vlan", "OVSBridge", "OVSIntPort"], |
| 576 |
}, |
| 577 |
"gateway6": { |
| 578 |
"type": "str", |
| 579 |
"deletable": True, |
| 580 |
"iface_types": ["eth", "bridge", "bond", "vlan", "OVSBridge", "OVSIntPort"], |
| 581 |
}, |
| 582 |
"autostart": { |
| 583 |
"type": "bool", |
| 584 |
"iface_types": ["eth", "bridge", "bond", "vlan", "OVSBridge"], |
| 585 |
}, |
| 586 |
"comments": { |
| 587 |
"type": "str", |
| 588 |
"deletable": True, |
| 589 |
"iface_types": [ |
| 590 |
"eth", |
| 591 |
"bridge", |
| 592 |
"bond", |
| 593 |
"vlan", |
| 594 |
"OVSBridge", |
| 595 |
"OVSBond", |
| 596 |
"OVSIntPort", |
| 597 |
], |
| 598 |
}, |
| 599 |
"mtu": { |
| 600 |
"type": "int", |
| 601 |
"deletable": True, |
| 602 |
"iface_types": [ |
| 603 |
"eth", |
| 604 |
"bridge", |
| 605 |
"bond", |
| 606 |
"vlan", |
| 607 |
"OVSBridge", |
| 608 |
"OVSBond", |
| 609 |
"OVSIntPort", |
| 610 |
], |
| 611 |
}, |
| 612 |
"bridge_ports": { |
| 613 |
"type": "str", |
| 614 |
"deletable": True, |
| 615 |
"default_response": "", |
| 616 |
"iface_types": ["bridge"], |
| 617 |
}, |
| 618 |
"bridge_vids": { |
| 619 |
"type": "str", |
| 620 |
"deletable": True, |
| 621 |
"default_response": "2-4094", |
| 622 |
"iface_types": ["bridge"], |
| 623 |
}, |
| 624 |
"bridge_vlan_aware": { |
| 625 |
"type": "bool", |
| 626 |
"iface_types": ["bridge"], |
| 627 |
}, |
| 628 |
"bond_primary": { |
| 629 |
"type": "str", |
| 630 |
"api_name": "bond-primary", |
| 631 |
"iface_types": ["bond"], |
| 632 |
}, |
| 633 |
"bond_mode": { |
| 634 |
"type": "str", |
| 635 |
"choices": [ |
| 636 |
"balance-rr", |
| 637 |
"active-backup", |
| 638 |
"balance-xor", |
| 639 |
"broadcast", |
| 640 |
"802.3ad", |
| 641 |
"balance-tlb", |
| 642 |
"balance-alb", |
| 643 |
"balance-slb", |
| 644 |
"lacp-balance-slb", |
| 645 |
"lacp-balance-tcp", |
| 646 |
], |
| 647 |
"iface_types": ["bond", "OVSBond"], |
| 648 |
}, |
| 649 |
"bond_xmit_hash_policy": { |
| 650 |
"type": "str", |
| 651 |
"choices": ["layer2", "layer2+3", "layer3+4"], |
| 652 |
"iface_types": ["bond"], |
| 653 |
}, |
| 654 |
"slaves": { |
| 655 |
"type": "str", |
| 656 |
"iface_types": ["bond"], |
| 657 |
}, |
| 658 |
"vlan_raw_device": { |
| 659 |
"type": "str", |
| 660 |
"api_name": "vlan-raw-device", |
| 661 |
"iface_types": ["vlan"], |
| 662 |
}, |
| 663 |
"ovs_ports": { |
| 664 |
"type": "str", |
| 665 |
"deletable": True, |
| 666 |
"default_response": "", |
| 667 |
"iface_types": ["OVSBridge"], |
| 668 |
}, |
| 669 |
"ovs_options": { |
| 670 |
"type": "str", |
| 671 |
"deletable": True, |
| 672 |
"default_response": "", |
| 673 |
"iface_types": ["OVSBridge", "OVSBond", "OVSIntPort"], |
| 674 |
}, |
| 675 |
"ovs_bonds": { |
| 676 |
"type": "str", |
| 677 |
"iface_types": ["OVSBond"], |
| 678 |
}, |
| 679 |
"ovs_bridge": { |
| 680 |
"type": "str", |
| 681 |
"iface_types": ["OVSBond", "OVSIntPort"], |
| 682 |
}, |
| 683 |
"ovs_tag": { |
| 684 |
"type": "int", |
| 685 |
"deletable": True, |
| 686 |
"iface_types": ["OVSBond", "OVSIntPort"], |
| 687 |
}, |
| 688 |
# Read-only parameters returned by API |
| 689 |
"active": { |
| 690 |
"type": "bool", |
| 691 |
"read_only": True, |
| 692 |
}, |
| 693 |
"vlan_id": { |
| 694 |
"type": "int", |
| 695 |
"read_only": True, |
| 696 |
"api_name": "vlan-id", |
| 697 |
}, |
| 698 |
"exists": { |
| 699 |
"type": "bool", |
| 700 |
"read_only": True, |
| 701 |
}, |
| 702 |
"priority": { |
| 703 |
"type": "int", |
| 704 |
"read_only": True, |
| 705 |
}, |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
def _is_valid_cidr(cidr): |
| 710 |
"""Validate IPv4 host address with prefix using ipaddress.""" |
| 711 |
if not cidr: |
| 712 |
return False |
| 713 |
try: |
| 714 |
iface = ip_interface(cidr) |
| 715 |
return iface.version == 4 # noqa: PLR2004 |
| 716 |
except Exception: |
| 717 |
return False |
| 718 |
|
| 719 |
|
| 720 |
def _is_valid_cidr6(cidr): |
| 721 |
"""Validate IPv6 host address with prefix using ipaddress.""" |
| 722 |
if not cidr: |
| 723 |
return False |
| 724 |
try: |
| 725 |
iface = ip_interface(cidr) |
| 726 |
return iface.version == 6 # noqa: PLR2004 |
| 727 |
except Exception: |
| 728 |
return False |
| 729 |
|
| 730 |
|
| 731 |
def _is_valid_ipv4(addr): |
| 732 |
"""Validate IPv4 address using ipaddress.""" |
| 733 |
if not addr: |
| 734 |
return False |
| 735 |
try: |
| 736 |
return ip_address(addr).version == 4 # noqa: PLR2004 |
| 737 |
except Exception: |
| 738 |
return False |
| 739 |
|
| 740 |
|
| 741 |
def _is_valid_ipv6(addr): |
| 742 |
"""Validate IPv6 address using ipaddress.""" |
| 743 |
if not addr: |
| 744 |
return False |
| 745 |
try: |
| 746 |
return ip_address(addr).version == 6 # noqa: PLR2004 |
| 747 |
except Exception: |
| 748 |
return False |
| 749 |
|
| 750 |
|
| 751 |
def module_args(): |
| 752 |
"""Get network-specific arguments for AnsibleModule.""" |
| 753 |
args = {} |
| 754 |
for param_name, param_def in PARAMETER_DEFINITIONS.items(): |
| 755 |
if param_def.get("read_only"): |
| 756 |
continue |
| 757 |
|
| 758 |
arg_def = {"type": param_def["type"]} |
| 759 |
|
| 760 |
if "choices" in param_def: |
| 761 |
arg_def["choices"] = param_def["choices"] |
| 762 |
if "default" in param_def: |
| 763 |
arg_def["default"] = param_def["default"] |
| 764 |
if "required" in param_def: |
| 765 |
arg_def["required"] = param_def["required"] |
| 766 |
|
| 767 |
args[param_name] = arg_def |
| 768 |
|
| 769 |
return args |
| 770 |
|
| 771 |
|
| 772 |
def module_options(): |
| 773 |
return dict(required_if=[("state", "present", ["iface", "iface_type"])]) |
| 774 |
|
| 775 |
|
| 776 |
class ProxmoxNetworkManager(ProxmoxAnsible): |
| 777 |
"""Manages Proxmox network interfaces.""" |
| 778 |
|
| 779 |
def __init__(self, module): |
| 780 |
super().__init__(module) |
| 781 |
self.params = module.params |
| 782 |
self.node = self.params["node"] |
| 783 |
|
| 784 |
def get_params_for_interface_type(self, iface_type): |
| 785 |
"""Get parameters applicable to a specific interface type.""" |
| 786 |
params = [] |
| 787 |
for param_name, param_def in PARAMETER_DEFINITIONS.items(): |
| 788 |
if param_def.get("read_only"): |
| 789 |
continue |
| 790 |
|
| 791 |
if "iface_types" in param_def and iface_type in param_def["iface_types"]: |
| 792 |
params.append(param_name) |
| 793 |
|
| 794 |
return params |
| 795 |
|
| 796 |
def get_all_valid_params(self): |
| 797 |
"""Get all valid parameter names.""" |
| 798 |
return list(PARAMETER_DEFINITIONS.keys()) |
| 799 |
|
| 800 |
def get_core_params(self): |
| 801 |
"""Get core parameters (not read-only and not interface-specific).""" |
| 802 |
return { |
| 803 |
param_name |
| 804 |
for param_name, param_def in PARAMETER_DEFINITIONS.items() |
| 805 |
if not param_def.get("read_only") and "iface_types" not in param_def |
| 806 |
} |
| 807 |
|
| 808 |
def normalize_comment(self, comment): |
| 809 |
"""Normalize comment string.""" |
| 810 |
if comment is None: |
| 811 |
return None |
| 812 |
return str(comment).strip() |
| 813 |
|
| 814 |
def get_api_name(self, param_name): |
| 815 |
"""Get API parameter name for a given Ansible parameter name.""" |
| 816 |
if param_name in PARAMETER_DEFINITIONS: |
| 817 |
return PARAMETER_DEFINITIONS[param_name].get("api_name", param_name) |
| 818 |
return param_name |
| 819 |
|
| 820 |
def get_ansible_name(self, api_param_name): |
| 821 |
"""Get Ansible parameter name for a given API parameter name.""" |
| 822 |
for param_name, param_def in PARAMETER_DEFINITIONS.items(): |
| 823 |
if param_def.get("api_name") == api_param_name: |
| 824 |
return param_name |
| 825 |
return api_param_name |
| 826 |
|
| 827 |
def is_delete_intention(self, value, param_name): |
| 828 |
"""Check if a value represents delete intention for a parameter.""" |
| 829 |
if param_name not in PARAMETER_DEFINITIONS: |
| 830 |
return False |
| 831 |
|
| 832 |
param_def = PARAMETER_DEFINITIONS[param_name] |
| 833 |
if not param_def.get("deletable"): |
| 834 |
return False |
| 835 |
|
| 836 |
param_type = param_def["type"] |
| 837 |
|
| 838 |
if param_type == "str": |
| 839 |
return value == "" |
| 840 |
elif param_type == "int": |
| 841 |
return value == -1 |
| 842 |
else: |
| 843 |
return False |
| 844 |
|
| 845 |
def get_effective_value(self, value, param_name): |
| 846 |
"""Get the effective value for validation (treat delete intentions as null).""" |
| 847 |
if param_name not in PARAMETER_DEFINITIONS: |
| 848 |
return value |
| 849 |
|
| 850 |
param_def = PARAMETER_DEFINITIONS[param_name] |
| 851 |
if not param_def.get("deletable"): |
| 852 |
return value |
| 853 |
|
| 854 |
param_type = param_def["type"] |
| 855 |
|
| 856 |
if param_type == "str" and value == "" or param_type == "int" and value == -1: |
| 857 |
return None |
| 858 |
else: |
| 859 |
return value |
| 860 |
|
| 861 |
def is_effectively_deleted(self, current_value, param_name): |
| 862 |
"""Check if a parameter is effectively deleted based on current value.""" |
| 863 |
if param_name not in PARAMETER_DEFINITIONS: |
| 864 |
return False |
| 865 |
|
| 866 |
param_def = PARAMETER_DEFINITIONS[param_name] |
| 867 |
if not param_def.get("deletable"): |
| 868 |
return False |
| 869 |
|
| 870 |
default_response = param_def.get("default_response") |
| 871 |
|
| 872 |
if default_response is None: |
| 873 |
return current_value is None |
| 874 |
else: |
| 875 |
# If current_value is None (not present), it's effectively deleted |
| 876 |
# because the API will return the default_response when we try to delete it |
| 877 |
if current_value is None: |
| 878 |
return True |
| 879 |
return current_value == default_response |
| 880 |
|
| 881 |
def validate_common_params(self): |
| 882 |
"""Validate common parameters.""" |
| 883 |
errors = [] |
| 884 |
|
| 885 |
mtu_value = self.get_effective_value(self.params.get("mtu"), "mtu") |
| 886 |
if mtu_value is not None: |
| 887 |
try: |
| 888 |
mtu = int(mtu_value) |
| 889 |
if not (MIN_MTU <= mtu <= MAX_MTU): |
| 890 |
errors.append(f"mtu must be between {MIN_MTU} and {MAX_MTU}") |
| 891 |
except (ValueError, TypeError): |
| 892 |
errors.append("mtu must be an integer") |
| 893 |
|
| 894 |
cidr_value = self.get_effective_value(self.params.get("cidr"), "cidr") |
| 895 |
if cidr_value and not _is_valid_cidr(cidr_value): |
| 896 |
errors.append("Invalid IPv4 cidr format") |
| 897 |
|
| 898 |
cidr6_value = self.get_effective_value(self.params.get("cidr6"), "cidr6") |
| 899 |
if cidr6_value and not _is_valid_cidr6(cidr6_value): |
| 900 |
errors.append("Invalid IPv6 cidr format") |
| 901 |
|
| 902 |
# Gateway requires corresponding CIDR to be defined |
| 903 |
gateway_value = self.get_effective_value(self.params.get("gateway"), "gateway") |
| 904 |
if gateway_value and not cidr_value: |
| 905 |
errors.append("gateway cannot be set when cidr is not defined") |
| 906 |
elif gateway_value and not _is_valid_ipv4(gateway_value): |
| 907 |
errors.append("gateway must be a valid IPv4 address") |
| 908 |
|
| 909 |
gateway6_value = self.get_effective_value(self.params.get("gateway6"), "gateway6") |
| 910 |
if gateway6_value and not cidr6_value: |
| 911 |
errors.append("gateway6 cannot be set when cidr6 is not defined") |
| 912 |
elif gateway6_value and not _is_valid_ipv6(gateway6_value): |
| 913 |
errors.append("gateway6 must be a valid IPv6 address") |
| 914 |
|
| 915 |
return errors |
| 916 |
|
| 917 |
def validate_eth_params(self): |
| 918 |
"""Validate eth interface parameters.""" |
| 919 |
errors = [] |
| 920 |
return errors |
| 921 |
|
| 922 |
def validate_bridge_params(self): |
| 923 |
"""Validate bridge-specific parameters.""" |
| 924 |
errors = [] |
| 925 |
|
| 926 |
# bridge_vids requires bridge_vlan_aware to be enabled |
| 927 |
bridge_vids_value = self.get_effective_value(self.params.get("bridge_vids"), "bridge_vids") |
| 928 |
if bridge_vids_value is not None and not self.params.get("bridge_vlan_aware"): |
| 929 |
errors.append("bridge_vids should not be defined if bridge_vlan_aware is not set or false") |
| 930 |
|
| 931 |
return errors |
| 932 |
|
| 933 |
def validate_bond_params(self): |
| 934 |
"""Validate bond-specific parameters.""" |
| 935 |
errors = [] |
| 936 |
|
| 937 |
if not self.params.get("bond_mode"): |
| 938 |
errors.append("bond_mode is required for bond type") |
| 939 |
else: |
| 940 |
bond_mode = self.params.get("bond_mode") |
| 941 |
valid_modes = [ |
| 942 |
"balance-rr", |
| 943 |
"active-backup", |
| 944 |
"balance-xor", |
| 945 |
"broadcast", |
| 946 |
"802.3ad", |
| 947 |
"balance-tlb", |
| 948 |
"balance-alb", |
| 949 |
] |
| 950 |
if bond_mode not in valid_modes: |
| 951 |
errors.append(f"Invalid bond_mode for bond type. Must be one of: {', '.join(valid_modes)}") |
| 952 |
|
| 953 |
if not self.params.get("slaves"): |
| 954 |
errors.append("slaves is required for bond type") |
| 955 |
|
| 956 |
# Mode-specific validation |
| 957 |
bond_mode = self.params.get("bond_mode") |
| 958 |
if bond_mode == "active-backup": |
| 959 |
if not self.params.get("bond_primary"): |
| 960 |
errors.append("bond_primary is required for active-backup mode") |
| 961 |
else: |
| 962 |
bond_primary = self.params.get("bond_primary") |
| 963 |
slaves = self.params.get("slaves", "").split() |
| 964 |
if bond_primary not in slaves: |
| 965 |
errors.append("bond_primary must be included in slaves for active-backup mode") |
| 966 |
elif bond_mode in ["balance-xor", "802.3ad"] and not self.params.get("bond_xmit_hash_policy"): |
| 967 |
errors.append("bond_xmit_hash_policy is required for balance-xor and 802.3ad modes") |
| 968 |
|
| 969 |
# Validate parameter combinations based on bond mode |
| 970 |
if self.params.get("bond_primary") is not None and bond_mode != "active-backup": |
| 971 |
errors.append("bond_primary should not be defined if bond_mode is not active-backup") |
| 972 |
|
| 973 |
if self.params.get("bond_xmit_hash_policy") is not None and bond_mode not in [ |
| 974 |
"balance-xor", |
| 975 |
"802.3ad", |
| 976 |
]: |
| 977 |
errors.append("bond_xmit_hash_policy should not be defined if bond_mode is not balance-xor or 802.3ad") |
| 978 |
|
| 979 |
return errors |
| 980 |
|
| 981 |
def validate_vlan_params(self): |
| 982 |
"""Validate VLAN-specific parameters.""" |
| 983 |
errors = [] |
| 984 |
|
| 985 |
iface = self.params.get("iface", "") |
| 986 |
|
| 987 |
# Validate vlan_raw_device based on interface naming format |
| 988 |
if iface.startswith("vlan"): |
| 989 |
if not self.params.get("vlan_raw_device"): |
| 990 |
errors.append(f"vlan_raw_device is required for VLAN interface '{iface}' in vlanXY format") |
| 991 |
elif self.params.get("vlan_raw_device") is not None: |
| 992 |
errors.append( |
| 993 |
f"vlan_raw_device should not be specified for VLAN interface '{iface}' in iface_name.vlan_id format" |
| 994 |
) |
| 995 |
|
| 996 |
return errors |
| 997 |
|
| 998 |
def validate_ovs_bridge_params(self): |
| 999 |
"""Validate OVS bridge parameters.""" |
| 1000 |
errors = [] |
| 1001 |
return errors |
| 1002 |
|
| 1003 |
def validate_ovs_bond_params(self): |
| 1004 |
"""Validate OVS bond parameters.""" |
| 1005 |
errors = [] |
| 1006 |
|
| 1007 |
if not self.params.get("bond_mode"): |
| 1008 |
errors.append("bond_mode is required for OVSBond type") |
| 1009 |
else: |
| 1010 |
bond_mode = self.params.get("bond_mode") |
| 1011 |
valid_modes = [ |
| 1012 |
"active-backup", |
| 1013 |
"balance-slb", |
| 1014 |
"lacp-balance-slb", |
| 1015 |
"lacp-balance-tcp", |
| 1016 |
] |
| 1017 |
if bond_mode not in valid_modes: |
| 1018 |
errors.append(f"Invalid bond_mode for OVSBond. Must be one of: {', '.join(valid_modes)}") |
| 1019 |
|
| 1020 |
if not self.params.get("ovs_bonds"): |
| 1021 |
errors.append("ovs_bonds is required for OVSBond type") |
| 1022 |
|
| 1023 |
if not self.params.get("ovs_bridge"): |
| 1024 |
errors.append("ovs_bridge is required for OVSBond type") |
| 1025 |
|
| 1026 |
# Validate VLAN tag range (1-4094) |
| 1027 |
ovs_tag_value = self.get_effective_value(self.params.get("ovs_tag"), "ovs_tag") |
| 1028 |
if ovs_tag_value is not None: |
| 1029 |
try: |
| 1030 |
ovs_tag = int(ovs_tag_value) |
| 1031 |
if not (MIN_VLAN_ID <= ovs_tag <= MAX_VLAN_ID): |
| 1032 |
errors.append(f"ovs_tag must be between {MIN_VLAN_ID} and {MAX_VLAN_ID}") |
| 1033 |
except (ValueError, TypeError): |
| 1034 |
errors.append("ovs_tag must be an integer") |
| 1035 |
|
| 1036 |
return errors |
| 1037 |
|
| 1038 |
def validate_ovs_int_port_params(self): |
| 1039 |
"""Validate OVS internal port parameters.""" |
| 1040 |
errors = [] |
| 1041 |
|
| 1042 |
if not self.params.get("ovs_bridge"): |
| 1043 |
errors.append("ovs_bridge is required for OVSIntPort type") |
| 1044 |
|
| 1045 |
# Validate VLAN tag range (1-4094) |
| 1046 |
ovs_tag_value = self.get_effective_value(self.params.get("ovs_tag"), "ovs_tag") |
| 1047 |
if ovs_tag_value is not None: |
| 1048 |
try: |
| 1049 |
ovs_tag = int(ovs_tag_value) |
| 1050 |
if not (MIN_VLAN_ID <= ovs_tag <= MAX_VLAN_ID): |
| 1051 |
errors.append(f"ovs_tag must be between {MIN_VLAN_ID} and {MAX_VLAN_ID}") |
| 1052 |
except (ValueError, TypeError): |
| 1053 |
errors.append("ovs_tag must be an integer") |
| 1054 |
|
| 1055 |
return errors |
| 1056 |
|
| 1057 |
def validate_params(self): |
| 1058 |
"""Validate all parameters based on interface type.""" |
| 1059 |
errors = [] |
| 1060 |
|
| 1061 |
# Basic state requirements |
| 1062 |
state = self.params.get("state", "present") |
| 1063 |
if state == "present": |
| 1064 |
if not self.params.get("iface"): |
| 1065 |
errors.append("iface is required when state is present") |
| 1066 |
if not self.params.get("iface_type"): |
| 1067 |
errors.append("iface_type is required when state is present") |
| 1068 |
elif state == "absent": |
| 1069 |
if not self.params.get("iface"): |
| 1070 |
errors.append("iface is required when state is absent") |
| 1071 |
elif state in ["apply", "revert"]: |
| 1072 |
# For apply/revert states, only node parameter is allowed |
| 1073 |
allowed_params = ["node", "state"] |
| 1074 |
auth_params = list(proxmox_auth_argument_spec().keys()) |
| 1075 |
all_allowed_params = allowed_params + auth_params |
| 1076 |
|
| 1077 |
# Check for any parameters other than node, state, and auth params |
| 1078 |
for param_name, value in self.params.items(): |
| 1079 |
if value is not None and param_name not in all_allowed_params: |
| 1080 |
errors.append( |
| 1081 |
f"Parameter '{param_name}' is not allowed when state is '{state}'. Only 'node' parameter is accepted." |
| 1082 |
) |
| 1083 |
|
| 1084 |
# Return early for apply/revert states - no further validation needed |
| 1085 |
return errors |
| 1086 |
|
| 1087 |
# Interface name format validation |
| 1088 |
errors.extend(self.validate_interface_name()) |
| 1089 |
|
| 1090 |
# Parameter combination validation |
| 1091 |
errors.extend(self.validate_parameter_combinations()) |
| 1092 |
|
| 1093 |
if errors: |
| 1094 |
return errors |
| 1095 |
|
| 1096 |
# Parameter value validation |
| 1097 |
errors.extend(self.validate_common_params()) |
| 1098 |
|
| 1099 |
# Type-specific validation |
| 1100 |
iface_type = self.params.get("iface_type") |
| 1101 |
if iface_type: |
| 1102 |
errors.extend(self._validate_interface(iface_type)) |
| 1103 |
|
| 1104 |
return errors |
| 1105 |
|
| 1106 |
def _validate_interface(self, iface_type): |
| 1107 |
errors = [] |
| 1108 |
if iface_type == "eth": |
| 1109 |
errors.extend(self.validate_eth_params()) |
| 1110 |
elif iface_type == "bridge": |
| 1111 |
errors.extend(self.validate_bridge_params()) |
| 1112 |
elif iface_type == "bond": |
| 1113 |
errors.extend(self.validate_bond_params()) |
| 1114 |
elif iface_type == "vlan": |
| 1115 |
errors.extend(self.validate_vlan_params()) |
| 1116 |
elif iface_type == "OVSBridge": |
| 1117 |
errors.extend(self.validate_ovs_bridge_params()) |
| 1118 |
elif iface_type == "OVSBond": |
| 1119 |
errors.extend(self.validate_ovs_bond_params()) |
| 1120 |
elif iface_type == "OVSIntPort": |
| 1121 |
errors.extend(self.validate_ovs_int_port_params()) |
| 1122 |
return errors |
| 1123 |
|
| 1124 |
def validate_interface_name(self): |
| 1125 |
"""Validate interface name format based on interface type.""" |
| 1126 |
errors = [] |
| 1127 |
iface = self.params.get("iface") |
| 1128 |
iface_type = self.params.get("iface_type") |
| 1129 |
|
| 1130 |
if not iface or not iface_type: |
| 1131 |
return errors |
| 1132 |
|
| 1133 |
# Bond interfaces must follow bondX format (X = 0-MAX_BOND_NUMBER) |
| 1134 |
if iface_type in ["bond", "OVSBond"]: |
| 1135 |
if not re.match(r"^bond\d{1,5}$", iface): |
| 1136 |
errors.append( |
| 1137 |
f"Interface name '{iface}' for type '{iface_type}' must follow format 'bondX' where X is a number between 0 and {MAX_BOND_NUMBER}" |
| 1138 |
) |
| 1139 |
else: |
| 1140 |
bond_number = int(iface[4:]) |
| 1141 |
if bond_number > MAX_BOND_NUMBER: |
| 1142 |
errors.append(f"bond interface number must be between 0 and {MAX_BOND_NUMBER}, got {bond_number}") |
| 1143 |
|
| 1144 |
# VLAN interfaces support two formats: eth0.100 and vlan100 |
| 1145 |
elif iface_type == "vlan": |
| 1146 |
if not (re.match(r"^(?!vlan\.)[a-zA-Z0-9_-]+\.\d+$", iface) or re.match(r"^vlan\d+$", iface)): |
| 1147 |
errors.append( |
| 1148 |
f"VLAN interface name '{iface}' must follow format 'vlanXY' (e.g., vlan100) or 'iface_name.vlan_id' (e.g., eth0.100)" |
| 1149 |
) |
| 1150 |
else: |
| 1151 |
try: |
| 1152 |
if iface.startswith("vlan"): |
| 1153 |
vlan_id = int(iface[4:]) |
| 1154 |
else: |
| 1155 |
vlan_id = int(iface.split(".")[-1]) |
| 1156 |
if not (MIN_VLAN_ID <= vlan_id <= MAX_VLAN_ID): |
| 1157 |
errors.append(f"vlan_id must be between {MIN_VLAN_ID} and {MAX_VLAN_ID}, got {vlan_id}") |
| 1158 |
except (ValueError, TypeError): |
| 1159 |
errors.append("vlan_id must be a valid integer") |
| 1160 |
|
| 1161 |
return errors |
| 1162 |
|
| 1163 |
def validate_parameter_combinations(self): |
| 1164 |
"""Validate parameter combinations based on interface type.""" |
| 1165 |
errors = [] |
| 1166 |
iface_type = self.params.get("iface_type") |
| 1167 |
|
| 1168 |
if not iface_type: |
| 1169 |
return errors |
| 1170 |
|
| 1171 |
# Get parameters that are set and validate against allowed parameters |
| 1172 |
set_params = [key for key, value in self.params.items() if value is not None] |
| 1173 |
allowed_params = self.get_params_for_interface_type(iface_type) |
| 1174 |
core_params = self.get_core_params() |
| 1175 |
auth_params = list(proxmox_auth_argument_spec().keys()) |
| 1176 |
all_allowed_params = allowed_params + list(core_params) + auth_params |
| 1177 |
|
| 1178 |
invalid_params = [param for param in set_params if param not in all_allowed_params] |
| 1179 |
|
| 1180 |
if invalid_params: |
| 1181 |
errors.append(f"Parameters {', '.join(invalid_params)} are not valid for interface type '{iface_type}'") |
| 1182 |
|
| 1183 |
return errors |
| 1184 |
|
| 1185 |
def _is_eth_interface_deleted(self, interface): |
| 1186 |
"""Check if an eth interface is considered deleted (inactive).""" |
| 1187 |
has_priority = "priority" in interface |
| 1188 |
autostart = interface.get("autostart", False) |
| 1189 |
active = interface.get("active", False) |
| 1190 |
|
| 1191 |
# Interface is deleted if it has no priority, autostart is false, and active is false |
| 1192 |
return not has_priority and not autostart and not active |
| 1193 |
|
| 1194 |
def get_network_changes(self): |
| 1195 |
"""Check if there are pending network configuration changes. |
| 1196 |
|
| 1197 |
Workaround for proxmoxer module which doesn't provide access to the 'changes' field. |
| 1198 |
""" |
| 1199 |
try: |
| 1200 |
resp = self.proxmox_api._store["session"].request( |
| 1201 |
"GET", |
| 1202 |
f"{self.proxmox_api._store['base_url']}/nodes/{self.node}/network", |
| 1203 |
) |
| 1204 |
resp.raise_for_status() |
| 1205 |
changes = resp.json().get("changes", None) |
| 1206 |
return changes |
| 1207 |
except Exception as e: |
| 1208 |
self.module.fail_json(msg=f"Failed to check network changes for node {self.node}: {e}") |
| 1209 |
|
| 1210 |
def get_all_interfaces(self): |
| 1211 |
"""Get all network interfaces.""" |
| 1212 |
try: |
| 1213 |
interfaces = self.proxmox_api.nodes(self.node).network.get() |
| 1214 |
# Convert API parameter names to Ansible format and normalize values |
| 1215 |
converted_interfaces = [] |
| 1216 |
for interface in interfaces: |
| 1217 |
converted_interface = {} |
| 1218 |
for key, value in interface.items(): |
| 1219 |
converted_key = self.get_ansible_name(key) |
| 1220 |
normalized_value = value |
| 1221 |
if converted_key == "comments": |
| 1222 |
normalized_value = self.normalize_comment(value) |
| 1223 |
elif ( |
| 1224 |
isinstance(value, int) |
| 1225 |
and value in [0, 1] |
| 1226 |
and converted_key in PARAMETER_DEFINITIONS |
| 1227 |
and PARAMETER_DEFINITIONS[converted_key]["type"] == "bool" |
| 1228 |
): |
| 1229 |
normalized_value = proxmox_to_ansible_bool(value) |
| 1230 |
converted_interface[converted_key] = normalized_value |
| 1231 |
converted_interfaces.append(converted_interface) |
| 1232 |
return converted_interfaces |
| 1233 |
except Exception as e: |
| 1234 |
self.module.fail_json( |
| 1235 |
msg=f"Failed to get network interfaces: {to_native(e)}", |
| 1236 |
exception=traceback.format_exc(), |
| 1237 |
) |
| 1238 |
|
| 1239 |
def get_interface_config(self): |
| 1240 |
"""Get current interface configuration.""" |
| 1241 |
try: |
| 1242 |
interfaces = self.get_all_interfaces() |
| 1243 |
iface = self.params.get("iface") |
| 1244 |
|
| 1245 |
# Find the specific interface |
| 1246 |
for interface in interfaces: |
| 1247 |
if interface.get("iface") == iface: |
| 1248 |
return interface |
| 1249 |
|
| 1250 |
return None # Interface not found |
| 1251 |
except Exception as e: |
| 1252 |
self.module.fail_json( |
| 1253 |
msg=f"Failed to get interface configuration: {to_native(e)}", |
| 1254 |
exception=traceback.format_exc(), |
| 1255 |
) |
| 1256 |
|
| 1257 |
def create_interface(self): |
| 1258 |
"""Create network interface.""" |
| 1259 |
try: |
| 1260 |
data = self._build_interface_data() |
| 1261 |
# Remove 'delete' property for creation (only valid for updates) |
| 1262 |
if "delete" in data: |
| 1263 |
del data["delete"] |
| 1264 |
self.proxmox_api.nodes(self.node).network.post(**data) |
| 1265 |
return True |
| 1266 |
except Exception as e: |
| 1267 |
self.module.fail_json( |
| 1268 |
msg=f"Failed to create interface: {to_native(e)}", |
| 1269 |
exception=traceback.format_exc(), |
| 1270 |
) |
| 1271 |
|
| 1272 |
def update_interface(self): |
| 1273 |
"""Update network interface.""" |
| 1274 |
try: |
| 1275 |
data = self._build_interface_data() |
| 1276 |
iface = self.params.get("iface") |
| 1277 |
self.proxmox_api.nodes(self.node).network(iface).put(**data) |
| 1278 |
return True |
| 1279 |
except Exception as e: |
| 1280 |
self.module.fail_json( |
| 1281 |
msg=f"Failed to update interface: {to_native(e)}", |
| 1282 |
exception=traceback.format_exc(), |
| 1283 |
) |
| 1284 |
|
| 1285 |
def delete_interface(self): |
| 1286 |
"""Delete network interface.""" |
| 1287 |
try: |
| 1288 |
iface = self.params.get("iface") |
| 1289 |
self.proxmox_api.nodes(self.node).network(iface).delete() |
| 1290 |
return True |
| 1291 |
except Exception as e: |
| 1292 |
self.module.fail_json( |
| 1293 |
msg=f"Failed to delete interface: {to_native(e)}", |
| 1294 |
exception=traceback.format_exc(), |
| 1295 |
) |
| 1296 |
|
| 1297 |
def apply_network(self): |
| 1298 |
"""Apply network configuration.""" |
| 1299 |
try: |
| 1300 |
self.proxmox_api.nodes(self.node).network.put() |
| 1301 |
return True |
| 1302 |
except Exception as e: |
| 1303 |
self.module.fail_json( |
| 1304 |
msg=f"Failed to apply network: {to_native(e)}", |
| 1305 |
exception=traceback.format_exc(), |
| 1306 |
) |
| 1307 |
|
| 1308 |
def revert_network(self): |
| 1309 |
"""Revert network configuration.""" |
| 1310 |
try: |
| 1311 |
self.proxmox_api.nodes(self.node).network.delete() |
| 1312 |
return True |
| 1313 |
except Exception as e: |
| 1314 |
self.module.fail_json( |
| 1315 |
msg=f"Failed to revert network: {to_native(e)}", |
| 1316 |
exception=traceback.format_exc(), |
| 1317 |
) |
| 1318 |
|
| 1319 |
def execute(self): |
| 1320 |
"""Execute the network management operation.""" |
| 1321 |
state = self.params.get("state", "present") |
| 1322 |
|
| 1323 |
# Validate node exists before any API calls |
| 1324 |
try: |
| 1325 |
node_info = self.get_node(self.node) |
| 1326 |
if not node_info: |
| 1327 |
self.module.fail_json(msg=f"Node '{self.node}' not found in the Proxmox cluster") |
| 1328 |
except Exception as e: |
| 1329 |
self.module.fail_json(msg=f"Failed to validate node '{self.node}': {e}") |
| 1330 |
|
| 1331 |
try: |
| 1332 |
if state == "present": |
| 1333 |
return self._handle_present_state() |
| 1334 |
elif state == "absent": |
| 1335 |
return self._handle_absent_state() |
| 1336 |
elif state == "apply": |
| 1337 |
return self._handle_apply_state() |
| 1338 |
elif state == "revert": |
| 1339 |
return self._handle_revert_state() |
| 1340 |
except Exception as e: |
| 1341 |
self.module.fail_json( |
| 1342 |
msg=f"Failed to manage network interface: {to_native(e)}", |
| 1343 |
exception=traceback.format_exc(), |
| 1344 |
) |
| 1345 |
|
| 1346 |
def _handle_present_state(self): |
| 1347 |
"""Handle present state (create or update).""" |
| 1348 |
iface = self.params.get("iface") |
| 1349 |
current_config = self.get_interface_config() |
| 1350 |
|
| 1351 |
if current_config: |
| 1352 |
# Check if interface type is being changed (not allowed) |
| 1353 |
current_type = current_config.get("iface_type") |
| 1354 |
desired_type = self.params.get("iface_type") |
| 1355 |
if current_type and desired_type and current_type != desired_type: |
| 1356 |
self.module.fail_json( |
| 1357 |
msg=f"Cannot change interface type from '{current_type}' to '{desired_type}'. " |
| 1358 |
f"Interface type cannot be modified after creation. " |
| 1359 |
f"Delete the interface and recreate it with the desired type." |
| 1360 |
) |
| 1361 |
|
| 1362 |
if self._has_differences(current_config): |
| 1363 |
if not self.module.check_mode: |
| 1364 |
self.update_interface() |
| 1365 |
updated_config = self.get_interface_config() |
| 1366 |
msg = f"Interface {iface} updated successfully" |
| 1367 |
after_config = updated_config |
| 1368 |
before_config = current_config |
| 1369 |
else: |
| 1370 |
after_config = self._build_interface_config() |
| 1371 |
before_config = self._filter_config_to_user_params(current_config) |
| 1372 |
msg = f"Interface {iface} would be updated" |
| 1373 |
|
| 1374 |
return { |
| 1375 |
"changed": True, |
| 1376 |
"msg": msg, |
| 1377 |
"interface": after_config, |
| 1378 |
"diff": self._format_diff(before_config, after_config, iface), |
| 1379 |
} |
| 1380 |
else: |
| 1381 |
return { |
| 1382 |
"changed": False, |
| 1383 |
"msg": f"Interface {iface} already exists with correct configuration", |
| 1384 |
"interface": current_config, |
| 1385 |
} |
| 1386 |
else: |
| 1387 |
# Prevent creation of eth interfaces (physical hardware) |
| 1388 |
iface_type = self.params.get("iface_type") |
| 1389 |
if iface_type == "eth": |
| 1390 |
self.module.fail_json( |
| 1391 |
msg=f"Cannot create interface '{iface}' of type 'eth'. " |
| 1392 |
f"Ethernet interfaces represent physical hardware and cannot be created via API. " |
| 1393 |
f"Only existing physical interfaces can be configured." |
| 1394 |
) |
| 1395 |
|
| 1396 |
if not self.module.check_mode: |
| 1397 |
self.create_interface() |
| 1398 |
created_config = self.get_interface_config() |
| 1399 |
msg = f"Interface {iface} created successfully" |
| 1400 |
else: |
| 1401 |
created_config = self._build_interface_config() |
| 1402 |
msg = f"Interface {iface} would be created" |
| 1403 |
|
| 1404 |
return { |
| 1405 |
"changed": True, |
| 1406 |
"msg": msg, |
| 1407 |
"interface": created_config, |
| 1408 |
"diff": self._format_diff(None, created_config, iface), |
| 1409 |
} |
| 1410 |
|
| 1411 |
def _handle_absent_state(self): |
| 1412 |
"""Handle absent state (delete).""" |
| 1413 |
iface = self.params.get("iface") |
| 1414 |
iface_type = self.params.get("iface_type") |
| 1415 |
current_config = self.get_interface_config() |
| 1416 |
|
| 1417 |
# Special handling for eth interfaces (check if actually deleted/inactive) |
| 1418 |
if iface_type == "eth" and current_config and self._is_eth_interface_deleted(current_config): |
| 1419 |
return {"changed": False, "msg": f"Interface {iface} does not exist"} |
| 1420 |
|
| 1421 |
if current_config: |
| 1422 |
if not self.module.check_mode: |
| 1423 |
self.delete_interface() |
| 1424 |
msg = f"Interface {iface} deleted successfully" |
| 1425 |
else: |
| 1426 |
msg = f"Interface {iface} would be deleted" |
| 1427 |
return { |
| 1428 |
"changed": True, |
| 1429 |
"msg": msg, |
| 1430 |
"diff": self._format_diff(current_config, None, iface), |
| 1431 |
} |
| 1432 |
else: |
| 1433 |
return {"changed": False, "msg": f"Interface {iface} does not exist"} |
| 1434 |
|
| 1435 |
def _handle_apply_state(self): |
| 1436 |
"""Handle apply state.""" |
| 1437 |
if self.module.check_mode: |
| 1438 |
return { |
| 1439 |
"changed": True, |
| 1440 |
"msg": "Staged network configuration changes may be applied", |
| 1441 |
} |
| 1442 |
|
| 1443 |
changes = self.get_network_changes() |
| 1444 |
|
| 1445 |
if changes: |
| 1446 |
self.apply_network() |
| 1447 |
return { |
| 1448 |
"changed": True, |
| 1449 |
"msg": "Staged network configuration changes applied successfully", |
| 1450 |
"diff": {"prepared": changes}, |
| 1451 |
} |
| 1452 |
else: |
| 1453 |
return { |
| 1454 |
"changed": False, |
| 1455 |
"msg": "No staged network configuration changes to apply", |
| 1456 |
} |
| 1457 |
|
| 1458 |
def _handle_revert_state(self): |
| 1459 |
"""Handle revert state.""" |
| 1460 |
if self.module.check_mode: |
| 1461 |
return { |
| 1462 |
"changed": True, |
| 1463 |
"msg": "Staged network configuration changes may be reverted", |
| 1464 |
} |
| 1465 |
|
| 1466 |
changes = self.get_network_changes() |
| 1467 |
|
| 1468 |
if changes: |
| 1469 |
self.revert_network() |
| 1470 |
return { |
| 1471 |
"changed": True, |
| 1472 |
"msg": "Staged network configuration changes reverted successfully", |
| 1473 |
} |
| 1474 |
else: |
| 1475 |
return { |
| 1476 |
"changed": False, |
| 1477 |
"msg": "No staged network configuration changes to revert", |
| 1478 |
} |
| 1479 |
|
| 1480 |
def _build_interface_data(self): |
| 1481 |
"""Build interface data for API calls.""" |
| 1482 |
data = {} |
| 1483 |
delete_list = [] |
| 1484 |
|
| 1485 |
if self.params.get("iface"): |
| 1486 |
data["iface"] = self.params.get("iface") |
| 1487 |
|
| 1488 |
if self.params.get("iface_type"): |
| 1489 |
data["type"] = self.params.get("iface_type") |
| 1490 |
|
| 1491 |
# Process all parameters for the interface type |
| 1492 |
for param_name in self.get_params_for_interface_type(self.params.get("iface_type")): |
| 1493 |
if self.params.get(param_name) is not None: |
| 1494 |
value = self.params.get(param_name) |
| 1495 |
|
| 1496 |
# Handle delete intentions (empty string for str, -1 for int) |
| 1497 |
if self.is_delete_intention(value, param_name): |
| 1498 |
api_param_name = self.get_api_name(param_name) |
| 1499 |
delete_list.append(api_param_name) |
| 1500 |
else: |
| 1501 |
# Handle boolean values: False = delete, True = set to 1 |
| 1502 |
if isinstance(value, bool): |
| 1503 |
if value is False: |
| 1504 |
api_param_name = self.get_api_name(param_name) |
| 1505 |
delete_list.append(api_param_name) |
| 1506 |
continue |
| 1507 |
else: |
| 1508 |
value = ansible_to_proxmox_bool(value) |
| 1509 |
|
| 1510 |
api_param_name = self.get_api_name(param_name) |
| 1511 |
data[api_param_name] = value |
| 1512 |
|
| 1513 |
if delete_list: |
| 1514 |
data["delete"] = delete_list |
| 1515 |
|
| 1516 |
return data |
| 1517 |
|
| 1518 |
def _has_differences(self, current_config): |
| 1519 |
"""Check if there are differences between current and desired configuration.""" |
| 1520 |
core_params = self.get_core_params() |
| 1521 |
network_params = module_args() |
| 1522 |
for param_name in self.params: |
| 1523 |
if ( |
| 1524 |
param_name in network_params |
| 1525 |
and param_name not in core_params |
| 1526 |
and self.params.get(param_name) is not None |
| 1527 |
): |
| 1528 |
current_value = current_config.get(param_name) |
| 1529 |
desired_value = self.params.get(param_name) |
| 1530 |
|
| 1531 |
# Check if parameter should be deleted |
| 1532 |
if self.is_delete_intention(desired_value, param_name): |
| 1533 |
if not self.is_effectively_deleted(current_value, param_name): |
| 1534 |
return True |
| 1535 |
continue |
| 1536 |
|
| 1537 |
# Handle boolean comparison with Proxmox format conversion |
| 1538 |
if isinstance(desired_value, bool): |
| 1539 |
if current_value is None: |
| 1540 |
# Assume False if not present in config |
| 1541 |
if desired_value is not False: |
| 1542 |
return True |
| 1543 |
continue |
| 1544 |
if current_value in [0, 1]: |
| 1545 |
current_value = proxmox_to_ansible_bool(current_value) |
| 1546 |
desired_value_proxmox = ansible_to_proxmox_bool(desired_value) |
| 1547 |
if current_value != desired_value_proxmox: |
| 1548 |
return True |
| 1549 |
elif param_name == "comments": |
| 1550 |
# Normalize comments for comparison |
| 1551 |
current_value_normalized = self.normalize_comment(current_value) |
| 1552 |
if current_value_normalized != desired_value: |
| 1553 |
return True |
| 1554 |
elif str(current_value) != str(desired_value): |
| 1555 |
return True |
| 1556 |
|
| 1557 |
return False |
| 1558 |
|
| 1559 |
def _format_diff(self, before_config, after_config, iface): |
| 1560 |
"""Format configuration differences in YAML format for Ansible diff.""" |
| 1561 |
# Determine operation type for diff formatting |
| 1562 |
is_create = before_config is None and after_config is not None |
| 1563 |
is_delete = before_config is not None and after_config is None |
| 1564 |
|
| 1565 |
if is_create: |
| 1566 |
diff_entry = { |
| 1567 |
"after": yaml_dump(after_config, default_flow_style=False, indent=2), |
| 1568 |
"after_header": iface, |
| 1569 |
"before": None, |
| 1570 |
} |
| 1571 |
elif is_delete: |
| 1572 |
diff_entry = { |
| 1573 |
"before": yaml_dump(before_config, default_flow_style=False, indent=2), |
| 1574 |
"before_header": iface, |
| 1575 |
"after": None, |
| 1576 |
} |
| 1577 |
else: |
| 1578 |
diff_entry = { |
| 1579 |
"before": yaml_dump(before_config, default_flow_style=False, indent=2), |
| 1580 |
"after": yaml_dump(after_config, default_flow_style=False, indent=2), |
| 1581 |
"before_header": iface, |
| 1582 |
"after_header": iface, |
| 1583 |
} |
| 1584 |
|
| 1585 |
return [diff_entry] |
| 1586 |
|
| 1587 |
def _filter_config_to_user_params(self, config): |
| 1588 |
"""Filter configuration to only include parameters that the user has defined.""" |
| 1589 |
if config is None: |
| 1590 |
return None |
| 1591 |
|
| 1592 |
filtered_config = {} |
| 1593 |
|
| 1594 |
# Always include interface identification |
| 1595 |
if "iface" in config: |
| 1596 |
filtered_config["iface"] = config["iface"] |
| 1597 |
if "iface_type" in config: |
| 1598 |
filtered_config["iface_type"] = config["iface_type"] |
| 1599 |
|
| 1600 |
# Include only parameters that the user has set |
| 1601 |
for param_name in self.get_all_valid_params(): |
| 1602 |
if self.params.get(param_name) is not None and param_name in config: |
| 1603 |
filtered_config[param_name] = config[param_name] |
| 1604 |
|
| 1605 |
return filtered_config |
| 1606 |
|
| 1607 |
def _build_interface_config(self): |
| 1608 |
"""Build interface configuration dictionary.""" |
| 1609 |
config = { |
| 1610 |
"iface": self.params.get("iface"), |
| 1611 |
"iface_type": self.params.get("iface_type"), |
| 1612 |
} |
| 1613 |
|
| 1614 |
# Add all parameters that are set, with proper value conversion |
| 1615 |
for param_name in self.get_all_valid_params(): |
| 1616 |
if self.params.get(param_name) is not None: |
| 1617 |
value = self.params.get(param_name) |
| 1618 |
|
| 1619 |
# Skip delete intentions in config output |
| 1620 |
if self.is_delete_intention(value, param_name): |
| 1621 |
continue |
| 1622 |
|
| 1623 |
# Convert boolean values to Proxmox format for consistency |
| 1624 |
if isinstance(value, bool): |
| 1625 |
value = ansible_to_proxmox_bool(value) |
| 1626 |
|
| 1627 |
# Normalize comments for return data |
| 1628 |
if param_name == "comments" and isinstance(value, str): |
| 1629 |
value = self.normalize_comment(value) |
| 1630 |
|
| 1631 |
config[param_name] = value |
| 1632 |
|
| 1633 |
return config |
| 1634 |
|
| 1635 |
|
| 1636 |
def main(): |
| 1637 |
"""Main function.""" |
| 1638 |
module = create_proxmox_module(module_args(), **module_options()) |
| 1639 |
proxmox = ProxmoxNetworkManager(module) |
| 1640 |
|
| 1641 |
# Validate parameters |
| 1642 |
validation_errors = proxmox.validate_params() |
| 1643 |
if validation_errors: |
| 1644 |
module.fail_json(msg="Parameter validation failed: " + "; ".join(validation_errors)) |
| 1645 |
|
| 1646 |
# Execute the operation |
| 1647 |
result = proxmox.execute() |
| 1648 |
|
| 1649 |
module.exit_json(**result) |
| 1650 |
|
| 1651 |
|
| 1652 |
if __name__ == "__main__": |
| 1653 |
main() |