diff --git a/plugins/modules/hcloud_ssh_key.py b/plugins/modules/hcloud_ssh_key.py index 31c9625..df13922 100644 --- a/plugins/modules/hcloud_ssh_key.py +++ b/plugins/modules/hcloud_ssh_key.py @@ -156,7 +156,7 @@ class AnsibleHcloudSSHKey(Hcloud): self.module.params.get("name") ) - except APIException as e: + except Exception as e: self.module.fail_json(msg=e.message) def _create_ssh_key(self): @@ -170,7 +170,10 @@ class AnsibleHcloudSSHKey(Hcloud): } if not self.module.check_mode: - self.client.ssh_keys.create(**params) + try: + self.client.ssh_keys.create(**params) + except Exception as e: + self.module.fail_json(msg=e.message) self._mark_as_changed() self._get_ssh_key() diff --git a/tests/integration/targets/hcloud_ssh_key/defaults/main.yml b/tests/integration/targets/hcloud_ssh_key/defaults/main.yml index 666d334..aa2c739 100644 --- a/tests/integration/targets/hcloud_ssh_key/defaults/main.yml +++ b/tests/integration/targets/hcloud_ssh_key/defaults/main.yml @@ -2,7 +2,7 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) --- hcloud_prefix: "tests" -hcloud_server_name: "{{hcloud_prefix}}-integration" +hcloud_server_name: "{{hcloud_prefix}}" hcloud_ssh_key_name: "{{hcloud_prefix}}-integration" hcloud_ssh_key_public_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDnaTPfKaX1QKcRLOfr34buVLh5FhJAThI9NYB0xNdXsMd4Y0zLyyCQzHbx4eWCVZxym/s6csWSeLaAhO1GOHeAw3hQFMqf1oTBx6Y8g0pKpeotKPa/PDSUzdZF9Lc+DadtpQd8kFVHAu1Kd3zoEUnk1u6kP7I4qu4Z/6F9qBDF+M3aobiPVxdS7GwaVRW3nZu+FcQDLiBiNOjuRDyjHcDfEUkoh2SOu25RrFtGPzFu5mGmBJwotKpWAocLGfHzyn/fAHxgw3jKZVH/t+XWQFnl82Ie8yE3Z1EZ7oDkNRqFQT9AdXEQOLycTTYTQMJZpgeFTv3sAo6lPRCusiFmmLcf ci@ansible.hetzner.cloud" hcloud_ssh_key_fingerprint: "56:89:c4:d6:a7:4a:79:82:f4:c2:58:9c:e1:d2:2d:4e" diff --git a/tests/integration/targets/hcloud_ssh_key/tasks/main.yml b/tests/integration/targets/hcloud_ssh_key/tasks/main.yml index baaadd4..17f3c20 100644 --- a/tests/integration/targets/hcloud_ssh_key/tasks/main.yml +++ b/tests/integration/targets/hcloud_ssh_key/tasks/main.yml @@ -113,7 +113,7 @@ hcloud_server: name: "{{ hcloud_server_name }}" server_type: cx11 - image: "ubuntu-18.04" + image: "ubuntu-20.04" ssh_keys: - "{{ hcloud_ssh_key_name }}" state: started @@ -128,11 +128,23 @@ id: "{{ sshKey.hcloud_ssh_key.id }}" state: absent register: result -- name: verify absent server +- name: verify absent sshkey assert: that: - result is success +- name: test fail cleanly on double created ssh key + hcloud_ssh_key: + name: "{{ hcloud_ssh_key_name }}othername" + public_key: "{{ hcloud_ssh_key_public_key }}" + register: result + ignore_errors: yes +- name: verify failed correctly + assert: + that: + - result is failed + - 'result.msg == "SSH key with the same fingerprint already exists"' + - name: cleanup hcloud_server: name: "{{ hcloud_server_name }}" diff --git a/tests/utils/gitlab/sanity.sh b/tests/utils/gitlab/sanity.sh index 1418edc..e7f708f 100755 --- a/tests/utils/gitlab/sanity.sh +++ b/tests/utils/gitlab/sanity.sh @@ -43,5 +43,5 @@ pip install pylint # shellcheck disable=SC2086 ansible-test sanity --color -v --junit ${COVERAGE:+"$COVERAGE"} ${CHANGED:+"$CHANGED"} \ --base-branch "${base_branch}" \ - --exclude shippable.yml --exclude tests/utils/ \ + --exclude tests/utils/ \ "${options[@]}" --allow-disabled