diff --git a/changelogs/fragments/fix-idempotency-floating-ip.yml b/changelogs/fragments/fix-idempotency-floating-ip.yml new file mode 100644 index 0000000..99bc49c --- /dev/null +++ b/changelogs/fragments/fix-idempotency-floating-ip.yml @@ -0,0 +1,2 @@ +bugfixes: + - hcloud_floating_ip Fix idempotency when floating ip is assigned to server diff --git a/plugins/modules/hcloud_floating_ip.py b/plugins/modules/hcloud_floating_ip.py index a08932e..9f50342 100644 --- a/plugins/modules/hcloud_floating_ip.py +++ b/plugins/modules/hcloud_floating_ip.py @@ -259,17 +259,24 @@ class AnsibleHcloudFloatingIP(Hcloud): self._mark_as_changed() server = self.module.params.get("server") - if server is not None: - if self.module.params.get("force") or self.hcloud_floating_ip.server is None: + if server is not None and self.hcloud_floating_ip.server is not None: + if self.module.params.get("force") and server != self.hcloud_floating_ip.server.name: if not self.module.check_mode: self.hcloud_floating_ip.assign( - self.client.servers.get_by_name(self.module.params.get("server")) + self.client.servers.get_by_name(server) ) - else: + self._mark_as_changed() + elif server != self.hcloud_floating_ip.server.name: self.module.warn( - "Floating IP is already assigned to server %s. You need to unassign the Floating IP or use force=yes." + "Floating IP is already assigned to another server %s. You need to unassign the Floating IP or use force=yes." % self.hcloud_floating_ip.server.name ) + self._mark_as_changed() + elif server is not None and self.hcloud_floating_ip.server is None: + if not self.module.check_mode: + self.hcloud_floating_ip.assign( + self.client.servers.get_by_name(server) + ) self._mark_as_changed() elif server is None and self.hcloud_floating_ip.server is not None: if not self.module.check_mode: diff --git a/tests/integration/targets/hcloud_floating_ip/tasks/main.yml b/tests/integration/targets/hcloud_floating_ip/tasks/main.yml index 8f9d4dc..70f23e3 100644 --- a/tests/integration/targets/hcloud_floating_ip/tasks/main.yml +++ b/tests/integration/targets/hcloud_floating_ip/tasks/main.yml @@ -213,6 +213,18 @@ - floatingIP is changed - floatingIP.hcloud_floating_ip.server == "{{ main_server.hcloud_server.name }}" +- name: test assign Floating IP idempotency + hcloud_floating_ip: + name: "{{ hcloud_floating_ip_name }}" + description: "changed-description" + type: ipv4 + server: "{{ main_server.hcloud_server.name }}" + register: floatingIP +- name: verify test unassign Floating IPidempotency + assert: + that: + - floatingIP is not changed + - name: test unassign Floating IP hcloud_floating_ip: name: "{{ hcloud_floating_ip_name }}"