From ced3285659fa632168df39a628498b76cb819d2a Mon Sep 17 00:00:00 2001 From: shaerpour Date: Wed, 14 May 2025 12:31:52 +0330 Subject: [PATCH] feat: Add force option for ssh_key to re-create key if needed --- plugins/modules/ssh_key.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plugins/modules/ssh_key.py b/plugins/modules/ssh_key.py index e33987c..c9e2f20 100644 --- a/plugins/modules/ssh_key.py +++ b/plugins/modules/ssh_key.py @@ -44,6 +44,12 @@ options: - The Public Key to add. - Required if ssh_key does not exist. type: str + force: + description: + - Recreate ssh_key if it exists, using a new public key. + - Required only if using the same key name. + type: bool + default: false state: description: - State of the ssh_key. @@ -71,6 +77,13 @@ EXAMPLES = """ mylabel: 123 state: present +- name: Force create ssh_key that already exists + hetzner.hcloud.ssh_key: + name: my-ssh_key + public_key: ssh-rsa AAAAC3NzaC1...0C + state: present + force: true + - name: Ensure the ssh_key is absent (remove if needed) hetzner.hcloud.ssh_key: name: my-ssh_key @@ -175,6 +188,12 @@ class AnsibleHCloudSSHKey(AnsibleHCloud): self.hcloud_ssh_key.update(labels=labels) self._mark_as_changed() + force = self.module.params.get("force") + if force is not None and force: + if not self.module.check_mode: + self.hcloud_ssh_key.delete() + self._create_ssh_key() + self._mark_as_changed() self._get_ssh_key() def present_ssh_key(self): @@ -204,6 +223,10 @@ class AnsibleHCloudSSHKey(AnsibleHCloud): public_key={"type": "str"}, fingerprint={"type": "str"}, labels={"type": "dict"}, + force={ + "type": "bool", + "default": False, + }, state={ "choices": ["absent", "present"], "default": "present",