1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add hcloud_ssh_key module (#53550)

This commit is contained in:
Lukas Kämmerling 2019-03-11 15:42:49 +01:00 committed by René Moser
parent ffa014e680
commit 3e02502056
4 changed files with 414 additions and 0 deletions

View file

@ -0,0 +1,250 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = """
---
module: hcloud_ssh_key
short_description: Create and manage ssh keys on the Hetzner Cloud.
version_added: "2.8"
description:
- Create, update and manage ssh keys on the Hetzner Cloud.
author:
- Lukas Kaemmerling (@lkaemmerling)
options:
id:
description:
- The ID of the Hetzner Cloud ssh_key to manage.
- Only required if no ssh_key I(name) is given
type: int
name:
description:
- The Name of the Hetzner Cloud ssh_key to manage.
- Only required if no ssh_key I(id) is given or a ssh_key does not exists.
type: str
fingerprint:
description:
- The Fingerprint of the Hetzner Cloud ssh_key to manage.
- Only required if no ssh_key I(id) or I(name) is given.
type: str
labels:
description:
- User-defined labels (key-value pairs)
type: dict
public_key:
description:
- The Public Key to add.
- Required if ssh_key does not exists.
type: str
state:
description:
- State of the ssh_key.
default: present
choices: [ absent, present ]
type: str
extends_documentation_fragment: hcloud
"""
EXAMPLES = """
- name: Create a basic ssh_key
hcloud_ssh_key:
name: my-ssh_key
public_key: "ssh-rsa AAAjjk76kgf...Xt"
state: present
- name: Create a ssh_key with labels
hcloud_ssh_key:
name: my-ssh_key
public_key: "ssh-rsa AAAjjk76kgf...Xt"
labels:
key: value
mylabel: 123
state: present
- name: Ensure the ssh_key is absent (remove if needed)
hcloud_ssh_key:
name: my-ssh_key
state: absent
"""
RETURN = """
hcloud_ssh_key:
description: The ssh_key instance
returned: Always
type: complex
contains:
id:
description: ID of the ssh_key
type: int
returned: Always
sample: 12345
name:
description: Name of the ssh_key
type: string
returned: Always
sample: my-ssh-key
fingerprint:
description: Fingerprint of the ssh_key
type: string
returned: Always
sample: b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f
public_key:
description: Public key of the ssh_key
type: string
returned: Always
sample: "ssh-rsa AAAjjk76kgf...Xt"
labels:
description: User-defined labels (key-value pairs)
type: dict
returned: Always
sample:
key: value
mylabel: 123
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.hcloud import Hcloud
try:
from hcloud.volumes.domain import Volume
from hcloud.ssh_keys.domain import SSHKey
from hcloud.ssh_keys.domain import Server
from hcloud import APIException
except ImportError:
pass
class AnsibleHcloudSSHKey(Hcloud):
def __init__(self, module):
Hcloud.__init__(self, module, "hcloud_ssh_key")
self.hcloud_ssh_key = None
def _prepare_result(self):
return {
"id": to_native(self.hcloud_ssh_key.id),
"name": to_native(self.hcloud_ssh_key.name),
"fingerprint": to_native(self.hcloud_ssh_key.fingerprint),
"public_key": to_native(self.hcloud_ssh_key.public_key),
"labels": self.hcloud_ssh_key.labels,
}
def _get_ssh_key(self):
try:
if self.module.params.get("id") is not None:
self.hcloud_ssh_key = self.client.ssh_keys.get_by_id(
self.module.params.get("id")
)
elif self.module.params.get("fingerprint") is not None:
self.hcloud_ssh_key = self.client.ssh_keys.get_by_fingerprint(
self.module.params.get("fingerprint")
)
elif self.module.params.get("name") is not None:
self.hcloud_ssh_key = self.client.ssh_keys.get_by_name(
self.module.params.get("name")
)
except APIException as e:
self.module.fail_json(msg=e.message)
def _create_ssh_key(self):
self.module.fail_on_missing_params(
required_params=["name", "public_key"]
)
params = {
"name": self.module.params.get("name"),
"public_key": self.module.params.get("public_key"),
"labels": self.module.params.get("labels")
}
if not self.module.check_mode:
self.client.ssh_keys.create(**params)
self._mark_as_changed()
self._get_ssh_key()
def _update_ssh_key(self):
name = self.module.params.get("name")
if name is not None and self.hcloud_ssh_key.name != name:
self.module.fail_on_missing_params(
required_params=["id"]
)
if not self.module.check_mode:
self.hcloud_ssh_key.update(name=name)
self._mark_as_changed()
labels = self.module.params.get("labels")
if labels is not None and self.hcloud_ssh_key.labels != labels:
if not self.module.check_mode:
self.hcloud_ssh_key.update(labels=labels)
self._mark_as_changed()
self._get_ssh_key()
def present_ssh_key(self):
self._get_ssh_key()
if self.hcloud_ssh_key is None:
self._create_ssh_key()
else:
self._update_ssh_key()
def delete_ssh_key(self):
self._get_ssh_key()
if self.hcloud_ssh_key is not None:
if not self.module.check_mode:
self.client.ssh_keys.delete(self.hcloud_ssh_key)
self._mark_as_changed()
self.hcloud_ssh_key = None
@staticmethod
def define_module():
return AnsibleModule(
argument_spec=dict(
id={"type": "int"},
name={"type": "str"},
public_key={"type": "str"},
fingerprint={"type": "str"},
labels={"type": "dict"},
state={
"choices": ["absent", "present"],
"default": "present",
},
**Hcloud.base_module_arguments()
),
required_one_of=[['id', 'name', 'fingerprint']],
required_if=[['state', 'present', ['name']]],
supports_check_mode=True,
)
def main():
module = AnsibleHcloudSSHKey.define_module()
hcloud = AnsibleHcloudSSHKey(module)
state = module.params.get("state")
if state == "absent":
hcloud.delete_ssh_key()
elif state == "present":
hcloud.present_ssh_key()
module.exit_json(**hcloud.get_result())
if __name__ == "__main__":
main()

View file

@ -0,0 +1,2 @@
cloud/hcloud
unsupported

View file

@ -0,0 +1,8 @@
# 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)
---
hcloud_prefix: "tests"
hcloud_server_name: "{{hcloud_prefix}}-integration"
hcloud_ssh_key_name: "{{hcloud_prefix}}-integration"
hcloud_ssh_key_public_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDnaTPfKaX1QKcRLOfr34buVLh5FhJAThI9NYB0xNdXsMd4Y0zLyyCQzHbx4eWCVZxym/s6csWSeLaAhO1GOHeAw3hQFMqf1oTBx6Y8g0pKpeotKPa/PDSUzdZF9Lc+DadtpQd8kFVHAu1Kd3zoEUnk1u6kP7I4qu4Z/6F9qBDF+M3aobiPVxdS7GwaVRW3nZu+FcQDLiBiNOjuRDyjHcDfEUkoh2SOu25RrFtGPzFu5mGmBJwotKpWAocLGfHzyn/fAHxgw3jKZVH/t+XWQFnl82Ie8yE3Z1EZ7oDkNRqFQT9AdXEQOLycTTYTQMJZpgeFTv3sAo6lPRCusiFmmLcf ci@ansible.hetzner.cloud"
hcloud_ssh_key_fingerprint: "56:89:c4:d6:a7:4a:79:82:f4:c2:58:9c:e1:d2:2d:4e"

View file

@ -0,0 +1,154 @@
# 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)
---
- name: setup
hcloud_ssh_key:
fingerprint: "{{ hcloud_ssh_key_fingerprint }}"
state: absent
register: result
- name: verify setup
assert:
that:
- result is success
- name: test missing required parameters on create ssh_key
hcloud_ssh_key:
name: "{{ hcloud_ssh_key_name }}"
register: result
ignore_errors: yes
- name: verify fail test missing required parameters on create server
assert:
that:
- result is failed
- 'result.msg == "missing required arguments: public_key"'
- name: test create ssh key with check mode
hcloud_ssh_key:
name: "{{ hcloud_ssh_key_name }}"
public_key: "{{ hcloud_ssh_key_public_key }}"
register: result
check_mode: yes
- name: test create ssh key with check mode
assert:
that:
- result is changed
- name: test create ssh key
hcloud_ssh_key:
name: "{{ hcloud_ssh_key_name }}"
public_key: "{{ hcloud_ssh_key_public_key }}"
labels:
key: value
my-label: label
register: sshKey
- name: verify create ssh key
assert:
that:
- sshKey is changed
- sshKey.hcloud_ssh_key.name == "{{ hcloud_ssh_key_name }}"
- sshKey.hcloud_ssh_key.public_key == "{{ hcloud_ssh_key_public_key }}"
- sshKey.hcloud_ssh_key.fingerprint == "{{ hcloud_ssh_key_fingerprint }}"
- sshKey.hcloud_ssh_key.labels.key == "value"
- name: test create ssh key idempotence
hcloud_ssh_key:
name: "{{ hcloud_ssh_key_name }}"
public_key: "{{ hcloud_ssh_key_public_key }}"
register: result
- name: verify create ssh key idempotence
assert:
that:
- result is not changed
- name: test update ssh key with check mode
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
name: "changed-ssh-key-name"
register: result
check_mode: yes
- name: test create ssh key with check mode
assert:
that:
- result is changed
- name: test update ssh key
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
name: "changed-ssh-key-name"
labels:
key: value
register: result
- name: test update ssh key
assert:
that:
- result is changed
- result.hcloud_ssh_key.name == "changed-ssh-key-name"
- name: test update ssh key with same labels
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
name: "changed-ssh-key-name"
labels:
key: value
register: result
- name: test update ssh key with same labels
assert:
that:
- result is not changed
- name: test update ssh key with other labels
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
name: "changed-ssh-key-name"
labels:
key: value
test: 123
register: result
- name: test update ssh key with other labels
assert:
that:
- result is changed
- name: test rename ssh key
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
name: "{{ hcloud_ssh_key_name }}"
register: result
- name: test rename ssh key
assert:
that:
- result is changed
- result.hcloud_ssh_key.name == "{{ hcloud_ssh_key_name }}"
- name: test create server with ssh key
hcloud_server:
name: "{{ hcloud_server_name }}"
server_type: cx11
image: "ubuntu-18.04"
ssh_keys:
- "{{ hcloud_ssh_key_name }}"
state: started
register: main_server
- name: verify create server with ssh key
assert:
that:
- main_server is changed
- name: absent ssh key
hcloud_ssh_key:
id: "{{ sshKey.hcloud_ssh_key.id }}"
state: absent
register: result
- name: verify absent server
assert:
that:
- result is success
- name: cleanup
hcloud_server:
name: "{{ hcloud_server_name }}"
state: absent
register: result
- name: verify cleanup
assert:
that:
- result is success