diff --git a/changelogs/fragments/11851-gitlab-project-members-ambiguous.yml b/changelogs/fragments/11851-gitlab-project-members-ambiguous.yml new file mode 100644 index 0000000000..f622144f18 --- /dev/null +++ b/changelogs/fragments/11851-gitlab-project-members-ambiguous.yml @@ -0,0 +1,4 @@ +bugfixes: + - gitlab_project_members - fail with a clear error when multiple projects match the given name, instead of silently operating on the first result + (https://github.com/ansible-collections/community.general/issues/2767, + https://github.com/ansible-collections/community.general/pull/11851). diff --git a/plugins/modules/gitlab_project_members.py b/plugins/modules/gitlab_project_members.py index 1bd759abce..4c3c9c080e 100644 --- a/plugins/modules/gitlab_project_members.py +++ b/plugins/modules/gitlab_project_members.py @@ -179,8 +179,15 @@ class GitLabProjectMembers: return project_exists.id except gitlab.exceptions.GitlabGetError: project_exists = self._gitlab.projects.list(search=project_name, all=False) - if project_exists: + if len(project_exists) == 1: return project_exists[0].id + if len(project_exists) > 1: + self._module.fail_json( + msg=( + f"More than one project matches '{project_name}'. " + "Use the full path ('group/project') to disambiguate." + ) + ) def get_user_id(self, gitlab_user): user_exists = self._gitlab.users.list(username=gitlab_user, all=False)