From 7f08de6b6ce7ca7ea46d34d34f001acc1ca5df2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= <4281581+LKaemmerling@users.noreply.github.com> Date: Mon, 27 May 2019 07:48:48 +0200 Subject: [PATCH] hcloud_volume attach step should be idempotent (#56906) --- ...6906-hcloud_volume_attaching_idempotent.yaml | 2 ++ .../modules/cloud/hcloud/hcloud_volume.py | 2 +- .../targets/hcloud_volume/tasks/main.yml | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/56906-hcloud_volume_attaching_idempotent.yaml diff --git a/changelogs/fragments/56906-hcloud_volume_attaching_idempotent.yaml b/changelogs/fragments/56906-hcloud_volume_attaching_idempotent.yaml new file mode 100644 index 0000000000..7c2552feff --- /dev/null +++ b/changelogs/fragments/56906-hcloud_volume_attaching_idempotent.yaml @@ -0,0 +1,2 @@ +bugfixes: + - hcloud_volume - Fix idempotency when attaching a server to a volume. \ No newline at end of file diff --git a/lib/ansible/modules/cloud/hcloud/hcloud_volume.py b/lib/ansible/modules/cloud/hcloud/hcloud_volume.py index 1902c89a83..5426c8cee0 100644 --- a/lib/ansible/modules/cloud/hcloud/hcloud_volume.py +++ b/lib/ansible/modules/cloud/hcloud/hcloud_volume.py @@ -232,7 +232,7 @@ class AnsibleHcloudVolume(Hcloud): server_name = self.module.params.get("server") if server_name: server = self.client.servers.get_by_name(server_name) - if self.hcloud_volume.server != server: + if self.hcloud_volume.server is None or self.hcloud_volume.server.name != server.name: if not self.module.check_mode: automount = self.module.params.get("automount", False) self.hcloud_volume.attach(server, automount=automount).wait_until_finished() diff --git a/test/integration/targets/hcloud_volume/tasks/main.yml b/test/integration/targets/hcloud_volume/tasks/main.yml index ba59af06b1..76e04b5dc8 100644 --- a/test/integration/targets/hcloud_volume/tasks/main.yml +++ b/test/integration/targets/hcloud_volume/tasks/main.yml @@ -8,11 +8,11 @@ image: ubuntu-18.04 state: started location: "fsn1" - register: main_server + register: vol_server - name: verify setup server assert: that: - - main_server is changed + - vol_server is changed - name: test missing size parameter on create Volume hcloud_volume: @@ -86,7 +86,17 @@ that: - volume is changed - volume.hcloud_volume.server == "{{hcloud_server_name}}" - - main_server is changed + +- name: test attach volume idempotence + hcloud_volume: + name: "{{hcloud_volume_name}}" + server: "{{hcloud_server_name}}" + register: volume +- name: verify attach volume idempotence + assert: + that: + - volume is not changed + - volume.hcloud_volume.server == "{{hcloud_server_name}}" - name: test detach volume with checkmode hcloud_volume: @@ -109,7 +119,6 @@ - volume is changed - volume.hcloud_volume.location == "fsn1" - volume.hcloud_volume.server != "{{hcloud_server_name}}" - - main_server is changed - name: test update volume label hcloud_volume: