| 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_firewall_info |
| 10 |
short_description: Manage firewall rules in Proxmox |
| 11 |
version_added: "1.4.0" |
| 12 |
description: |
| 13 |
- Get firewall rules at cluster/group/vnet/node/vm level. |
| 14 |
- Get firewall security groups at cluster level. |
| 15 |
- Get aliases at cluster/VM level. |
| 16 |
author: 'Jana Hoch <[email protected]> (!UNKNOWN)' |
| 17 |
options: |
| 18 |
level: |
| 19 |
description: |
| 20 |
- Level at which the firewall rule applies. |
| 21 |
type: str |
| 22 |
choices: |
| 23 |
- cluster |
| 24 |
- group |
| 25 |
- vnet |
| 26 |
- node |
| 27 |
- vm |
| 28 |
default: cluster |
| 29 |
node: |
| 30 |
description: |
| 31 |
- Name of the node. |
| 32 |
- Only needed when O(level=node). |
| 33 |
type: str |
| 34 |
vmid: |
| 35 |
description: |
| 36 |
- ID of the VM to which the rule applies. |
| 37 |
- Only needed when O(level=vm). |
| 38 |
type: int |
| 39 |
vnet: |
| 40 |
description: |
| 41 |
- Name of the virtual network for the rule. |
| 42 |
- Only needed when O(level=vnet). |
| 43 |
type: str |
| 44 |
pos: |
| 45 |
description: |
| 46 |
- Position of the rule in the list. |
| 47 |
type: int |
| 48 |
group: |
| 49 |
description: |
| 50 |
- Name of the group to which the rule belongs. |
| 51 |
- Only needed when O(level=group). |
| 52 |
type: str |
| 53 |
extends_documentation_fragment: |
| 54 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 55 |
- community.proxmox.proxmox.documentation |
| 56 |
- community.proxmox.attributes |
| 57 |
- community.proxmox.attributes.info_module |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Get Cluster level firewall rules, aliases, and security groups |
| 62 |
community.proxmox.proxmox_firewall_info: |
| 63 |
level: cluster |
| 64 |
""" |
| 65 |
|
| 66 |
RETURN = r""" |
| 67 |
groups: |
| 68 |
description: |
| 69 |
- List of firewall security groups. |
| 70 |
- This will always be given for cluster level regardless of the level passed. |
| 71 |
- Because only at cluster level we can have firewall security groups. |
| 72 |
returned: on success |
| 73 |
type: list |
| 74 |
elements: str |
| 75 |
sample: |
| 76 |
[ "test" ] |
| 77 |
|
| 78 |
ip_sets: |
| 79 |
description: |
| 80 |
- List of IP Sets. |
| 81 |
- These are only supported on the O(level) = cluster, other inputs are ignored. |
| 82 |
returned: on success |
| 83 |
type: list |
| 84 |
elements: dict |
| 85 |
sample: |
| 86 |
[ |
| 87 |
{ |
| 88 |
"cidrs": [], |
| 89 |
"digest": "8c4a5e793355b2a877659315faaa78cbd0bc9f6c", |
| 90 |
"name": "emptytest" |
| 91 |
}, |
| 92 |
{ |
| 93 |
"cidrs": [ |
| 94 |
{ |
| 95 |
"cidr": "192.168.1.10", |
| 96 |
"comment": "Proxmox pve-01", |
| 97 |
"digest": "ed830373d096f6b9f868e59c6182e0b2042e6bad", |
| 98 |
"nomatch": false |
| 99 |
}, |
| 100 |
{ |
| 101 |
"cidr": "192.168.1.11", |
| 102 |
"comment": "Proxmox pve-02", |
| 103 |
"digest": "ed830373d096f6b9f868e59c6182e0b2042e6bad", |
| 104 |
"nomatch": true |
| 105 |
} |
| 106 |
], |
| 107 |
"comment": "PVE hosts", |
| 108 |
"digest": "8c4a5e793355b2a877659315faaa78cbd0bc9f6c", |
| 109 |
"name": "hypervisors" |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cidrs": [ |
| 113 |
{ |
| 114 |
"cidr": "10.10.1.0", |
| 115 |
"comment": "Proxmox pve-01", |
| 116 |
"digest": "f15cef67632375227d849ee0a449d845ab136677", |
| 117 |
"nomatch": false |
| 118 |
} |
| 119 |
], |
| 120 |
"comment": "PVE hosts", |
| 121 |
"digest": "8c4a5e793355b2a877659315faaa78cbd0bc9f6c", |
| 122 |
"name": "test" |
| 123 |
} |
| 124 |
] |
| 125 |
|
| 126 |
aliases: |
| 127 |
description: |
| 128 |
- List of alias present at given level. |
| 129 |
- Aliases are only available for cluster and VM level so if any other level it'll be empty list. |
| 130 |
returned: on success |
| 131 |
type: list |
| 132 |
elements: dict |
| 133 |
sample: |
| 134 |
[ |
| 135 |
{ |
| 136 |
"cidr": "10.10.1.0/24", |
| 137 |
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f", |
| 138 |
"ipversion": 4, |
| 139 |
"name": "test1" |
| 140 |
}, |
| 141 |
{ |
| 142 |
"cidr": "10.10.2.0/24", |
| 143 |
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f", |
| 144 |
"ipversion": 4, |
| 145 |
"name": "test2" |
| 146 |
}, |
| 147 |
{ |
| 148 |
"cidr": "10.10.3.0/24", |
| 149 |
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f", |
| 150 |
"ipversion": 4, |
| 151 |
"name": "test3" |
| 152 |
} |
| 153 |
] |
| 154 |
|
| 155 |
firewall_rules: |
| 156 |
description: List of firewall rules at given level. |
| 157 |
returned: on success |
| 158 |
type: list |
| 159 |
elements: dict |
| 160 |
sample: |
| 161 |
[ |
| 162 |
{ |
| 163 |
"action": "ACCEPT", |
| 164 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 165 |
"dport": "53", |
| 166 |
"enable": 1, |
| 167 |
"ipversion": 4, |
| 168 |
"log": "nolog", |
| 169 |
"pos": 0, |
| 170 |
"proto": "udp", |
| 171 |
"source": "192.168.1.0/24", |
| 172 |
"type": "in" |
| 173 |
}, |
| 174 |
{ |
| 175 |
"action": "ACCEPT", |
| 176 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 177 |
"dport": "53", |
| 178 |
"enable": 1, |
| 179 |
"ipversion": 4, |
| 180 |
"log": "nolog", |
| 181 |
"pos": 1, |
| 182 |
"proto": "tcp", |
| 183 |
"source": "192.168.1.0/24", |
| 184 |
"type": "in" |
| 185 |
}, |
| 186 |
{ |
| 187 |
"action": "ACCEPT", |
| 188 |
"dest": "192.168.1.0/24", |
| 189 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 190 |
"enable": 1, |
| 191 |
"ipversion": 4, |
| 192 |
"log": "nolog", |
| 193 |
"pos": 2, |
| 194 |
"type": "out" |
| 195 |
}, |
| 196 |
{ |
| 197 |
"action": "ACCEPT", |
| 198 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 199 |
"enable": 1, |
| 200 |
"ipversion": 4, |
| 201 |
"log": "nolog", |
| 202 |
"pos": 3, |
| 203 |
"source": "192.168.1.0/24", |
| 204 |
"type": "in" |
| 205 |
}, |
| 206 |
{ |
| 207 |
"action": "ACCEPT", |
| 208 |
"dest": "+sdn/test2-gateway", |
| 209 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 210 |
"enable": 1, |
| 211 |
"iface": "test2", |
| 212 |
"log": "nolog", |
| 213 |
"macro": "DNS", |
| 214 |
"pos": 4, |
| 215 |
"type": "in" |
| 216 |
}, |
| 217 |
{ |
| 218 |
"action": "ACCEPT", |
| 219 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 220 |
"enable": 1, |
| 221 |
"iface": "test2", |
| 222 |
"log": "nolog", |
| 223 |
"macro": "DHCPfwd", |
| 224 |
"pos": 5, |
| 225 |
"type": "in" |
| 226 |
}, |
| 227 |
{ |
| 228 |
"action": "ACCEPT", |
| 229 |
"dest": "+sdn/test2-all", |
| 230 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 231 |
"dport": "68", |
| 232 |
"enable": 1, |
| 233 |
"log": "nolog", |
| 234 |
"pos": 6, |
| 235 |
"proto": "udp", |
| 236 |
"source": "+sdn/test2-gateway", |
| 237 |
"sport": "67", |
| 238 |
"type": "out" |
| 239 |
}, |
| 240 |
{ |
| 241 |
"action": "DROP", |
| 242 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 243 |
"enable": 1, |
| 244 |
"log": "nolog", |
| 245 |
"pos": 7, |
| 246 |
"type": "in" |
| 247 |
}, |
| 248 |
{ |
| 249 |
"action": "DROP", |
| 250 |
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9", |
| 251 |
"enable": 1, |
| 252 |
"log": "nolog", |
| 253 |
"pos": 8, |
| 254 |
"type": "out" |
| 255 |
} |
| 256 |
] |
| 257 |
""" |
| 258 |
|
| 259 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import create_proxmox_module |
| 260 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_sdn import ProxmoxSdnAnsible |
| 261 |
|
| 262 |
|
| 263 |
def module_args(): |
| 264 |
return dict( |
| 265 |
level=dict(type="str", choices=["cluster", "node", "vm", "vnet", "group"], default="cluster", required=False), |
| 266 |
node=dict(type="str", required=False), |
| 267 |
vmid=dict(type="int", required=False), |
| 268 |
vnet=dict(type="str", required=False), |
| 269 |
group=dict(type="str", required=False), |
| 270 |
pos=dict(type="int", required=False), |
| 271 |
) |
| 272 |
|
| 273 |
|
| 274 |
def module_options(): |
| 275 |
return dict( |
| 276 |
required_if=[ |
| 277 |
("level", "vm", ["vmid"]), |
| 278 |
("level", "node", ["node"]), |
| 279 |
("level", "vnet", ["vnet"]), |
| 280 |
("level", "group", ["group"]), |
| 281 |
], |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
class ProxmoxFirewallInfoAnsible(ProxmoxSdnAnsible): |
| 286 |
def __init__(self, module): |
| 287 |
super().__init__(module) |
| 288 |
self.params = module.params |
| 289 |
|
| 290 |
def run(self): |
| 291 |
level = self.params.get("level") |
| 292 |
|
| 293 |
if level == "vm": |
| 294 |
vm = self.get_vm(vmid=self.params.get("vmid")) |
| 295 |
node = self.proxmox_api.nodes(vm["node"]) |
| 296 |
virt = node(vm["type"]) |
| 297 |
firewall_obj = virt(str(vm["vmid"])).firewall |
| 298 |
rules_obj = firewall_obj().rules |
| 299 |
|
| 300 |
elif level == "node": |
| 301 |
firewall_obj = self.proxmox_api.nodes(self.params.get("node")).firewall |
| 302 |
rules_obj = firewall_obj().rules |
| 303 |
|
| 304 |
elif level == "vnet": |
| 305 |
firewall_obj = self.proxmox_api.cluster().sdn().vnets(self.params.get("vnet")).firewall |
| 306 |
rules_obj = firewall_obj().rules |
| 307 |
|
| 308 |
elif level == "group": |
| 309 |
firewall_obj = None |
| 310 |
rules_obj = self.proxmox_api.cluster().firewall().groups(self.params.get("group")) |
| 311 |
|
| 312 |
else: |
| 313 |
firewall_obj = self.proxmox_api.cluster().firewall |
| 314 |
rules_obj = firewall_obj().rules |
| 315 |
|
| 316 |
rules = self.get_fw_rules(rules_obj, pos=self.params.get("pos")) |
| 317 |
groups = self.get_groups() |
| 318 |
aliases = self.get_aliases(firewall_obj=firewall_obj) |
| 319 |
ip_sets = self.get_ip_sets(firewall_obj=firewall_obj) |
| 320 |
self.module.exit_json( |
| 321 |
changed=False, |
| 322 |
firewall_rules=rules, |
| 323 |
groups=groups, |
| 324 |
aliases=aliases, |
| 325 |
ip_sets=ip_sets, |
| 326 |
msg="successfully retrieved firewall rules and groups", |
| 327 |
) |
| 328 |
|
| 329 |
|
| 330 |
def main(): |
| 331 |
module = create_proxmox_module(module_args(), **module_options()) |
| 332 |
proxmox = ProxmoxFirewallInfoAnsible(module) |
| 333 |
|
| 334 |
try: |
| 335 |
proxmox.run() |
| 336 |
except Exception as e: |
| 337 |
module.fail_json(msg=f"An error occurred: {e}") |
| 338 |
|
| 339 |
|
| 340 |
if __name__ == "__main__": |
| 341 |
main() |