2020-06-29 13:41:30 +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 = """
|
2020-06-29 13:41:30 +00:00
|
|
|
---
|
2023-11-20 12:21:23 +00:00
|
|
|
module: load_balancer
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
short_description: Create and manage cloud Load Balancers on the Hetzner Cloud.
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage cloud Load Balancers on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@LKaemmerling)
|
|
|
|
version_added: 0.1.0
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud Load Balancer to manage.
|
|
|
|
- Only required if no Load Balancer I(name) is given
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud Load Balancer to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no Load Balancer I(id) is given or a Load Balancer does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
|
|
|
load_balancer_type:
|
|
|
|
description:
|
|
|
|
- The Load Balancer Type of the Hetzner Cloud Load Balancer to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if Load Balancer does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
2023-10-20 10:20:09 +00:00
|
|
|
algorithm:
|
|
|
|
description:
|
|
|
|
- Algorithm of the Load Balancer.
|
|
|
|
type: str
|
|
|
|
default: round_robin
|
|
|
|
choices: [round_robin, least_connections]
|
2020-06-29 13:41:30 +00:00
|
|
|
location:
|
|
|
|
description:
|
|
|
|
- Location of Load Balancer.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if no I(network_zone) is given and Load Balancer does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
|
|
|
network_zone:
|
|
|
|
description:
|
|
|
|
- Network Zone of Load Balancer.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required of no I(location) is given and Load Balancer does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs).
|
|
|
|
type: dict
|
|
|
|
disable_public_interface:
|
|
|
|
description:
|
|
|
|
- Disables the public interface.
|
|
|
|
type: bool
|
|
|
|
default: False
|
|
|
|
delete_protection:
|
|
|
|
description:
|
|
|
|
- Protect the Load Balancer for deletion.
|
|
|
|
type: bool
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the Load Balancer.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- hetzner.hcloud.hcloud
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic Load Balancer
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.load_balancer:
|
2020-06-29 13:41:30 +00:00
|
|
|
name: my-Load Balancer
|
|
|
|
load_balancer_type: lb11
|
2023-10-20 10:20:09 +00:00
|
|
|
algorithm: round_robin
|
2020-06-29 13:41:30 +00:00
|
|
|
location: fsn1
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the Load Balancer is absent (remove if needed)
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.load_balancer:
|
2020-06-29 13:41:30 +00:00
|
|
|
name: my-Load Balancer
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_load_balancer:
|
|
|
|
description: The Load Balancer instance
|
|
|
|
returned: Always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Numeric identifier of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 1937415
|
|
|
|
name:
|
|
|
|
description: Name of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: my-Load-Balancer
|
|
|
|
status:
|
|
|
|
description: Status of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: running
|
|
|
|
load_balancer_type:
|
|
|
|
description: Name of the Load Balancer type of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: cx11
|
2023-10-20 10:20:09 +00:00
|
|
|
algorithm:
|
|
|
|
description: Algorithm of the Load Balancer.
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
choices: [round_robin, least_connections]
|
|
|
|
sample: round_robin
|
2020-06-29 13:41:30 +00:00
|
|
|
ipv4_address:
|
|
|
|
description: Public IPv4 address of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: 116.203.104.109
|
|
|
|
ipv6_address:
|
|
|
|
description: Public IPv6 address of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: 2a01:4f8:1c1c:c140::1
|
|
|
|
location:
|
|
|
|
description: Name of the location of the Load Balancer
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: fsn1
|
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
delete_protection:
|
|
|
|
description: True if Load Balancer is protected for deletion
|
|
|
|
type: bool
|
|
|
|
returned: always
|
|
|
|
sample: false
|
|
|
|
disable_public_interface:
|
|
|
|
description: True if Load Balancer public interface is disabled
|
|
|
|
type: bool
|
|
|
|
returned: always
|
|
|
|
sample: false
|
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
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 HCloudException
|
2023-10-20 10:20:09 +00:00
|
|
|
from ..module_utils.vendor.hcloud.load_balancers import (
|
|
|
|
BoundLoadBalancer,
|
|
|
|
LoadBalancerAlgorithm,
|
|
|
|
)
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudLoadBalancer(AnsibleHCloud):
|
2023-09-26 07:41:01 +00:00
|
|
|
represent = "hcloud_load_balancer"
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
hcloud_load_balancer: BoundLoadBalancer | None = None
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
2024-03-27 13:11:30 +00:00
|
|
|
"id": str(self.hcloud_load_balancer.id),
|
|
|
|
"name": self.hcloud_load_balancer.name,
|
|
|
|
"ipv4_address": self.hcloud_load_balancer.public_net.ipv4.ip,
|
|
|
|
"ipv6_address": self.hcloud_load_balancer.public_net.ipv6.ip,
|
|
|
|
"private_ipv4_address": (
|
|
|
|
self.hcloud_load_balancer.private_net[0].ip if len(self.hcloud_load_balancer.private_net) else None
|
|
|
|
),
|
|
|
|
"load_balancer_type": self.hcloud_load_balancer.load_balancer_type.name,
|
|
|
|
"algorithm": self.hcloud_load_balancer.algorithm.type,
|
|
|
|
"location": self.hcloud_load_balancer.location.name,
|
2020-06-29 13:41:30 +00:00
|
|
|
"labels": self.hcloud_load_balancer.labels,
|
|
|
|
"delete_protection": self.hcloud_load_balancer.protection["delete"],
|
2024-03-27 13:11:30 +00:00
|
|
|
"disable_public_interface": not self.hcloud_load_balancer.public_net.enabled,
|
2020-06-29 13:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_load_balancer(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_load_balancer = self.client.load_balancers.get_by_id(self.module.params.get("id"))
|
2020-06-29 13:41:30 +00:00
|
|
|
else:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(self.module.params.get("name"))
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
def _create_load_balancer(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name", "load_balancer_type"])
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
2023-10-20 10:20:09 +00:00
|
|
|
"algorithm": LoadBalancerAlgorithm(type=self.module.params.get("algorithm", "round_robin")),
|
2021-02-24 12:37:58 +00:00
|
|
|
"load_balancer_type": self.client.load_balancer_types.get_by_name(
|
|
|
|
self.module.params.get("load_balancer_type")
|
|
|
|
),
|
|
|
|
"labels": self.module.params.get("labels"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.module.params.get("location") is None and self.module.params.get("network_zone") is None:
|
|
|
|
self.module.fail_json(msg="one of the following is required: location, network_zone")
|
|
|
|
elif self.module.params.get("location") is not None and self.module.params.get("network_zone") is None:
|
2023-06-27 09:50:13 +00:00
|
|
|
params["location"] = self.client.locations.get_by_name(self.module.params.get("location"))
|
2021-02-24 12:37:58 +00:00
|
|
|
elif self.module.params.get("location") is None and self.module.params.get("network_zone") is not None:
|
|
|
|
params["network_zone"] = self.module.params.get("network_zone")
|
2020-06-29 13:41:30 +00:00
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
resp = self.client.load_balancers.create(**params)
|
|
|
|
resp.action.wait_until_finished(max_retries=1000)
|
2020-10-01 09:09:51 +00:00
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
delete_protection = self.module.params.get("delete_protection")
|
|
|
|
if delete_protection is not None:
|
|
|
|
self._get_load_balancer()
|
|
|
|
self.hcloud_load_balancer.change_protection(delete=delete_protection).wait_until_finished()
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-06-29 13:41:30 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_load_balancer()
|
|
|
|
|
|
|
|
def _update_load_balancer(self):
|
|
|
|
try:
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and labels != self.hcloud_load_balancer.labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_load_balancer.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
delete_protection = self.module.params.get("delete_protection")
|
|
|
|
if delete_protection is not None and delete_protection != self.hcloud_load_balancer.protection["delete"]:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_load_balancer.change_protection(delete=delete_protection).wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_load_balancer()
|
|
|
|
|
|
|
|
disable_public_interface = self.module.params.get("disable_public_interface")
|
2023-06-27 09:50:13 +00:00
|
|
|
if disable_public_interface is not None and disable_public_interface != (
|
|
|
|
not self.hcloud_load_balancer.public_net.enabled
|
|
|
|
):
|
2020-06-29 13:41:30 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
if disable_public_interface is True:
|
|
|
|
self.hcloud_load_balancer.disable_public_interface().wait_until_finished()
|
|
|
|
else:
|
|
|
|
self.hcloud_load_balancer.enable_public_interface().wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
2020-08-10 07:58:30 +00:00
|
|
|
|
|
|
|
load_balancer_type = self.module.params.get("load_balancer_type")
|
2023-06-27 09:50:13 +00:00
|
|
|
if (
|
|
|
|
load_balancer_type is not None
|
|
|
|
and self.hcloud_load_balancer.load_balancer_type.name != load_balancer_type
|
|
|
|
):
|
2020-08-11 14:16:02 +00:00
|
|
|
new_load_balancer_type = self.client.load_balancer_types.get_by_name(load_balancer_type)
|
2020-08-10 07:58:30 +00:00
|
|
|
if not new_load_balancer_type:
|
|
|
|
self.module.fail_json(msg="unknown load balancer type")
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_load_balancer.change_type(
|
|
|
|
load_balancer_type=new_load_balancer_type,
|
|
|
|
).wait_until_finished(max_retries=1000)
|
|
|
|
|
|
|
|
self._mark_as_changed()
|
2023-10-20 10:20:09 +00:00
|
|
|
|
|
|
|
algorithm = self.module.params.get("algorithm")
|
|
|
|
if algorithm is not None and self.hcloud_load_balancer.algorithm.type != algorithm:
|
|
|
|
self.hcloud_load_balancer.change_algorithm(
|
|
|
|
algorithm=LoadBalancerAlgorithm(type=algorithm)
|
|
|
|
).wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
2020-06-29 13:41:30 +00:00
|
|
|
self._get_load_balancer()
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
def present_load_balancer(self):
|
|
|
|
self._get_load_balancer()
|
|
|
|
if self.hcloud_load_balancer is None:
|
|
|
|
self._create_load_balancer()
|
|
|
|
else:
|
|
|
|
self._update_load_balancer()
|
|
|
|
|
|
|
|
def delete_load_balancer(self):
|
|
|
|
try:
|
|
|
|
self._get_load_balancer()
|
|
|
|
if self.hcloud_load_balancer is not None:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.load_balancers.delete(self.hcloud_load_balancer)
|
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_load_balancer = None
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-06-29 13:41:30 +00:00
|
|
|
|
2023-08-02 10:05:00 +00:00
|
|
|
@classmethod
|
|
|
|
def define_module(cls):
|
2020-06-29 13:41:30 +00:00
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
|
|
|
load_balancer_type={"type": "str"},
|
2023-10-20 10:20:09 +00:00
|
|
|
algorithm={"choices": ["round_robin", "least_connections"], "default": "round_robin"},
|
2020-06-29 13:41:30 +00:00
|
|
|
location={"type": "str"},
|
|
|
|
network_zone={"type": "str"},
|
|
|
|
labels={"type": "dict"},
|
|
|
|
delete_protection={"type": "bool"},
|
2020-12-08 06:37:54 +00:00
|
|
|
disable_public_interface={"type": "bool", "default": False},
|
2020-06-29 13:41:30 +00:00
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-11-23 13:53:10 +00:00
|
|
|
**super().base_module_arguments(),
|
2020-06-29 13:41:30 +00:00
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
2020-06-29 13:41:30 +00:00
|
|
|
mutually_exclusive=[["location", "network_zone"]],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudLoadBalancer.define_module()
|
2020-06-29 13:41:30 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudLoadBalancer(module)
|
2020-06-29 13:41:30 +00:00
|
|
|
state = module.params.get("state")
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_load_balancer()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_load_balancer()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|