From 39e0eb8206db58ece59b59ef856f46d6004916c3 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 28 Apr 2025 10:59:45 +0200 Subject: [PATCH] rewrite attach volume logic --- plugins/modules/volume_attachment.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/plugins/modules/volume_attachment.py b/plugins/modules/volume_attachment.py index 9906044..07d7a1f 100644 --- a/plugins/modules/volume_attachment.py +++ b/plugins/modules/volume_attachment.py @@ -121,14 +121,29 @@ class AnsibleHcloudVolumeAttachment(AnsibleHCloud): def attach_volume(self): try: self._get_server_and_volume() - server_name = self.module.params.get("server") - server = self.client.servers.get_by_name(server_name) - if self.hcloud_volume.server is None or self.hcloud_server.name != server.name: + + if self.hcloud_volume.server is not None: + if self.hcloud_volume.server.id == self.hcloud_server.id: + return + if not self.module.check_mode: - automount = self.module.params.get("automount", False) - action = self.hcloud_volume.attach(server=server, automount=automount) + action = self.hcloud_volume.detach() action.wait_until_finished() + + self.hcloud_volume.server = None self._mark_as_changed() + + else: + if not self.module.check_mode: + action = self.hcloud_volume.attach( + server=self.hcloud_server, + automount=self.module.params.get("automount"), + ) + action.wait_until_finished() + + self.hcloud_volume.server = self.hcloud_server + self._mark_as_changed() + except HCloudException as exception: self.fail_json_hcloud(exception)