From a3c5cc6773381b9842aa22cefb2a34b027cc4350 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:34:51 +0200 Subject: [PATCH] [PR #8796/e9071e98 backport][stable-8] Fix gitlab access token crash in check mode for new tokens (#8803) Fix gitlab access token crash in check mode for new tokens (#8796) Fix crash in check mode when attempting to create a new gitlab access token (cherry picked from commit e9071e9871188fc8d5d7d8ac541b1fdeda010426) Co-authored-by: Veikko Virrankoski <71337077+vvirrank@users.noreply.github.com> --- changelogs/fragments/8796-gitlab-access-token-check-mode.yml | 3 +++ plugins/modules/gitlab_group_access_token.py | 5 ++++- plugins/modules/gitlab_project_access_token.py | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/8796-gitlab-access-token-check-mode.yml diff --git a/changelogs/fragments/8796-gitlab-access-token-check-mode.yml b/changelogs/fragments/8796-gitlab-access-token-check-mode.yml new file mode 100644 index 0000000000..6585584fac --- /dev/null +++ b/changelogs/fragments/8796-gitlab-access-token-check-mode.yml @@ -0,0 +1,3 @@ +bugfixes: + - gitlab_group_access_token - fix crash in check mode caused by attempted access to a newly created access token (https://github.com/ansible-collections/community.general/pull/8796). + - gitlab_project_access_token - fix crash in check mode caused by attempted access to a newly created access token (https://github.com/ansible-collections/community.general/pull/8796). diff --git a/plugins/modules/gitlab_group_access_token.py b/plugins/modules/gitlab_group_access_token.py index 85bba205db..1db7414081 100644 --- a/plugins/modules/gitlab_group_access_token.py +++ b/plugins/modules/gitlab_group_access_token.py @@ -313,7 +313,10 @@ def main(): module.exit_json(changed=True, msg="Successfully recreated access token", access_token=gitlab_access_token.access_token_object._attrs) else: gitlab_access_token.create_access_token(group, {'name': name, 'scopes': scopes, 'access_level': access_level, 'expires_at': expires_at}) - module.exit_json(changed=True, msg="Successfully created access token", access_token=gitlab_access_token.access_token_object._attrs) + if module.check_mode: + module.exit_json(changed=True, msg="Successfully created access token", access_token={}) + else: + module.exit_json(changed=True, msg="Successfully created access token", access_token=gitlab_access_token.access_token_object._attrs) if __name__ == '__main__': diff --git a/plugins/modules/gitlab_project_access_token.py b/plugins/modules/gitlab_project_access_token.py index e692a30577..9bfbc51cc7 100644 --- a/plugins/modules/gitlab_project_access_token.py +++ b/plugins/modules/gitlab_project_access_token.py @@ -311,7 +311,10 @@ def main(): module.exit_json(changed=True, msg="Successfully recreated access token", access_token=gitlab_access_token.access_token_object._attrs) else: gitlab_access_token.create_access_token(project, {'name': name, 'scopes': scopes, 'access_level': access_level, 'expires_at': expires_at}) - module.exit_json(changed=True, msg="Successfully created access token", access_token=gitlab_access_token.access_token_object._attrs) + if module.check_mode: + module.exit_json(changed=True, msg="Successfully created access token", access_token={}) + else: + module.exit_json(changed=True, msg="Successfully created access token", access_token=gitlab_access_token.access_token_object._attrs) if __name__ == '__main__':