From 1b0b8d5cc1f11892a24746f42be21c8a84227527 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 19 Apr 2026 22:42:37 +1200 Subject: [PATCH] gitlab_project_variable - use find_project() for graceful error handling (#11878) * gitlab_project_variable - use find_project() for consistent error handling Replace the bare projects.get() call in GitlabProjectVariables.get_project() with find_project() from module_utils/gitlab, which all other GitLab modules already use. This ensures a graceful fail_json (with a clear error message) when the project is not found, rather than an unhandled GitlabGetError propagating as a module traceback. Co-Authored-By: Claude Sonnet 4.6 (1M context) * Add changelog fragment for PR 11878 Co-Authored-By: Claude Sonnet 4.6 (1M context) * minor adjustment in f-string --------- Co-authored-by: Claude Sonnet 4.6 (1M context) --- .../11878-gitlab_project_variable-find_project.yml | 5 +++++ plugins/modules/gitlab_project_variable.py | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/11878-gitlab_project_variable-find_project.yml diff --git a/changelogs/fragments/11878-gitlab_project_variable-find_project.yml b/changelogs/fragments/11878-gitlab_project_variable-find_project.yml new file mode 100644 index 0000000000..68c35f82d7 --- /dev/null +++ b/changelogs/fragments/11878-gitlab_project_variable-find_project.yml @@ -0,0 +1,5 @@ +bugfixes: + - gitlab_project_variable - use ``find_project()`` from module utils for project lookup, consistent + with all other GitLab modules in the collection + (https://github.com/ansible-collections/community.general/issues/3157, + https://github.com/ansible-collections/community.general/pull/11878). diff --git a/plugins/modules/gitlab_project_variable.py b/plugins/modules/gitlab_project_variable.py index 642b22f92a..2eca64da3e 100644 --- a/plugins/modules/gitlab_project_variable.py +++ b/plugins/modules/gitlab_project_variable.py @@ -240,6 +240,7 @@ from ansible.module_utils.basic import AnsibleModule from ansible_collections.community.general.plugins.module_utils.gitlab import ( auth_argument_spec, filter_returned_variables, + find_project, gitlab_authentication, list_all_kwargs, vars_to_variables, @@ -249,11 +250,14 @@ from ansible_collections.community.general.plugins.module_utils.gitlab import ( class GitlabProjectVariables: def __init__(self, module, gitlab_instance): self.repo = gitlab_instance - self.project = self.get_project(module.params["project"]) self._module = module + self.project = self.get_project(module.params["project"]) def get_project(self, project_name): - return self.repo.projects.get(project_name) + project = find_project(self.repo, project_name) + if project is None: + self._module.fail_json(msg=f"Project {project_name} not found or insufficient permissions.") + return project def list_all_project_variables(self): return list(self.project.variables.list(**list_all_kwargs))