2021-03-11 10:07:41 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2020, Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = """
|
2021-03-11 10:07:41 +00:00
|
|
|
---
|
2023-11-20 12:21:23 +00:00
|
|
|
module: firewall
|
2021-03-11 10:07:41 +00:00
|
|
|
short_description: Create and manage firewalls on the Hetzner Cloud.
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage firewalls on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- The ID of the Hetzner Cloud Firewall to manage.
|
|
|
|
- Only required if no firewall O(name) is given.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- The Name of the Hetzner Cloud Firewall to manage.
|
|
|
|
- Only required if no firewall O(id) is given, or the firewall does not exist.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- User-defined labels (key-value pairs).
|
2021-03-11 10:07:41 +00:00
|
|
|
type: dict
|
|
|
|
rules:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- List of rules the firewall contain.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
suboptions:
|
2023-12-21 16:47:56 +00:00
|
|
|
description:
|
2021-03-11 10:07:41 +00:00
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- User defined description of this rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
direction:
|
2021-03-11 10:07:41 +00:00
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- The direction of the firewall rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
choices: [in, out]
|
2021-03-11 10:07:41 +00:00
|
|
|
protocol:
|
|
|
|
description:
|
|
|
|
- The protocol of the firewall rule.
|
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
choices: [icmp, tcp, udp, esp, gre]
|
|
|
|
port:
|
|
|
|
description:
|
|
|
|
- The port or port range allowed by this rule.
|
|
|
|
- A port range can be specified by separating two ports with a dash, e.g 1024-5000.
|
|
|
|
- Only used if O(rules[].protocol=tcp) or O(rules[].protocol=udp).
|
|
|
|
type: str
|
2021-03-11 10:07:41 +00:00
|
|
|
source_ips:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- List of CIDRs that are allowed within this rule.
|
|
|
|
- Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses.
|
|
|
|
- Only used if O(rules[].direction=in).
|
2021-03-11 10:07:41 +00:00
|
|
|
type: list
|
|
|
|
elements: str
|
2023-12-21 16:47:56 +00:00
|
|
|
default: []
|
2021-03-11 10:07:41 +00:00
|
|
|
destination_ips:
|
|
|
|
description:
|
2023-12-21 16:47:56 +00:00
|
|
|
- List of CIDRs that are allowed within this rule.
|
|
|
|
- Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses.
|
|
|
|
- Only used if O(rules[].direction=out).
|
2021-03-11 10:07:41 +00:00
|
|
|
type: list
|
|
|
|
elements: str
|
2023-12-21 16:47:56 +00:00
|
|
|
default: []
|
2024-02-02 08:48:56 +00:00
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Force the deletion of the Firewall when still in use.
|
|
|
|
type: bool
|
|
|
|
default: false
|
2021-03-11 10:07:41 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the firewall.
|
|
|
|
default: present
|
2023-12-21 16:47:56 +00:00
|
|
|
choices: [absent, present]
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
extends_documentation_fragment:
|
2023-12-21 16:47:56 +00:00
|
|
|
- hetzner.hcloud.hcloud
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic firewall
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Create a firewall with rules
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
rules:
|
2023-12-21 16:47:56 +00:00
|
|
|
- description: allow icmp from everywhere
|
|
|
|
direction: in
|
2023-10-19 10:41:44 +00:00
|
|
|
protocol: icmp
|
|
|
|
source_ips:
|
|
|
|
- 0.0.0.0/0
|
|
|
|
- ::/0
|
2021-03-11 10:07:41 +00:00
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Create a firewall with labels
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
labels:
|
2023-10-19 10:41:44 +00:00
|
|
|
key: value
|
|
|
|
mylabel: 123
|
2021-03-11 10:07:41 +00:00
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the firewall is absent (remove if needed)
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_firewall:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: The firewall instance.
|
|
|
|
returned: always
|
|
|
|
type: dict
|
2021-03-11 10:07:41 +00:00
|
|
|
contains:
|
|
|
|
id:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: Numeric identifier of the firewall.
|
2021-03-11 10:07:41 +00:00
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 1937415
|
|
|
|
name:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: Name of the firewall.
|
2021-03-11 10:07:41 +00:00
|
|
|
returned: always
|
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
sample: my-firewall
|
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs).
|
|
|
|
returned: always
|
|
|
|
type: dict
|
2021-03-11 10:07:41 +00:00
|
|
|
rules:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: List of rules the firewall contain.
|
2021-03-11 10:07:41 +00:00
|
|
|
returned: always
|
2023-12-21 16:47:56 +00:00
|
|
|
type: list
|
|
|
|
elements: dict
|
2021-03-11 10:07:41 +00:00
|
|
|
contains:
|
2023-12-21 16:47:56 +00:00
|
|
|
description:
|
|
|
|
description: User defined description of this rule.
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: allow http from anywhere
|
2021-03-11 10:07:41 +00:00
|
|
|
direction:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: The direction of the firewall rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: in
|
|
|
|
protocol:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: The protocol of the firewall rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
|
|
|
returned: always
|
2023-12-21 16:47:56 +00:00
|
|
|
sample: tcp
|
2021-03-11 10:07:41 +00:00
|
|
|
port:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: The port or port range allowed by this rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
2023-12-21 16:47:56 +00:00
|
|
|
returned: if RV(hcloud_firewall.rules[].protocol=tcp) or RV(hcloud_firewall.rules[].protocol=udp)
|
|
|
|
sample: "80"
|
2021-03-11 10:07:41 +00:00
|
|
|
source_ips:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: List of source CIDRs that are allowed within this rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
returned: always
|
2023-12-21 16:47:56 +00:00
|
|
|
sample: ["0.0.0.0/0", "::/0"]
|
2021-03-11 10:07:41 +00:00
|
|
|
destination_ips:
|
2023-12-21 16:47:56 +00:00
|
|
|
description: List of destination CIDRs that are allowed within this rule.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
returned: always
|
2023-12-21 16:47:56 +00:00
|
|
|
sample: []
|
2024-02-01 15:50:13 +00:00
|
|
|
applied_to:
|
|
|
|
description: List of Resources the Firewall is applied to.
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
contains:
|
|
|
|
type:
|
|
|
|
description: Type of the resource.
|
|
|
|
type: str
|
|
|
|
choices: [server, label_selector]
|
|
|
|
sample: label_selector
|
|
|
|
server:
|
|
|
|
description: ID of the server.
|
|
|
|
type: int
|
|
|
|
sample: 12345
|
|
|
|
label_selector:
|
|
|
|
description: Label selector value.
|
|
|
|
type: str
|
|
|
|
sample: env=prod
|
|
|
|
applied_to_resources:
|
|
|
|
description: List of Resources the Firewall label selector is applied to.
|
|
|
|
returned: if RV(hcloud_firewall.applied_to[].type=label_selector)
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
contains:
|
|
|
|
type:
|
|
|
|
description: Type of resource referenced.
|
|
|
|
type: str
|
|
|
|
choices: [server]
|
|
|
|
sample: server
|
|
|
|
server:
|
|
|
|
description: ID of the Server.
|
|
|
|
type: int
|
|
|
|
sample: 12345
|
2021-03-11 10:07:41 +00:00
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-07-31 08:12:55 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
from ..module_utils.hcloud import AnsibleHCloud
|
2023-07-31 08:12:55 +00:00
|
|
|
from ..module_utils.vendor.hcloud import APIException, HCloudException
|
2024-02-01 15:50:13 +00:00
|
|
|
from ..module_utils.vendor.hcloud.firewalls import (
|
|
|
|
BoundFirewall,
|
|
|
|
FirewallResource,
|
|
|
|
FirewallRule,
|
|
|
|
)
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudFirewall(AnsibleHCloud):
|
2023-09-26 07:41:01 +00:00
|
|
|
represent = "hcloud_firewall"
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
hcloud_firewall: BoundFirewall | None = None
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
2024-03-27 13:11:30 +00:00
|
|
|
"id": str(self.hcloud_firewall.id),
|
|
|
|
"name": self.hcloud_firewall.name,
|
2021-03-11 10:07:41 +00:00
|
|
|
"rules": [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules],
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.hcloud_firewall.labels,
|
2024-02-01 15:50:13 +00:00
|
|
|
"applied_to": [self._prepare_result_applied_to(resource) for resource in self.hcloud_firewall.applied_to],
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:50:13 +00:00
|
|
|
def _prepare_result_rule(self, rule: FirewallRule):
|
2021-03-11 10:07:41 +00:00
|
|
|
return {
|
2024-03-27 13:11:30 +00:00
|
|
|
"direction": rule.direction,
|
|
|
|
"protocol": rule.protocol,
|
|
|
|
"port": rule.port,
|
|
|
|
"source_ips": rule.source_ips,
|
|
|
|
"destination_ips": rule.destination_ips,
|
|
|
|
"description": rule.description,
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:50:13 +00:00
|
|
|
def _prepare_result_applied_to(self, resource: FirewallResource):
|
|
|
|
result = {
|
2024-03-27 13:11:30 +00:00
|
|
|
"type": resource.type,
|
|
|
|
"server": str(resource.server.id) if resource.server is not None else None,
|
|
|
|
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None,
|
2024-02-01 15:50:13 +00:00
|
|
|
}
|
|
|
|
if resource.applied_to_resources is not None:
|
|
|
|
result["applied_to_resources"] = [
|
|
|
|
{
|
2024-03-27 13:11:30 +00:00
|
|
|
"type": item.type,
|
|
|
|
"server": str(item.server.id) if item.server is not None else None,
|
2024-02-01 15:50:13 +00:00
|
|
|
}
|
|
|
|
for item in resource.applied_to_resources
|
|
|
|
]
|
|
|
|
return result
|
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
def _get_firewall(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_firewall = self.client.firewalls.get_by_id(self.module.params.get("id"))
|
2021-03-11 10:07:41 +00:00
|
|
|
elif self.module.params.get("name") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_firewall = self.client.firewalls.get_by_name(self.module.params.get("name"))
|
2021-03-11 10:07:41 +00:00
|
|
|
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
def _create_firewall(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name"])
|
2021-03-11 10:07:41 +00:00
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.module.params.get("labels"),
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
rules = self.module.params.get("rules")
|
|
|
|
if rules is not None:
|
|
|
|
params["rules"] = [
|
|
|
|
FirewallRule(
|
|
|
|
direction=rule["direction"],
|
|
|
|
protocol=rule["protocol"],
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [],
|
|
|
|
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [],
|
2021-08-12 11:13:19 +00:00
|
|
|
port=rule["port"],
|
|
|
|
description=rule["description"],
|
2021-03-11 10:07:41 +00:00
|
|
|
)
|
|
|
|
for rule in rules
|
|
|
|
]
|
2024-02-01 15:50:13 +00:00
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
try:
|
|
|
|
self.client.firewalls.create(**params)
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception, params=params)
|
2024-02-01 15:50:13 +00:00
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_firewall()
|
|
|
|
|
|
|
|
def _update_firewall(self):
|
|
|
|
name = self.module.params.get("name")
|
|
|
|
if name is not None and self.hcloud_firewall.name != name:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["id"])
|
2021-03-11 10:07:41 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_firewall.update(name=name)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and self.hcloud_firewall.labels != labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_firewall.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
rules = self.module.params.get("rules")
|
2021-04-06 09:28:38 +00:00
|
|
|
if rules is not None and rules != [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules]:
|
2021-03-11 10:07:41 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
new_rules = [
|
|
|
|
FirewallRule(
|
|
|
|
direction=rule["direction"],
|
|
|
|
protocol=rule["protocol"],
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [],
|
|
|
|
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [],
|
2021-08-12 11:13:19 +00:00
|
|
|
port=rule["port"],
|
|
|
|
description=rule["description"],
|
2021-03-11 10:07:41 +00:00
|
|
|
)
|
|
|
|
for rule in rules
|
|
|
|
]
|
|
|
|
self.hcloud_firewall.set_rules(new_rules)
|
|
|
|
self._mark_as_changed()
|
2024-02-01 15:50:13 +00:00
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
self._get_firewall()
|
|
|
|
|
|
|
|
def present_firewall(self):
|
|
|
|
self._get_firewall()
|
|
|
|
if self.hcloud_firewall is None:
|
|
|
|
self._create_firewall()
|
|
|
|
else:
|
|
|
|
self._update_firewall()
|
|
|
|
|
|
|
|
def delete_firewall(self):
|
|
|
|
self._get_firewall()
|
|
|
|
if self.hcloud_firewall is not None:
|
|
|
|
if not self.module.check_mode:
|
2024-02-02 08:48:56 +00:00
|
|
|
if self.hcloud_firewall.applied_to:
|
|
|
|
if self.module.params.get("force"):
|
|
|
|
actions = self.hcloud_firewall.remove_from_resources(self.hcloud_firewall.applied_to)
|
|
|
|
for action in actions:
|
|
|
|
action.wait_until_finished()
|
|
|
|
else:
|
|
|
|
self.module.warn(
|
|
|
|
f"Firewall {self.hcloud_firewall.name} is currently used by "
|
|
|
|
"other resources. You need to unassign the resources before "
|
|
|
|
"deleting the Firewall or use force=true."
|
|
|
|
)
|
|
|
|
|
2022-11-08 13:05:13 +00:00
|
|
|
retry_count = 0
|
2024-02-02 08:48:56 +00:00
|
|
|
while True:
|
2022-11-08 13:05:13 +00:00
|
|
|
try:
|
2024-02-02 08:48:56 +00:00
|
|
|
self.hcloud_firewall.delete()
|
2022-11-08 13:05:13 +00:00
|
|
|
break
|
2023-08-25 14:19:15 +00:00
|
|
|
except APIException as exception:
|
2024-02-02 08:48:56 +00:00
|
|
|
if "is still in use" in exception.message and retry_count < 10:
|
|
|
|
retry_count += 1
|
2022-11-08 13:05:13 +00:00
|
|
|
time.sleep(0.5 * retry_count)
|
2024-02-02 08:48:56 +00:00
|
|
|
continue
|
|
|
|
self.fail_json_hcloud(exception)
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2024-02-02 08:48:56 +00:00
|
|
|
|
2021-03-11 10:07:41 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_firewall = None
|
|
|
|
|
2023-08-02 10:05:00 +00:00
|
|
|
@classmethod
|
|
|
|
def define_module(cls):
|
2021-03-11 10:07:41 +00:00
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
2023-12-21 16:47:56 +00:00
|
|
|
labels={"type": "dict"},
|
2021-03-11 10:07:41 +00:00
|
|
|
rules=dict(
|
|
|
|
type="list",
|
|
|
|
elements="dict",
|
|
|
|
options=dict(
|
2023-12-21 16:47:56 +00:00
|
|
|
description={"type": "str"},
|
2021-03-11 10:07:41 +00:00
|
|
|
direction={"type": "str", "choices": ["in", "out"]},
|
2022-11-07 12:58:43 +00:00
|
|
|
protocol={"type": "str", "choices": ["icmp", "udp", "tcp", "esp", "gre"]},
|
2021-03-11 10:07:41 +00:00
|
|
|
port={"type": "str"},
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips={"type": "list", "elements": "str", "default": []},
|
2021-04-06 09:28:38 +00:00
|
|
|
destination_ips={"type": "list", "elements": "str", "default": []},
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
2021-05-27 06:13:57 +00:00
|
|
|
required_together=[["direction", "protocol"]],
|
2023-10-04 08:59:50 +00:00
|
|
|
required_if=[
|
|
|
|
["protocol", "udp", ["port"]],
|
|
|
|
["protocol", "tcp", ["port"]],
|
|
|
|
],
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
2024-02-02 08:48:56 +00:00
|
|
|
force={"type": "bool", "default": False},
|
2021-03-11 10:07:41 +00:00
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-11-23 13:53:10 +00:00
|
|
|
**super().base_module_arguments(),
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
|
|
|
required_if=[["state", "present", ["name"]]],
|
2021-03-11 10:07:41 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudFirewall.define_module()
|
2021-03-11 10:07:41 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudFirewall(module)
|
2021-03-11 10:07:41 +00:00
|
|
|
state = module.params.get("state")
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_firewall()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_firewall()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|