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-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = """
|
2020-03-09 13:36:01 +00:00
|
|
|
---
|
|
|
|
module: hcloud_rdns
|
|
|
|
|
|
|
|
short_description: Create and manage reverse DNS entries on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and delete reverse DNS entries on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
options:
|
|
|
|
server:
|
|
|
|
description:
|
|
|
|
- The name of the Hetzner Cloud server you want to add the reverse DNS entry to.
|
|
|
|
type: str
|
2020-09-08 06:13:10 +00:00
|
|
|
floating_ip:
|
|
|
|
description:
|
|
|
|
- The name of the Hetzner Cloud Floating IP you want to add the reverse DNS entry to.
|
|
|
|
type: str
|
2022-06-29 07:58:01 +00:00
|
|
|
primary_ip:
|
|
|
|
description:
|
|
|
|
- The name of the Hetzner Cloud Primary IP you want to add the reverse DNS entry to.
|
|
|
|
type: str
|
2021-08-17 12:48:12 +00:00
|
|
|
load_balancer:
|
|
|
|
description:
|
|
|
|
- The name of the Hetzner Cloud Load Balancer you want to add the reverse DNS entry to.
|
|
|
|
type: str
|
2020-03-09 13:36:01 +00:00
|
|
|
ip_address:
|
|
|
|
description:
|
|
|
|
- The IP address that should point to I(dns_ptr).
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
dns_ptr:
|
|
|
|
description:
|
|
|
|
- The DNS address the I(ip_address) should resolve to.
|
|
|
|
- Omit the param to reset the reverse DNS entry to the default value.
|
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the reverse DNS entry.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
|
|
|
|
requirements:
|
|
|
|
- hcloud-python >= 1.3.0
|
|
|
|
|
|
|
|
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 reverse DNS entry for a server
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_rdns:
|
2020-03-09 13:36:01 +00:00
|
|
|
server: my-server
|
|
|
|
ip_address: 123.123.123.123
|
|
|
|
dns_ptr: example.com
|
|
|
|
state: present
|
|
|
|
|
2020-09-08 06:13:10 +00:00
|
|
|
- name: Create a reverse DNS entry for a Floating IP
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_rdns:
|
2020-09-08 06:13:10 +00:00
|
|
|
floating_ip: my-floating-ip
|
|
|
|
ip_address: 123.123.123.123
|
|
|
|
dns_ptr: example.com
|
|
|
|
state: present
|
|
|
|
|
2022-06-29 07:58:01 +00:00
|
|
|
- name: Create a reverse DNS entry for a Primary IP
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_rdns:
|
2022-06-29 07:58:01 +00:00
|
|
|
primary_ip: my-primary-ip
|
|
|
|
ip_address: 123.123.123.123
|
|
|
|
dns_ptr: example.com
|
|
|
|
state: present
|
|
|
|
|
2021-08-17 12:48:12 +00:00
|
|
|
- name: Create a reverse DNS entry for a Load Balancer
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_rdns:
|
2021-08-17 12:48:12 +00:00
|
|
|
load_balancer: my-load-balancer
|
|
|
|
ip_address: 123.123.123.123
|
|
|
|
dns_ptr: example.com
|
|
|
|
state: present
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
- name: Ensure the reverse DNS entry is absent (remove if needed)
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_rdns:
|
2020-03-09 13:36:01 +00:00
|
|
|
server: my-server
|
|
|
|
ip_address: 123.123.123.123
|
|
|
|
dns_ptr: example.com
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_rdns:
|
|
|
|
description: The reverse DNS entry
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
server:
|
|
|
|
description: Name of the server
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: my-server
|
2020-09-08 06:13:10 +00:00
|
|
|
floating_ip:
|
|
|
|
description: Name of the Floating IP
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: my-floating-ip
|
2022-06-29 07:58:01 +00:00
|
|
|
primary_ip:
|
|
|
|
description: Name of the Primary IP
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: my-primary-ip
|
2021-08-17 12:48:12 +00:00
|
|
|
load_balancer:
|
|
|
|
description: Name of the Load Balancer
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: my-load-balancer
|
2020-03-09 13:36:01 +00:00
|
|
|
ip_address:
|
|
|
|
description: The IP address that point to the DNS ptr
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: 123.123.123.123
|
|
|
|
dns_ptr:
|
|
|
|
description: The DNS that resolves to the IP
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: example.com
|
|
|
|
"""
|
|
|
|
|
2023-09-26 07:41:01 +00:00
|
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
|
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-06-27 09:50:13 +00:00
|
|
|
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import (
|
|
|
|
utils,
|
|
|
|
)
|
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
|
|
|
|
from ..module_utils.vendor.hcloud.load_balancers import BoundLoadBalancer
|
|
|
|
from ..module_utils.vendor.hcloud.primary_ips import BoundPrimaryIP
|
|
|
|
from ..module_utils.vendor.hcloud.servers import BoundServer
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudReverseDNS(AnsibleHCloud):
|
2023-09-26 07:41:01 +00:00
|
|
|
represent = "hcloud_rdns"
|
|
|
|
|
|
|
|
hcloud_resource: Optional[Union[BoundServer, BoundFloatingIP, BoundLoadBalancer, BoundPrimaryIP]] = None
|
|
|
|
hcloud_rdns: Optional[Dict[str, Any]] = None
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _prepare_result(self):
|
2020-09-08 06:13:10 +00:00
|
|
|
result = {
|
|
|
|
"server": None,
|
|
|
|
"floating_ip": None,
|
2021-08-17 12:48:12 +00:00
|
|
|
"load_balancer": None,
|
2020-03-09 13:36:01 +00:00
|
|
|
"ip_address": to_native(self.hcloud_rdns["ip_address"]),
|
|
|
|
"dns_ptr": to_native(self.hcloud_rdns["dns_ptr"]),
|
|
|
|
}
|
|
|
|
|
2020-09-08 06:13:10 +00:00
|
|
|
if self.module.params.get("server"):
|
|
|
|
result["server"] = to_native(self.hcloud_resource.name)
|
|
|
|
elif self.module.params.get("floating_ip"):
|
|
|
|
result["floating_ip"] = to_native(self.hcloud_resource.name)
|
2021-08-17 12:48:12 +00:00
|
|
|
elif self.module.params.get("load_balancer"):
|
|
|
|
result["load_balancer"] = to_native(self.hcloud_resource.name)
|
2022-06-29 07:58:01 +00:00
|
|
|
elif self.module.params.get("primary_ip"):
|
|
|
|
result["primary_ip"] = to_native(self.hcloud_resource.name)
|
2020-09-08 06:13:10 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _get_resource(self):
|
2020-03-09 13:36:01 +00:00
|
|
|
try:
|
2020-09-08 06:13:10 +00:00
|
|
|
if self.module.params.get("server"):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_resource = self.client.servers.get_by_name(self.module.params.get("server"))
|
2021-07-22 07:24:11 +00:00
|
|
|
if self.hcloud_resource is None:
|
|
|
|
self.module.fail_json(msg="The selected server does not exist")
|
2020-09-08 06:13:10 +00:00
|
|
|
elif self.module.params.get("floating_ip"):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_resource = self.client.floating_ips.get_by_name(self.module.params.get("floating_ip"))
|
2021-07-22 07:24:11 +00:00
|
|
|
if self.hcloud_resource is None:
|
|
|
|
self.module.fail_json(msg="The selected Floating IP does not exist")
|
2022-06-29 07:58:01 +00:00
|
|
|
elif self.module.params.get("primary_ip"):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_resource = self.client.primary_ips.get_by_name(self.module.params.get("primary_ip"))
|
2022-06-29 07:58:01 +00:00
|
|
|
if self.hcloud_resource is None:
|
|
|
|
self.module.fail_json(msg="The selected Floating IP does not exist")
|
2021-08-17 12:48:12 +00:00
|
|
|
elif self.module.params.get("load_balancer"):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_resource = self.client.load_balancers.get_by_name(self.module.params.get("load_balancer"))
|
2021-08-17 12:48:12 +00:00
|
|
|
if self.hcloud_resource is None:
|
|
|
|
self.module.fail_json(msg="The selected Load Balancer does not exist")
|
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 _get_rdns(self):
|
|
|
|
ip_address = self.module.params.get("ip_address")
|
|
|
|
if utils.validate_ip_address(ip_address):
|
2020-09-08 06:13:10 +00:00
|
|
|
if self.module.params.get("server"):
|
|
|
|
if self.hcloud_resource.public_net.ipv4.ip == ip_address:
|
2020-03-09 13:36:01 +00:00
|
|
|
self.hcloud_rdns = {
|
2020-09-08 06:13:10 +00:00
|
|
|
"ip_address": self.hcloud_resource.public_net.ipv4.ip,
|
|
|
|
"dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
|
2020-03-09 13:36:01 +00:00
|
|
|
}
|
2020-09-08 06:13:10 +00:00
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="The selected server does not have this IP address")
|
|
|
|
elif self.module.params.get("floating_ip"):
|
|
|
|
if self.hcloud_resource.ip == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": self.hcloud_resource.ip,
|
|
|
|
"dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="The selected Floating IP does not have this IP address")
|
2022-06-29 07:58:01 +00:00
|
|
|
elif self.module.params.get("primary_ip"):
|
|
|
|
if self.hcloud_resource.ip == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": self.hcloud_resource.ip,
|
|
|
|
"dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="The selected Primary IP does not have this IP address")
|
2021-08-17 12:48:12 +00:00
|
|
|
elif self.module.params.get("load_balancer"):
|
|
|
|
if self.hcloud_resource.public_net.ipv4.ip == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": self.hcloud_resource.public_net.ipv4.ip,
|
|
|
|
"dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="The selected Load Balancer does not have this IP address")
|
2020-09-08 06:13:10 +00:00
|
|
|
|
|
|
|
elif utils.validate_ip_v6_address(ip_address):
|
|
|
|
if self.module.params.get("server"):
|
|
|
|
for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
|
|
|
|
if ipv6_address_dns_ptr["ip"] == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": ipv6_address_dns_ptr["ip"],
|
|
|
|
"dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
|
|
|
|
}
|
|
|
|
elif self.module.params.get("floating_ip"):
|
|
|
|
for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
|
|
|
|
if ipv6_address_dns_ptr["ip"] == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": ipv6_address_dns_ptr["ip"],
|
|
|
|
"dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
|
|
|
|
}
|
2022-06-29 07:58:01 +00:00
|
|
|
elif self.module.params.get("primary_ip"):
|
|
|
|
for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
|
|
|
|
if ipv6_address_dns_ptr["ip"] == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": ipv6_address_dns_ptr["ip"],
|
|
|
|
"dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
|
|
|
|
}
|
2021-08-17 12:48:12 +00:00
|
|
|
elif self.module.params.get("load_balancer"):
|
|
|
|
for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
|
|
|
|
if ipv6_address_dns_ptr["ip"] == ip_address:
|
|
|
|
self.hcloud_rdns = {
|
|
|
|
"ip_address": ipv6_address_dns_ptr["ip"],
|
|
|
|
"dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
|
|
|
|
}
|
2020-03-09 13:36:01 +00:00
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="The given IP address is not valid")
|
|
|
|
|
|
|
|
def _create_rdns(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["dns_ptr"])
|
2020-03-09 13:36:01 +00:00
|
|
|
params = {
|
|
|
|
"ip": self.module.params.get("ip_address"),
|
|
|
|
"dns_ptr": self.module.params.get("dns_ptr"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if not self.module.check_mode:
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
self.hcloud_resource.change_dns_ptr(**params).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()
|
2020-09-08 06:13:10 +00:00
|
|
|
self._get_resource()
|
2020-03-09 13:36:01 +00:00
|
|
|
self._get_rdns()
|
|
|
|
|
|
|
|
def _update_rdns(self):
|
|
|
|
dns_ptr = self.module.params.get("dns_ptr")
|
|
|
|
if dns_ptr != self.hcloud_rdns["dns_ptr"]:
|
|
|
|
params = {
|
|
|
|
"ip": self.module.params.get("ip_address"),
|
|
|
|
"dns_ptr": dns_ptr,
|
|
|
|
}
|
|
|
|
|
|
|
|
if not self.module.check_mode:
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
self.hcloud_resource.change_dns_ptr(**params).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()
|
2020-09-08 06:13:10 +00:00
|
|
|
self._get_resource()
|
2020-03-09 13:36:01 +00:00
|
|
|
self._get_rdns()
|
|
|
|
|
|
|
|
def present_rdns(self):
|
2020-09-08 06:13:10 +00:00
|
|
|
self._get_resource()
|
2020-03-09 13:36:01 +00:00
|
|
|
self._get_rdns()
|
|
|
|
if self.hcloud_rdns is None:
|
|
|
|
self._create_rdns()
|
|
|
|
else:
|
|
|
|
self._update_rdns()
|
|
|
|
|
|
|
|
def delete_rdns(self):
|
2020-09-08 06:13:10 +00:00
|
|
|
self._get_resource()
|
2020-03-09 13:36:01 +00:00
|
|
|
self._get_rdns()
|
|
|
|
if self.hcloud_rdns is not None:
|
|
|
|
if not self.module.check_mode:
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_resource.change_dns_ptr(ip=self.hcloud_rdns["ip_address"], dns_ptr=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
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_rdns = None
|
|
|
|
|
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(
|
2020-09-08 06:13:10 +00:00
|
|
|
server={"type": "str"},
|
|
|
|
floating_ip={"type": "str"},
|
2021-08-17 12:48:12 +00:00
|
|
|
load_balancer={"type": "str"},
|
2022-06-29 07:58:01 +00:00
|
|
|
primary_ip={"type": "str"},
|
2020-03-09 13:36:01 +00:00
|
|
|
ip_address={"type": "str", "required": True},
|
|
|
|
dns_ptr={"type": "str"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-08-02 10:05:00 +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=[["server", "floating_ip", "load_balancer", "primary_ip"]],
|
|
|
|
mutually_exclusive=[["server", "floating_ip", "load_balancer", "primary_ip"]],
|
2020-03-09 13:36:01 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudReverseDNS.define_module()
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudReverseDNS(module)
|
2020-03-09 13:36:01 +00:00
|
|
|
state = module.params["state"]
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_rdns()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_rdns()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|