2020-03-09 13:36:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2019, 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-03-09 13:36:01 +00:00
|
|
|
---
|
2023-11-20 12:21:23 +00:00
|
|
|
module: floating_ip
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
short_description: Create and manage cloud Floating IPs on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage cloud Floating IPs on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
2020-06-29 13:41:30 +00:00
|
|
|
version_added: 0.1.0
|
2020-03-09 13:36:01 +00:00
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud Floating IPs to manage.
|
|
|
|
- Only required if no Floating IP I(name) is given.
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud Floating IPs to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no Floating IP I(id) is given or a Floating IP does not exist.
|
2020-03-09 13:36:01 +00:00
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The Description of the Hetzner Cloud Floating IPs.
|
|
|
|
type: str
|
|
|
|
home_location:
|
|
|
|
description:
|
|
|
|
- Home Location of the Hetzner Cloud Floating IP.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if no I(server) is given and Floating IP does not exist.
|
2020-03-09 13:36:01 +00:00
|
|
|
type: str
|
|
|
|
server:
|
|
|
|
description:
|
|
|
|
- Server Name the Floating IP should be assigned to.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if no I(home_location) is given and Floating IP does not exist.
|
2020-03-09 13:36:01 +00:00
|
|
|
type: str
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- Type of the Floating IP.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if Floating IP does not exist
|
2020-03-09 13:36:01 +00:00
|
|
|
choices: [ ipv4, ipv6 ]
|
|
|
|
type: str
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Force the assignment or deletion of the Floating IP.
|
|
|
|
type: bool
|
|
|
|
delete_protection:
|
|
|
|
description:
|
|
|
|
- Protect the Floating IP for deletion.
|
|
|
|
type: bool
|
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs).
|
|
|
|
type: dict
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the Floating IP.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- hetzner.hcloud.hcloud
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic IPv4 Floating IP
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.floating_ip:
|
2020-03-09 13:36:01 +00:00
|
|
|
name: my-floating-ip
|
|
|
|
home_location: fsn1
|
|
|
|
type: ipv4
|
|
|
|
state: present
|
|
|
|
- name: Create a basic IPv6 Floating IP
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.floating_ip:
|
2020-03-09 13:36:01 +00:00
|
|
|
name: my-floating-ip
|
|
|
|
home_location: fsn1
|
|
|
|
type: ipv6
|
|
|
|
state: present
|
|
|
|
- name: Assign a Floating IP to a server
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.floating_ip:
|
2020-03-09 13:36:01 +00:00
|
|
|
name: my-floating-ip
|
|
|
|
server: 1234
|
|
|
|
state: present
|
|
|
|
- name: Assign a Floating IP to another server
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.floating_ip:
|
2020-03-09 13:36:01 +00:00
|
|
|
name: my-floating-ip
|
|
|
|
server: 1234
|
2023-06-21 08:49:54 +00:00
|
|
|
force: true
|
2020-03-09 13:36:01 +00:00
|
|
|
state: present
|
|
|
|
- name: Floating IP should be absent
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.floating_ip:
|
2020-03-09 13:36:01 +00:00
|
|
|
name: my-floating-ip
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_floating_ip:
|
|
|
|
description: The Floating IP instance
|
|
|
|
returned: Always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: ID of the Floating IP
|
|
|
|
type: int
|
|
|
|
returned: Always
|
|
|
|
sample: 12345
|
|
|
|
name:
|
|
|
|
description: Name of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: my-floating-ip
|
|
|
|
description:
|
|
|
|
description: Description of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: my-floating-ip
|
|
|
|
ip:
|
|
|
|
description: IP Address of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: 116.203.104.109
|
|
|
|
type:
|
|
|
|
description: Type of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: ipv4
|
|
|
|
home_location:
|
|
|
|
description: Name of the home location of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: fsn1
|
|
|
|
server:
|
|
|
|
description: Name of the server the Floating IP is assigned to.
|
|
|
|
type: str
|
|
|
|
returned: Always
|
|
|
|
sample: "my-server"
|
|
|
|
delete_protection:
|
|
|
|
description: True if Floating IP is protected for deletion
|
|
|
|
type: bool
|
|
|
|
returned: always
|
|
|
|
sample: false
|
2020-06-29 13:41:30 +00:00
|
|
|
version_added: "0.1.0"
|
2020-03-09 13:36:01 +00:00
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
type: dict
|
|
|
|
returned: Always
|
|
|
|
sample:
|
|
|
|
key: value
|
|
|
|
mylabel: 123
|
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-06-27 11:17:21 +00:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
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-09-26 07:41:01 +00:00
|
|
|
from ..module_utils.vendor.hcloud.floating_ips import BoundFloatingIP
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudFloatingIP(AnsibleHCloud):
|
2023-09-26 07:41:01 +00:00
|
|
|
represent = "hcloud_floating_ip"
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
hcloud_floating_ip: BoundFloatingIP | None = None
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
server = None
|
|
|
|
|
|
|
|
if self.hcloud_floating_ip.server is not None:
|
|
|
|
server = to_native(self.hcloud_floating_ip.server.name)
|
|
|
|
return {
|
|
|
|
"id": to_native(self.hcloud_floating_ip.id),
|
|
|
|
"name": to_native(self.hcloud_floating_ip.name),
|
|
|
|
"description": to_native(self.hcloud_floating_ip.description),
|
|
|
|
"ip": to_native(self.hcloud_floating_ip.ip),
|
|
|
|
"type": to_native(self.hcloud_floating_ip.type),
|
|
|
|
"home_location": to_native(self.hcloud_floating_ip.home_location.name),
|
|
|
|
"labels": self.hcloud_floating_ip.labels,
|
|
|
|
"server": server,
|
|
|
|
"delete_protection": self.hcloud_floating_ip.protection["delete"],
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_floating_ip(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_floating_ip = self.client.floating_ips.get_by_id(self.module.params.get("id"))
|
2020-03-09 13:36:01 +00:00
|
|
|
else:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_floating_ip = self.client.floating_ips.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-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _create_floating_ip(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["type"])
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
params = {
|
|
|
|
"description": self.module.params.get("description"),
|
|
|
|
"type": self.module.params.get("type"),
|
|
|
|
"name": self.module.params.get("name"),
|
|
|
|
}
|
|
|
|
if self.module.params.get("home_location") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
params["home_location"] = self.client.locations.get_by_name(self.module.params.get("home_location"))
|
2021-02-24 12:37:58 +00:00
|
|
|
elif self.module.params.get("server") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
params["server"] = self.client.servers.get_by_name(self.module.params.get("server"))
|
2021-02-24 12:37:58 +00:00
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="one of the following is required: home_location, server")
|
2020-05-06 05:44:05 +00:00
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
if self.module.params.get("labels") is not None:
|
|
|
|
params["labels"] = self.module.params.get("labels")
|
|
|
|
if not self.module.check_mode:
|
|
|
|
resp = self.client.floating_ips.create(**params)
|
|
|
|
self.hcloud_floating_ip = resp.floating_ip
|
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.hcloud_floating_ip.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-03-09 13:36:01 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_floating_ip()
|
|
|
|
|
|
|
|
def _update_floating_ip(self):
|
|
|
|
try:
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and labels != self.hcloud_floating_ip.labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_floating_ip.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
description = self.module.params.get("description")
|
|
|
|
if description is not None and description != self.hcloud_floating_ip.description:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_floating_ip.update(description=description)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
server = self.module.params.get("server")
|
2020-10-05 09:09:00 +00:00
|
|
|
if server is not None and self.hcloud_floating_ip.server is not None:
|
|
|
|
if self.module.params.get("force") and server != self.hcloud_floating_ip.server.name:
|
2020-03-09 13:36:01 +00:00
|
|
|
if not self.module.check_mode:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_floating_ip.assign(self.client.servers.get_by_name(server))
|
2020-10-05 09:09:00 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
elif server != self.hcloud_floating_ip.server.name:
|
2020-03-09 13:36:01 +00:00
|
|
|
self.module.warn(
|
2023-08-24 09:27:40 +00:00
|
|
|
"Floating IP is already assigned to another server "
|
|
|
|
f"{self.hcloud_floating_ip.server.name}. You need to "
|
|
|
|
"unassign the Floating IP or use force=true."
|
2020-03-09 13:36:01 +00:00
|
|
|
)
|
2020-10-05 09:09:00 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
elif server is not None and self.hcloud_floating_ip.server is None:
|
|
|
|
if not self.module.check_mode:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_floating_ip.assign(self.client.servers.get_by_name(server))
|
2020-03-09 13:36:01 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
elif server is None and self.hcloud_floating_ip.server is not None:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_floating_ip.unassign()
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
delete_protection = self.module.params.get("delete_protection")
|
|
|
|
if delete_protection is not None and delete_protection != self.hcloud_floating_ip.protection["delete"]:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_floating_ip.change_protection(delete=delete_protection).wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
self._get_floating_ip()
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def present_floating_ip(self):
|
|
|
|
self._get_floating_ip()
|
|
|
|
if self.hcloud_floating_ip is None:
|
|
|
|
self._create_floating_ip()
|
|
|
|
else:
|
|
|
|
self._update_floating_ip()
|
|
|
|
|
|
|
|
def delete_floating_ip(self):
|
|
|
|
try:
|
|
|
|
self._get_floating_ip()
|
|
|
|
if self.hcloud_floating_ip is not None:
|
|
|
|
if self.module.params.get("force") or self.hcloud_floating_ip.server is None:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.floating_ips.delete(self.hcloud_floating_ip)
|
|
|
|
else:
|
|
|
|
self.module.warn(
|
2023-08-24 09:27:40 +00:00
|
|
|
"Floating IP is currently assigned to server "
|
|
|
|
f"{self.hcloud_floating_ip.server.name}. You need to "
|
|
|
|
"unassign the Floating IP or use force=true."
|
2020-03-09 13:36:01 +00:00
|
|
|
)
|
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_floating_ip = None
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-02 10:05:00 +00:00
|
|
|
@classmethod
|
|
|
|
def define_module(cls):
|
2020-03-09 13:36:01 +00:00
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
|
|
|
description={"type": "str"},
|
|
|
|
server={"type": "str"},
|
|
|
|
home_location={"type": "str"},
|
|
|
|
force={"type": "bool"},
|
|
|
|
type={"choices": ["ipv4", "ipv6"]},
|
|
|
|
labels={"type": "dict"},
|
|
|
|
delete_protection={"type": "bool"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-08-24 09:27:40 +00:00
|
|
|
**super().base_module_arguments(),
|
2020-03-09 13:36:01 +00:00
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
|
|
|
mutually_exclusive=[["home_location", "server"]],
|
2020-03-09 13:36:01 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudFloatingIP.define_module()
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudFloatingIP(module)
|
2020-03-09 13:36:01 +00:00
|
|
|
state = module.params["state"]
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_floating_ip()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_floating_ip()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|