| 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_info |
| 10 |
short_description: Retrieve information about Proxmox VE node network interfaces |
| 11 |
version_added: "1.4.0" |
| 12 |
description: |
| 13 |
- Retrieve information about network interfaces on Proxmox VE nodes. |
| 14 |
- This module does not make any changes to the system. |
| 15 |
author: "aleskxyz (@aleskxyz)" |
| 16 |
options: |
| 17 |
node: |
| 18 |
description: |
| 19 |
- The Proxmox node to retrieve network interface information from. |
| 20 |
type: str |
| 21 |
required: true |
| 22 |
iface: |
| 23 |
description: |
| 24 |
- Name of a specific network interface to retrieve information for. |
| 25 |
- If not specified, information for all interfaces will be returned. |
| 26 |
type: str |
| 27 |
required: false |
| 28 |
iface_type: |
| 29 |
description: |
| 30 |
- Filter results by interface type. |
| 31 |
type: str |
| 32 |
choices: ['bridge', 'bond', 'eth', 'vlan', 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort'] |
| 33 |
required: false |
| 34 |
check_changes: |
| 35 |
description: |
| 36 |
- Whether to check for pending network configuration changes. |
| 37 |
- When enabled, the module will return only information about any staged changes that are waiting to be applied. |
| 38 |
- When disabled (default), the module will return network interface information. |
| 39 |
- This parameter cannot be used together with C(iface) or C(iface_type) parameters, as checking for pending changes is a node-level operation. |
| 40 |
type: bool |
| 41 |
default: false |
| 42 |
required: false |
| 43 |
seealso: |
| 44 |
- module: community.proxmox.proxmox_node_network |
| 45 |
description: Manage network interfaces on Proxmox nodes. |
| 46 |
- name: Proxmox Network Configuration |
| 47 |
description: Proxmox VE network configuration documentation. |
| 48 |
link: "https://pve.proxmox.com/wiki/Network_Configuration" |
| 49 |
- name: Proxmox API Documentation |
| 50 |
description: Proxmox VE API documentation. |
| 51 |
link: "https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/network" |
| 52 |
attributes: |
| 53 |
check_mode: |
| 54 |
support: full |
| 55 |
details: Check mode is fully supported. |
| 56 |
diff_mode: |
| 57 |
support: none |
| 58 |
details: This is an info module and does not make changes. |
| 59 |
extends_documentation_fragment: |
| 60 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 61 |
- community.proxmox.proxmox.documentation |
| 62 |
- community.proxmox.attributes |
| 63 |
- community.proxmox.attributes.info_module |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Get all network interfaces on a node |
| 68 |
community.proxmox.proxmox_node_network_info: |
| 69 |
node: pve01 |
| 70 |
|
| 71 |
- name: Get information about a specific network interface |
| 72 |
community.proxmox.proxmox_node_network_info: |
| 73 |
node: pve01 |
| 74 |
iface: vmbr0 |
| 75 |
|
| 76 |
- name: Get all bridge interfaces on a node |
| 77 |
community.proxmox.proxmox_node_network_info: |
| 78 |
node: pve01 |
| 79 |
iface_type: bridge |
| 80 |
|
| 81 |
- name: Check only for pending changes |
| 82 |
community.proxmox.proxmox_node_network_info: |
| 83 |
node: pve01 |
| 84 |
check_changes: true |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = r""" |
| 88 |
proxmox_node_networks: |
| 89 |
description: List of network interfaces on the node |
| 90 |
returned: success, when check_changes is false or not specified |
| 91 |
type: list |
| 92 |
elements: dict |
| 93 |
sample: [ |
| 94 |
{ |
| 95 |
"iface": "vmbr0", |
| 96 |
"type": "bridge", |
| 97 |
"active": true, |
| 98 |
"autostart": true, |
| 99 |
"bridge_ports": "eth0", |
| 100 |
"cidr": "192.168.1.1/24", |
| 101 |
"address": "192.168.1.1", |
| 102 |
"netmask": "255.255.255.0", |
| 103 |
"gateway": "192.168.1.254", |
| 104 |
"mtu": 1500, |
| 105 |
"method": "static", |
| 106 |
"families": ["inet"], |
| 107 |
"exists": true |
| 108 |
}, |
| 109 |
{ |
| 110 |
"iface": "bond0", |
| 111 |
"type": "bond", |
| 112 |
"active": true, |
| 113 |
"autostart": true, |
| 114 |
"slaves": "eth1 eth2", |
| 115 |
"bond_mode": "active-backup", |
| 116 |
"bond-primary": "eth1", |
| 117 |
"mtu": 1500, |
| 118 |
"method": "manual", |
| 119 |
"families": ["inet"], |
| 120 |
"exists": true |
| 121 |
} |
| 122 |
] |
| 123 |
pending_changes: |
| 124 |
description: Pending network configuration changes |
| 125 |
returned: success, when check_changes is true |
| 126 |
type: str |
| 127 |
sample: | |
| 128 |
--- /etc/network/interfaces |
| 129 |
+++ /etc/network/interfaces |
| 130 |
@@ -10,6 +10,12 @@ |
| 131 |
# The primary network interface |
| 132 |
auto eth0 |
| 133 |
iface eth0 inet dhcp |
| 134 |
+ |
| 135 |
+# Bridge interface |
| 136 |
+auto vmbr1 |
| 137 |
+iface vmbr1 inet static |
| 138 |
+ address 192.168.2.1/24 |
| 139 |
+ bridge_ports eth1 |
| 140 |
has_pending_changes: |
| 141 |
description: Whether there are any pending network configuration changes |
| 142 |
returned: success, when check_changes is true |
| 143 |
type: bool |
| 144 |
sample: true |
| 145 |
""" |
| 146 |
|
| 147 |
import traceback |
| 148 |
|
| 149 |
from ansible.module_utils.common.text.converters import to_native |
| 150 |
|
| 151 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 152 |
ProxmoxAnsible, |
| 153 |
create_proxmox_module, |
| 154 |
proxmox_to_ansible_bool, |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
def module_args(): |
| 159 |
return dict( |
| 160 |
node=dict(type="str", required=True), |
| 161 |
iface=dict(type="str", required=False), |
| 162 |
iface_type=dict( |
| 163 |
type="str", |
| 164 |
required=False, |
| 165 |
choices=[ |
| 166 |
"bridge", |
| 167 |
"bond", |
| 168 |
"eth", |
| 169 |
"vlan", |
| 170 |
"OVSBridge", |
| 171 |
"OVSBond", |
| 172 |
"OVSPort", |
| 173 |
"OVSIntPort", |
| 174 |
], |
| 175 |
), |
| 176 |
check_changes=dict(type="bool", default=False, required=False), |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
def module_options(): |
| 181 |
return {} |
| 182 |
|
| 183 |
|
| 184 |
class ProxmoxNodeNetworkInfoAnsible(ProxmoxAnsible): |
| 185 |
def check_network_changes(self, node): |
| 186 |
"""Check if there are pending network configuration changes. |
| 187 |
|
| 188 |
This is a workaround for the proxmoxer module which doesn't provide |
| 189 |
access to the 'changes' field in the API response. We use a direct |
| 190 |
HTTP request to access this field for idempotency checks. |
| 191 |
""" |
| 192 |
try: |
| 193 |
resp = self.proxmox_api._store["session"].request( |
| 194 |
"GET", f"{self.proxmox_api._store['base_url']}/nodes/{node}/network" |
| 195 |
) |
| 196 |
resp.raise_for_status() |
| 197 |
changes = resp.json().get("changes", None) |
| 198 |
return changes |
| 199 |
except Exception as e: |
| 200 |
self.module.fail_json( |
| 201 |
msg=f"Failed to check network changes for node {node}: {to_native(e)}", |
| 202 |
exception=traceback.format_exc(), |
| 203 |
) |
| 204 |
|
| 205 |
def convert_boolean_values(self, network_data): |
| 206 |
"""Convert boolean values from Proxmox format (0/1) to Ansible format (True/False).""" |
| 207 |
# List of known boolean fields in network interface data |
| 208 |
boolean_fields = ["active", "autostart", "bridge_vlan_aware", "exists"] |
| 209 |
|
| 210 |
converted_data = {} |
| 211 |
for key, value in network_data.items(): |
| 212 |
if key in boolean_fields and isinstance(value, int) and value in [0, 1]: |
| 213 |
converted_data[key] = proxmox_to_ansible_bool(value) |
| 214 |
else: |
| 215 |
converted_data[key] = value |
| 216 |
|
| 217 |
return converted_data |
| 218 |
|
| 219 |
def run(self): |
| 220 |
node = self.module.params["node"] |
| 221 |
iface = self.module.params.get("iface") |
| 222 |
iface_type = self.module.params.get("iface_type") |
| 223 |
check_changes = self.module.params.get("check_changes") |
| 224 |
result = dict(changed=False) |
| 225 |
|
| 226 |
# Validate parameter combinations |
| 227 |
if check_changes and (iface or iface_type): |
| 228 |
self.module.fail_json( |
| 229 |
msg="check_changes cannot be used with iface or iface_type parameters. " |
| 230 |
"Checking for pending changes is a node-level operation that applies to all network interfaces." |
| 231 |
) |
| 232 |
|
| 233 |
# Validate node exists |
| 234 |
try: |
| 235 |
node_info = self.get_node(node) |
| 236 |
if not node_info: |
| 237 |
self.module.fail_json(msg=f"Node '{node}' not found in the Proxmox cluster") |
| 238 |
except Exception as e: |
| 239 |
self.module.fail_json( |
| 240 |
msg=f"Failed to validate node '{node}': {to_native(e)}", |
| 241 |
exception=traceback.format_exc(), |
| 242 |
) |
| 243 |
|
| 244 |
try: |
| 245 |
# Check for pending changes if requested |
| 246 |
if check_changes: |
| 247 |
pending_changes = self.check_network_changes(node) |
| 248 |
result["pending_changes"] = pending_changes |
| 249 |
result["has_pending_changes"] = pending_changes is not None and len(pending_changes.strip()) > 0 |
| 250 |
return result |
| 251 |
|
| 252 |
# Get all interfaces or filter by type using API parameter |
| 253 |
if iface_type: |
| 254 |
networks = self.proxmox_api.nodes(node).network.get(type=iface_type) |
| 255 |
else: |
| 256 |
networks = self.proxmox_api.nodes(node).network.get() |
| 257 |
|
| 258 |
# Convert boolean values for each network interface |
| 259 |
converted_networks = [] |
| 260 |
for network in networks: |
| 261 |
converted_network = self.convert_boolean_values(network) |
| 262 |
converted_networks.append(converted_network) |
| 263 |
|
| 264 |
if iface: |
| 265 |
# Search for interface by name |
| 266 |
converted_networks = [network for network in converted_networks if network["iface"] == iface] |
| 267 |
|
| 268 |
result["proxmox_node_networks"] = converted_networks |
| 269 |
|
| 270 |
except Exception as e: |
| 271 |
if iface: |
| 272 |
self.module.fail_json( |
| 273 |
msg=f"Failed to retrieve information for interface '{iface}' on node '{node}': {to_native(e)}", |
| 274 |
exception=traceback.format_exc(), |
| 275 |
) |
| 276 |
else: |
| 277 |
self.module.fail_json( |
| 278 |
msg=f"Failed to retrieve network interfaces from node '{node}': {to_native(e)}", |
| 279 |
exception=traceback.format_exc(), |
| 280 |
) |
| 281 |
|
| 282 |
return result |
| 283 |
|
| 284 |
|
| 285 |
def main(): |
| 286 |
module = create_proxmox_module(module_args(), **module_options()) |
| 287 |
proxmox = ProxmoxNodeNetworkInfoAnsible(module) |
| 288 |
|
| 289 |
result = proxmox.run() |
| 290 |
module.exit_json(**result) |
| 291 |
|
| 292 |
|
| 293 |
if __name__ == "__main__": |
| 294 |
main() |