| 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_info |
| 12 |
short_description: Gather infos about your Hetzner Cloud ssh_keys. |
| 13 |
description: |
| 14 |
- Gather facts about your Hetzner Cloud ssh_keys. |
| 15 |
author: |
| 16 |
- Christopher Schmitt (@cschmitt-hcloud) |
| 17 |
options: |
| 18 |
id: |
| 19 |
description: |
| 20 |
- The ID of the ssh key you want to get. |
| 21 |
- The module will fail if the provided ID is invalid. |
| 22 |
type: int |
| 23 |
name: |
| 24 |
description: |
| 25 |
- The name of the ssh key you want to get. |
| 26 |
type: str |
| 27 |
fingerprint: |
| 28 |
description: |
| 29 |
- The fingerprint of the ssh key you want to get. |
| 30 |
type: str |
| 31 |
label_selector: |
| 32 |
description: |
| 33 |
- The label selector for the ssh key you want to get. |
| 34 |
type: str |
| 35 |
extends_documentation_fragment: |
| 36 |
- hetzner.hcloud.hcloud |
| 37 |
|
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = """ |
| 41 |
- name: Gather hcloud sshkey infos |
| 42 |
hetzner.hcloud.ssh_key_info: |
| 43 |
register: output |
| 44 |
- name: Print the gathered infos |
| 45 |
debug: |
| 46 |
var: output.hcloud_ssh_key_info |
| 47 |
""" |
| 48 |
|
| 49 |
RETURN = """ |
| 50 |
hcloud_ssh_key_info: |
| 51 |
description: The ssh key instances |
| 52 |
returned: Always |
| 53 |
type: list |
| 54 |
contains: |
| 55 |
id: |
| 56 |
description: Numeric identifier of the ssh_key |
| 57 |
returned: always |
| 58 |
type: int |
| 59 |
sample: 1937415 |
| 60 |
name: |
| 61 |
description: Name of the ssh_key |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
sample: my-ssh-key |
| 65 |
fingerprint: |
| 66 |
description: Fingerprint of the ssh key |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: 0e:e0:bd:c7:2d:1f:69:49:94:44:91:f1:19:fd:35:f3 |
| 70 |
public_key: |
| 71 |
description: The actual public key |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
sample: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGpl/tnk74nnQJxxLAtutUApUZMRJxryKh7VXkNbd4g9 [email protected]" |
| 75 |
labels: |
| 76 |
description: User-defined labels (key-value pairs) |
| 77 |
returned: always |
| 78 |
type: dict |
| 79 |
""" |
| 80 |
|
| 81 |
from ansible.module_utils.basic import AnsibleModule |
| 82 |
|
| 83 |
from ..module_utils._base import AnsibleHCloud |
| 84 |
from ..module_utils._vendor.hcloud import HCloudException |
| 85 |
from ..module_utils._vendor.hcloud.ssh_keys import BoundSSHKey |
| 86 |
|
| 87 |
|
| 88 |
class AnsibleHCloudSSHKeyInfo(AnsibleHCloud): |
| 89 |
represent = "hcloud_ssh_key_info" |
| 90 |
|
| 91 |
hcloud_ssh_key_info: list[BoundSSHKey] | None = None |
| 92 |
|
| 93 |
def _prepare_result(self): |
| 94 |
tmp = [] |
| 95 |
|
| 96 |
for ssh_key in self.hcloud_ssh_key_info: |
| 97 |
if ssh_key is None: |
| 98 |
continue |
| 99 |
|
| 100 |
tmp.append( |
| 101 |
{ |
| 102 |
"id": ssh_key.id, |
| 103 |
"name": ssh_key.name, |
| 104 |
"fingerprint": ssh_key.fingerprint, |
| 105 |
"public_key": ssh_key.public_key, |
| 106 |
"labels": ssh_key.labels, |
| 107 |
} |
| 108 |
) |
| 109 |
return tmp |
| 110 |
|
| 111 |
def get_ssh_keys(self): |
| 112 |
try: |
| 113 |
if self.module.params.get("id") is not None: |
| 114 |
self.hcloud_ssh_key_info = [self.client.ssh_keys.get_by_id(self.module.params.get("id"))] |
| 115 |
elif self.module.params.get("name") is not None: |
| 116 |
self.hcloud_ssh_key_info = [self.client.ssh_keys.get_by_name(self.module.params.get("name"))] |
| 117 |
elif self.module.params.get("fingerprint") is not None: |
| 118 |
self.hcloud_ssh_key_info = [ |
| 119 |
self.client.ssh_keys.get_by_fingerprint(self.module.params.get("fingerprint")) |
| 120 |
] |
| 121 |
elif self.module.params.get("label_selector") is not None: |
| 122 |
self.hcloud_ssh_key_info = self.client.ssh_keys.get_all( |
| 123 |
label_selector=self.module.params.get("label_selector") |
| 124 |
) |
| 125 |
else: |
| 126 |
self.hcloud_ssh_key_info = self.client.ssh_keys.get_all() |
| 127 |
|
| 128 |
except HCloudException as exception: |
| 129 |
self.fail_json_hcloud(exception) |
| 130 |
|
| 131 |
@classmethod |
| 132 |
def define_module(cls): |
| 133 |
return AnsibleModule( |
| 134 |
argument_spec=dict( |
| 135 |
id={"type": "int"}, |
| 136 |
name={"type": "str"}, |
| 137 |
fingerprint={"type": "str"}, |
| 138 |
label_selector={"type": "str"}, |
| 139 |
**super().base_module_arguments(), |
| 140 |
), |
| 141 |
supports_check_mode=True, |
| 142 |
) |
| 143 |
|
| 144 |
|
| 145 |
def main(): |
| 146 |
module = AnsibleHCloudSSHKeyInfo.define_module() |
| 147 |
hcloud = AnsibleHCloudSSHKeyInfo(module) |
| 148 |
|
| 149 |
hcloud.get_ssh_keys() |
| 150 |
result = hcloud.get_result() |
| 151 |
|
| 152 |
ansible_info = {"hcloud_ssh_key_info": result["hcloud_ssh_key_info"]} |
| 153 |
module.exit_json(**ansible_info) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |