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-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = """
|
2020-06-29 13:41:30 +00:00
|
|
|
---
|
|
|
|
module: hcloud_certificate
|
|
|
|
|
|
|
|
short_description: Create and manage certificates on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage certificates on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud certificate to manage.
|
|
|
|
- Only required if no certificate I(name) is given
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud certificate to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no certificate I(id) is given or a certificate does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs)
|
|
|
|
type: dict
|
|
|
|
certificate:
|
|
|
|
description:
|
|
|
|
- Certificate and chain in PEM format, in order so that each record directly certifies the one preceding.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if certificate does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
|
|
|
private_key:
|
|
|
|
description:
|
|
|
|
- Certificate key in PEM format.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if certificate does not exist.
|
2020-06-29 13:41:30 +00:00
|
|
|
type: str
|
2021-04-06 09:59:59 +00:00
|
|
|
domain_names:
|
|
|
|
description:
|
|
|
|
- Certificate key in PEM format.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if certificate does not exist.
|
2021-04-06 09:59:59 +00:00
|
|
|
type: list
|
2022-11-08 07:41:17 +00:00
|
|
|
default: [ ]
|
2021-04-06 09:59:59 +00:00
|
|
|
elements: str
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- Choose between uploading a Certificate in PEM format or requesting a managed Let's Encrypt Certificate.
|
|
|
|
default: uploaded
|
|
|
|
choices: [ uploaded, managed ]
|
|
|
|
type: str
|
2020-06-29 13:41:30 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the certificate.
|
|
|
|
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 certificate
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_certificate:
|
2020-06-29 13:41:30 +00:00
|
|
|
name: my-certificate
|
|
|
|
certificate: "ssh-rsa AAAjjk76kgf...Xt"
|
|
|
|
private_key: "ssh-rsa AAAjjk76kgf...Xt"
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Create a certificate with labels
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_certificate:
|
2020-06-29 13:41:30 +00:00
|
|
|
name: my-certificate
|
|
|
|
certificate: "ssh-rsa AAAjjk76kgf...Xt"
|
|
|
|
private_key: "ssh-rsa AAAjjk76kgf...Xt"
|
|
|
|
labels:
|
|
|
|
key: value
|
|
|
|
mylabel: 123
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the certificate is absent (remove if needed)
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_certificate:
|
2020-06-29 13:41:30 +00:00
|
|
|
name: my-certificate
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_certificate:
|
|
|
|
description: The certificate instance
|
|
|
|
returned: Always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Numeric identifier of the certificate
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 1937415
|
|
|
|
name:
|
|
|
|
description: Name of the certificate
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: my website cert
|
|
|
|
fingerprint:
|
|
|
|
description: Fingerprint of the certificate
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f"
|
|
|
|
certificate:
|
|
|
|
description: Certificate and chain in PEM format
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "-----BEGIN CERTIFICATE-----..."
|
|
|
|
domain_names:
|
|
|
|
description: List of Domains and Subdomains covered by the Certificate
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
not_valid_before:
|
|
|
|
description: Point in time when the Certificate becomes valid (in ISO-8601 format)
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
not_valid_after:
|
|
|
|
description: Point in time when the Certificate stops being valid (in ISO-8601 format)
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
"""
|
|
|
|
|
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
|
2020-06-29 13:41:30 +00:00
|
|
|
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleHcloudCertificate(Hcloud):
|
|
|
|
def __init__(self, module):
|
|
|
|
Hcloud.__init__(self, module, "hcloud_certificate")
|
|
|
|
self.hcloud_certificate = None
|
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
|
|
|
"id": to_native(self.hcloud_certificate.id),
|
|
|
|
"name": to_native(self.hcloud_certificate.name),
|
2021-04-06 09:59:59 +00:00
|
|
|
"type": to_native(self.hcloud_certificate.type),
|
2020-06-29 13:41:30 +00:00
|
|
|
"fingerprint": to_native(self.hcloud_certificate.fingerprint),
|
|
|
|
"certificate": to_native(self.hcloud_certificate.certificate),
|
|
|
|
"not_valid_before": to_native(self.hcloud_certificate.not_valid_before),
|
|
|
|
"not_valid_after": to_native(self.hcloud_certificate.not_valid_after),
|
|
|
|
"domain_names": [to_native(domain) for domain in self.hcloud_certificate.domain_names],
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.hcloud_certificate.labels,
|
2020-06-29 13:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_certificate(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_certificate = self.client.certificates.get_by_id(self.module.params.get("id"))
|
2020-06-29 13:41:30 +00:00
|
|
|
elif self.module.params.get("name") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_certificate = self.client.certificates.get_by_name(self.module.params.get("name"))
|
2020-06-29 13:41:30 +00:00
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
except Exception as e:
|
2020-06-29 13:41:30 +00:00
|
|
|
self.module.fail_json(msg=e.message)
|
|
|
|
|
|
|
|
def _create_certificate(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name"])
|
2021-04-06 09:59:59 +00:00
|
|
|
|
2020-06-29 13:41:30 +00:00
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.module.params.get("labels"),
|
2020-06-29 13:41:30 +00:00
|
|
|
}
|
2023-06-27 09:50:13 +00:00
|
|
|
if self.module.params.get("type") == "uploaded":
|
|
|
|
self.module.fail_on_missing_params(required_params=["certificate", "private_key"])
|
2021-04-06 09:59:59 +00:00
|
|
|
params["certificate"] = self.module.params.get("certificate")
|
|
|
|
params["private_key"] = self.module.params.get("private_key")
|
|
|
|
if not self.module.check_mode:
|
|
|
|
try:
|
|
|
|
self.client.certificates.create(**params)
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.message)
|
|
|
|
else:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["domain_names"])
|
2021-04-06 09:59:59 +00:00
|
|
|
params["domain_names"] = self.module.params.get("domain_names")
|
|
|
|
if not self.module.check_mode:
|
|
|
|
try:
|
|
|
|
resp = self.client.certificates.create_managed(**params)
|
|
|
|
resp.action.wait_until_finished(max_retries=1000)
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.message)
|
2020-06-29 13:41:30 +00:00
|
|
|
|
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_certificate()
|
|
|
|
|
|
|
|
def _update_certificate(self):
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
name = self.module.params.get("name")
|
|
|
|
if name is not None and self.hcloud_certificate.name != name:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["id"])
|
2021-02-24 12:37:58 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_certificate.update(name=name)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and self.hcloud_certificate.labels != labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_certificate.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.message)
|
2020-06-29 13:41:30 +00:00
|
|
|
self._get_certificate()
|
|
|
|
|
|
|
|
def present_certificate(self):
|
|
|
|
self._get_certificate()
|
|
|
|
if self.hcloud_certificate is None:
|
|
|
|
self._create_certificate()
|
|
|
|
else:
|
|
|
|
self._update_certificate()
|
|
|
|
|
|
|
|
def delete_certificate(self):
|
|
|
|
self._get_certificate()
|
|
|
|
if self.hcloud_certificate is not None:
|
|
|
|
if not self.module.check_mode:
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
self.client.certificates.delete(self.hcloud_certificate)
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.message)
|
2020-06-29 13:41:30 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_certificate = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def define_module():
|
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
2021-04-06 09:59:59 +00:00
|
|
|
type={
|
|
|
|
"choices": ["uploaded", "managed"],
|
|
|
|
"default": "uploaded",
|
|
|
|
},
|
|
|
|
domain_names={"type": "list", "elements": "str", "default": []},
|
2020-06-29 13:41:30 +00:00
|
|
|
certificate={"type": "str"},
|
2021-03-26 09:02:52 +00:00
|
|
|
private_key={"type": "str", "no_log": True},
|
2020-06-29 13:41:30 +00:00
|
|
|
labels={"type": "dict"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
|
|
|
**Hcloud.base_module_arguments()
|
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
|
|
|
required_if=[["state", "present", ["name"]]],
|
2020-06-29 13:41:30 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleHcloudCertificate.define_module()
|
|
|
|
|
|
|
|
hcloud = AnsibleHcloudCertificate(module)
|
|
|
|
state = module.params.get("state")
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_certificate()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_certificate()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|