diff --git a/changelogs/fragments/10011-github_deploy_key-check-key-present.yml b/changelogs/fragments/10011-github_deploy_key-check-key-present.yml new file mode 100644 index 0000000000..1f3857794c --- /dev/null +++ b/changelogs/fragments/10011-github_deploy_key-check-key-present.yml @@ -0,0 +1,2 @@ +bugfixes: + - "github_deploy_key - check that key really exists on 422 to avoid masking other errors (https://github.com/ansible-collections/community.general/issues/6718, https://github.com/ansible-collections/community.general/pull/10011)." diff --git a/plugins/modules/github_deploy_key.py b/plugins/modules/github_deploy_key.py index 509a67c491..2e5f9125ad 100644 --- a/plugins/modules/github_deploy_key.py +++ b/plugins/modules/github_deploy_key.py @@ -259,7 +259,12 @@ class GithubDeployKey(object): key_id = response_body["id"] self.module.exit_json(changed=True, msg="Deploy key successfully added", id=key_id) elif status_code == 422: - self.module.exit_json(changed=False, msg="Deploy key already exists") + # there might be multiple reasons for a 422 + # so we must check if the reason is that the key already exists + if self.get_existing_key(): + self.module.exit_json(changed=False, msg="Deploy key already exists") + else: + self.handle_error(method="POST", info=info) else: self.handle_error(method="POST", info=info)