| 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_ipam_info |
| 10 |
short_description: Retrieve information about IPAMs. |
| 11 |
version_added: "1.4.0" |
| 12 |
description: |
| 13 |
- Retrieve all IPs under IPAM and limit it by IP or IPAM. |
| 14 |
author: 'Jana Hoch <[email protected]> (!UNKNOWN)' |
| 15 |
options: |
| 16 |
ipam: |
| 17 |
description: |
| 18 |
- Limit results to a single IPAM. |
| 19 |
type: str |
| 20 |
vmid: |
| 21 |
description: |
| 22 |
- Get IP of a VM under IPAM. |
| 23 |
type: int |
| 24 |
|
| 25 |
extends_documentation_fragment: |
| 26 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 27 |
- community.proxmox.proxmox.documentation |
| 28 |
- community.proxmox.attributes |
| 29 |
- community.proxmox.attributes.info_module |
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
- name: Get all IPs under all IPAM |
| 34 |
community.proxmox.proxmox_ipam_info: |
| 35 |
|
| 36 |
- name: Get all IPs under pve IPAM |
| 37 |
community.proxmox.proxmox_ipam_info: |
| 38 |
ipam: pve |
| 39 |
|
| 40 |
- name: Get IP under IPAM of vmid 102 |
| 41 |
community.proxmox.proxmox_ipam_info: |
| 42 |
vmid: 102 |
| 43 |
""" |
| 44 |
|
| 45 |
RETURN = r""" |
| 46 |
ips: |
| 47 |
description: Filter by vmid |
| 48 |
returned: on success |
| 49 |
type: list |
| 50 |
elements: dict |
| 51 |
sample: |
| 52 |
[ |
| 53 |
{ |
| 54 |
"hostname": "ns3.proxmox.pc", |
| 55 |
"ip": "10.10.5.5", |
| 56 |
"mac": "BC:24:11:0E:72:04", |
| 57 |
"subnet": "10.10.5.0/24", |
| 58 |
"vmid": 102, |
| 59 |
"vnet": "test", |
| 60 |
"zone": "ans1" |
| 61 |
}, |
| 62 |
{ |
| 63 |
"hostname": "ns3.proxmox.pc", |
| 64 |
"ip": "10.10.0.8", |
| 65 |
"mac": "BC:24:11:F3:B1:81", |
| 66 |
"subnet": "10.10.0.0/24", |
| 67 |
"vmid": 102, |
| 68 |
"vnet": "test2", |
| 69 |
"zone": "test1" |
| 70 |
} |
| 71 |
] |
| 72 |
ipams: |
| 73 |
description: List of all IPAMs and IPs under them. |
| 74 |
returned: on success |
| 75 |
type: dict |
| 76 |
elements: dict |
| 77 |
sample: |
| 78 |
{ |
| 79 |
"pve": [ |
| 80 |
{ |
| 81 |
"gateway": 1, |
| 82 |
"ip": "10.10.1.0", |
| 83 |
"subnet": "10.10.1.0/24", |
| 84 |
"vnet": "test2", |
| 85 |
"zone": "test1" |
| 86 |
}, |
| 87 |
{ |
| 88 |
"hostname": "ns3.proxmox.pc.test3", |
| 89 |
"ip": "10.10.0.6", |
| 90 |
"mac": "BC:24:11:0E:72:04", |
| 91 |
"subnet": "10.10.0.0/24", |
| 92 |
"vmid": 102, |
| 93 |
"vnet": "test2", |
| 94 |
"zone": "test1" |
| 95 |
}, |
| 96 |
{ |
| 97 |
"hostname": "ns4.proxmox.pc", |
| 98 |
"ip": "10.10.0.7", |
| 99 |
"mac": "BC:24:11:D5:CD:82", |
| 100 |
"subnet": "10.10.0.0/24", |
| 101 |
"vmid": 103, |
| 102 |
"vnet": "test2", |
| 103 |
"zone": "test1" |
| 104 |
}, |
| 105 |
{ |
| 106 |
"gateway": 1, |
| 107 |
"ip": "10.10.0.1", |
| 108 |
"subnet": "10.10.0.0/24", |
| 109 |
"vnet": "test2", |
| 110 |
"zone": "test1" |
| 111 |
}, |
| 112 |
{ |
| 113 |
"hostname": "ns2.proxmox.pc.test3", |
| 114 |
"ip": "10.10.0.5", |
| 115 |
"mac": "BC:24:11:86:77:56", |
| 116 |
"subnet": "10.10.0.0/24", |
| 117 |
"vmid": 101, |
| 118 |
"vnet": "test2", |
| 119 |
"zone": "test1" |
| 120 |
} |
| 121 |
] |
| 122 |
} |
| 123 |
""" |
| 124 |
|
| 125 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 126 |
ProxmoxAnsible, |
| 127 |
create_proxmox_module, |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
def module_args(): |
| 132 |
return dict(ipam=dict(type="str", required=False), vmid=dict(type="int", required=False)) |
| 133 |
|
| 134 |
|
| 135 |
def module_options(): |
| 136 |
return {} |
| 137 |
|
| 138 |
|
| 139 |
class ProxmoxIpamInfoAnsible(ProxmoxAnsible): |
| 140 |
def __init__(self, module): |
| 141 |
super().__init__(module) |
| 142 |
self.params = module.params |
| 143 |
|
| 144 |
def run(self): |
| 145 |
vmid = self.params.get("vmid") |
| 146 |
ipam = self.params.get("ipam") |
| 147 |
if vmid: |
| 148 |
self.module.exit_json(changed=False, ips=self.get_ip_by_vmid(vmid)) |
| 149 |
|
| 150 |
elif self.params.get("ipam"): |
| 151 |
if ipam not in self.get_ipams(): |
| 152 |
self.module.fail_json(msg=f"IPAM {ipam} is not present") |
| 153 |
else: |
| 154 |
self.module.exit_json(changed=False, ipams=self.get_ipam_status()[ipam]) |
| 155 |
else: |
| 156 |
self.module.exit_json(changed=False, ipams=self.get_ipam_status()) |
| 157 |
|
| 158 |
def get_ipams(self): |
| 159 |
try: |
| 160 |
ipams = self.proxmox_api.cluster().sdn().ipams().get() |
| 161 |
return [ipam["ipam"] for ipam in ipams] |
| 162 |
except Exception as e: |
| 163 |
self.module.fail_json(msg=f"Failed to retrieve IPAM information {e}") |
| 164 |
|
| 165 |
def get_ipam_status(self): |
| 166 |
try: |
| 167 |
ipam_status = dict() |
| 168 |
ipams = self.get_ipams() |
| 169 |
for ipam_id in ipams: |
| 170 |
ipam_status[ipam_id] = self.proxmox_api.cluster().sdn().ipams(ipam_id).status().get() |
| 171 |
return ipam_status |
| 172 |
except Exception as e: |
| 173 |
self.module.fail_json(msg=f"Failed to retrieve IPAM status {e}") |
| 174 |
|
| 175 |
def get_ip_by_vmid(self, vmid): |
| 176 |
ipam_status = self.get_ipam_status() |
| 177 |
ips = [] |
| 178 |
for ipam in ipam_status.values(): |
| 179 |
for item in ipam: |
| 180 |
# vmid is always an integer |
| 181 |
# ipam["vmid"] (if present) is a string on PVE 8.4 and an integer on PVE 9.0 |
| 182 |
if str(item.get("vmid")) == str(vmid): |
| 183 |
ips.append(item) |
| 184 |
return ips |
| 185 |
|
| 186 |
|
| 187 |
def main(): |
| 188 |
module = create_proxmox_module(module_args(), **module_options()) |
| 189 |
proxmox = ProxmoxIpamInfoAnsible(module) |
| 190 |
|
| 191 |
try: |
| 192 |
proxmox.run() |
| 193 |
except Exception as e: |
| 194 |
module.fail_json(msg=f"An error occurred: {e}") |
| 195 |
|
| 196 |
|
| 197 |
if __name__ == "__main__": |
| 198 |
main() |