diff --git a/changelogs/fragments/hcloud_server-improve-error-message-on-not-existing-firewall.yml b/changelogs/fragments/hcloud_server-improve-error-message-on-not-existing-firewall.yml new file mode 100644 index 0000000..c67adb5 --- /dev/null +++ b/changelogs/fragments/hcloud_server-improve-error-message-on-not-existing-firewall.yml @@ -0,0 +1,2 @@ +bugfixes: + - hcloud_server Improve Error Message when attaching a not existing firewall to a server diff --git a/plugins/modules/hcloud_server.py b/plugins/modules/hcloud_server.py index 9b447cf..175d49b 100644 --- a/plugins/modules/hcloud_server.py +++ b/plugins/modules/hcloud_server.py @@ -476,6 +476,8 @@ class AnsibleHcloudServer(Hcloud): self._mark_as_changed() if not self.module.check_mode: fw = self.client.firewalls.get_by_name(fname) + if fw is None: + self.module.fail_json(msg="firewall %s was not found" % fname) r = FirewallResource(type="server", server=self.hcloud_server) actions = self.client.firewalls.apply_to_resources(fw, [r]) for a in actions: diff --git a/tests/integration/targets/hcloud_server/tasks/main.yml b/tests/integration/targets/hcloud_server/tasks/main.yml index 526f74b..8534c0a 100644 --- a/tests/integration/targets/hcloud_server/tasks/main.yml +++ b/tests/integration/targets/hcloud_server/tasks/main.yml @@ -480,6 +480,20 @@ - main_server.hcloud_server.status == "running" - main_server.root_password != "" +- name: test add not existing firewall should fail + hcloud_server: + name: "{{ hcloud_server_name }}" + firewalls: + - not-existing + state: present + ignore_errors: yes + register: result +- name: verifyt add not existing firewall should fail + assert: + that: + - result is failed + - 'result.msg == "firewall not-existing was not found"' + - name: test activate rescue mode with check_mode hcloud_server: name: "{{ hcloud_server_name }}"