| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: ssh_key |
| 12 |
|
| 13 |
short_description: Create and manage ssh keys on the Hetzner Cloud. |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Create, update and manage ssh keys on the Hetzner Cloud. |
| 18 |
|
| 19 |
author: |
| 20 |
- Lukas Kaemmerling (@LKaemmerling) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- The ID of the Hetzner Cloud ssh_key to manage. |
| 26 |
- Only required if no ssh_key I(name) is given |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The Name of the Hetzner Cloud ssh_key to manage. |
| 31 |
- Only required if no ssh_key I(id) is given or a ssh_key does not exist. |
| 32 |
type: str |
| 33 |
fingerprint: |
| 34 |
description: |
| 35 |
- The Fingerprint of the Hetzner Cloud ssh_key to manage. |
| 36 |
- Only required if no ssh_key I(id) or I(name) is given. |
| 37 |
type: str |
| 38 |
labels: |
| 39 |
description: |
| 40 |
- User-defined labels (key-value pairs) |
| 41 |
type: dict |
| 42 |
public_key: |
| 43 |
description: |
| 44 |
- The Public Key to add. |
| 45 |
- Required if ssh_key does not exist. |
| 46 |
type: str |
| 47 |
force: |
| 48 |
description: |
| 49 |
- Recreate the SSH Key if the public key does not match the one in the API. |
| 50 |
type: bool |
| 51 |
default: false |
| 52 |
state: |
| 53 |
description: |
| 54 |
- State of the ssh_key. |
| 55 |
default: present |
| 56 |
choices: [ absent, present ] |
| 57 |
type: str |
| 58 |
extends_documentation_fragment: |
| 59 |
- hetzner.hcloud.hcloud |
| 60 |
|
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = """ |
| 64 |
- name: Create a basic ssh_key |
| 65 |
hetzner.hcloud.ssh_key: |
| 66 |
name: my-ssh_key |
| 67 |
public_key: ssh-rsa AAAjjk76kgf...Xt |
| 68 |
state: present |
| 69 |
|
| 70 |
- name: Create a ssh_key with labels |
| 71 |
hetzner.hcloud.ssh_key: |
| 72 |
name: my-ssh_key |
| 73 |
public_key: ssh-rsa AAAjjk76kgf...Xt |
| 74 |
labels: |
| 75 |
key: value |
| 76 |
mylabel: 123 |
| 77 |
state: present |
| 78 |
|
| 79 |
- name: Ensure the ssh_key is absent (remove if needed) |
| 80 |
hetzner.hcloud.ssh_key: |
| 81 |
name: my-ssh_key |
| 82 |
state: absent |
| 83 |
""" |
| 84 |
|
| 85 |
RETURN = """ |
| 86 |
hcloud_ssh_key: |
| 87 |
description: The ssh_key instance |
| 88 |
returned: Always |
| 89 |
type: dict |
| 90 |
contains: |
| 91 |
id: |
| 92 |
description: ID of the ssh_key |
| 93 |
type: int |
| 94 |
returned: Always |
| 95 |
sample: 12345 |
| 96 |
name: |
| 97 |
description: Name of the ssh_key |
| 98 |
type: str |
| 99 |
returned: Always |
| 100 |
sample: my-ssh-key |
| 101 |
fingerprint: |
| 102 |
description: Fingerprint of the ssh_key |
| 103 |
type: str |
| 104 |
returned: Always |
| 105 |
sample: b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f |
| 106 |
public_key: |
| 107 |
description: Public key of the ssh_key |
| 108 |
type: str |
| 109 |
returned: Always |
| 110 |
sample: "ssh-rsa AAAjjk76kgf...Xt" |
| 111 |
labels: |
| 112 |
description: User-defined labels (key-value pairs) |
| 113 |
type: dict |
| 114 |
returned: Always |
| 115 |
sample: |
| 116 |
key: value |
| 117 |
mylabel: 123 |
| 118 |
""" |
| 119 |
|
| 120 |
from ansible.module_utils.basic import AnsibleModule |
| 121 |
|
| 122 |
from ..module_utils._base import AnsibleHCloud |
| 123 |
from ..module_utils._ssh import ssh_public_key_md5_fingerprint |
| 124 |
from ..module_utils._vendor.hcloud import HCloudException |
| 125 |
from ..module_utils._vendor.hcloud.ssh_keys import BoundSSHKey |
| 126 |
|
| 127 |
|
| 128 |
class AnsibleHCloudSSHKey(AnsibleHCloud): |
| 129 |
represent = "hcloud_ssh_key" |
| 130 |
|
| 131 |
hcloud_ssh_key: BoundSSHKey | None = None |
| 132 |
|
| 133 |
def _prepare_result(self): |
| 134 |
return { |
| 135 |
"id": self.hcloud_ssh_key.id, |
| 136 |
"name": self.hcloud_ssh_key.name, |
| 137 |
"fingerprint": self.hcloud_ssh_key.fingerprint, |
| 138 |
"public_key": self.hcloud_ssh_key.public_key, |
| 139 |
"labels": self.hcloud_ssh_key.labels, |
| 140 |
} |
| 141 |
|
| 142 |
def _get_ssh_key(self): |
| 143 |
try: |
| 144 |
if self.module.params.get("id") is not None: |
| 145 |
self.hcloud_ssh_key = self.client.ssh_keys.get_by_id(self.module.params.get("id")) |
| 146 |
elif self.module.params.get("fingerprint") is not None: |
| 147 |
self.hcloud_ssh_key = self.client.ssh_keys.get_by_fingerprint(self.module.params.get("fingerprint")) |
| 148 |
elif self.module.params.get("name") is not None: |
| 149 |
self.hcloud_ssh_key = self.client.ssh_keys.get_by_name(self.module.params.get("name")) |
| 150 |
|
| 151 |
except HCloudException as exception: |
| 152 |
self.fail_json_hcloud(exception) |
| 153 |
|
| 154 |
def _create_ssh_key(self): |
| 155 |
self.module.fail_on_missing_params(required_params=["name", "public_key"]) |
| 156 |
params = { |
| 157 |
"name": self.module.params.get("name"), |
| 158 |
"public_key": self.module.params.get("public_key"), |
| 159 |
"labels": self.module.params.get("labels"), |
| 160 |
} |
| 161 |
|
| 162 |
if not self.module.check_mode: |
| 163 |
try: |
| 164 |
self.client.ssh_keys.create(**params) |
| 165 |
except HCloudException as exception: |
| 166 |
self.fail_json_hcloud(exception) |
| 167 |
self._mark_as_changed() |
| 168 |
self._get_ssh_key() |
| 169 |
|
| 170 |
def _update_ssh_key(self): |
| 171 |
name = self.module.params.get("name") |
| 172 |
if name is not None and self.hcloud_ssh_key.name != name: |
| 173 |
self.module.fail_on_missing_params(required_params=["id"]) |
| 174 |
if not self.module.check_mode: |
| 175 |
self.hcloud_ssh_key.update(name=name) |
| 176 |
self._mark_as_changed() |
| 177 |
|
| 178 |
labels = self.module.params.get("labels") |
| 179 |
if labels is not None and self.hcloud_ssh_key.labels != labels: |
| 180 |
if not self.module.check_mode: |
| 181 |
self.hcloud_ssh_key.update(labels=labels) |
| 182 |
self._mark_as_changed() |
| 183 |
|
| 184 |
public_key = self.module.params.get("public_key") |
| 185 |
if public_key is not None: |
| 186 |
fingerprint = ssh_public_key_md5_fingerprint(public_key) |
| 187 |
if fingerprint != self.hcloud_ssh_key.fingerprint: |
| 188 |
if self.module.params.get("force"): |
| 189 |
if not self.module.check_mode: |
| 190 |
self.hcloud_ssh_key.delete() |
| 191 |
self._create_ssh_key() |
| 192 |
self._mark_as_changed() |
| 193 |
else: |
| 194 |
self.module.warn( |
| 195 |
f"SSH Key '{self.hcloud_ssh_key.name}' in the API has a " |
| 196 |
f"different public key than the one provided. " |
| 197 |
f"Use the force=true argument to recreate the SSH Key in the API." |
| 198 |
) |
| 199 |
self._get_ssh_key() |
| 200 |
|
| 201 |
def present_ssh_key(self): |
| 202 |
self._get_ssh_key() |
| 203 |
if self.hcloud_ssh_key is None: |
| 204 |
self._create_ssh_key() |
| 205 |
else: |
| 206 |
self._update_ssh_key() |
| 207 |
|
| 208 |
def delete_ssh_key(self): |
| 209 |
self._get_ssh_key() |
| 210 |
if self.hcloud_ssh_key is not None: |
| 211 |
if not self.module.check_mode: |
| 212 |
try: |
| 213 |
self.client.ssh_keys.delete(self.hcloud_ssh_key) |
| 214 |
except HCloudException as exception: |
| 215 |
self.fail_json_hcloud(exception) |
| 216 |
self._mark_as_changed() |
| 217 |
self.hcloud_ssh_key = None |
| 218 |
|
| 219 |
@classmethod |
| 220 |
def define_module(cls): |
| 221 |
return AnsibleModule( |
| 222 |
argument_spec=dict( |
| 223 |
id={"type": "int"}, |
| 224 |
name={"type": "str"}, |
| 225 |
public_key={"type": "str"}, |
| 226 |
fingerprint={"type": "str"}, |
| 227 |
labels={"type": "dict"}, |
| 228 |
force={"type": "bool", "default": False}, |
| 229 |
state={ |
| 230 |
"choices": ["absent", "present"], |
| 231 |
"default": "present", |
| 232 |
}, |
| 233 |
**super().base_module_arguments(), |
| 234 |
), |
| 235 |
required_one_of=[["id", "name", "fingerprint"]], |
| 236 |
required_if=[["state", "present", ["name"]]], |
| 237 |
supports_check_mode=True, |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
def main(): |
| 242 |
module = AnsibleHCloudSSHKey.define_module() |
| 243 |
|
| 244 |
hcloud = AnsibleHCloudSSHKey(module) |
| 245 |
state = module.params.get("state") |
| 246 |
if state == "absent": |
| 247 |
hcloud.delete_ssh_key() |
| 248 |
elif state == "present": |
| 249 |
hcloud.present_ssh_key() |
| 250 |
|
| 251 |
module.exit_json(**hcloud.get_result()) |
| 252 |
|
| 253 |
|
| 254 |
if __name__ == "__main__": |
| 255 |
main() |