ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
825 stars 1.53k forks source link

gitlab_runner: check mode is throwing an Exception #8854

Open marcandre-larochelle-bell opened 1 month ago

marcandre-larochelle-bell commented 1 month ago

Summary

Check mode fails due to a code issue in the module regarding the check mode.

Line 519 expects the object called gitlab_runner.runner_object to be an object, however it receives a boolean due to an assignment in the function create_or_update_runner at line 325, when calling the create_runner function there is check for "check_mode" performed and it returns true at line 346 instead of a proper runner object, which then breaks at line 519.

main where the exception happens

        if gitlab_runner.create_or_update_runner(runner_description, runner_values):
->          module.exit_json(changed=True, runner=gitlab_runner.runner_object._attrs,                                 # Faulty attribute
                             msg="Successfully created or updated the runner %s" % runner_description)

create_or_update_runner function (from line 325 to the end of it)

->          runner = self.create_runner(arguments)                                                                                 # Assignment happens here
            changed = True
        else:
            changed, runner = self.update_runner(self.runner_object, arguments)
            if changed:
                if self._module.check_mode:
                    self._module.exit_json(changed=True, msg="Successfully updated the runner %s" % description)

                try:
                    runner.save()
                except Exception as e:
                    self._module.fail_json(msg="Failed to update runner: %s " % to_native(e))

->      self.runner_object = runner                                                                                                        # And then here
        return changed

create_runner function (see the return performed for the check_mode)

    def create_runner(self, arguments):
        if self._module.check_mode:
->          return True                                                                                                                            # Problematic code

        try:
            if arguments.get('token') is not None:
                runner = self._gitlab.runners.create(arguments)
            elif LooseVersion(gitlab.__version__) < LooseVersion('4.0.0'):
                self._module.fail_json(msg="New runner creation workflow requires python-gitlab 4.0.0 or higher")
            else:
                runner = self._gitlab.user.runners.create(arguments)
        except (gitlab.exceptions.GitlabCreateError) as e:
            self._module.fail_json(msg="Failed to create runner: %s " % to_native(e))

        return runner

Issue Type

Bug Report

Component Name

community.general.gitlab_runner module

Ansible Version

2.13.2

Community.general Version

9.4.0

Configuration

No response

OS / Environment

RHEL 9

Steps to Reproduce

Expected Results

Check mode doesn't fail

Actual Results

Stack trace:

The full traceback is:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.gitlab_runner', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.gitlab_runner', _modlib_path=modlib_path),
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/ansible_community.general.gitlab_runner_payload_ing1rl38/ansible_community.general.gitlab_runner_payload.zip/ansible_collections/community/general/plugins/modules/gitlab_runner.py", line 527, in <module>
  File "/tmp/ansible_community.general.gitlab_runner_payload_ing1rl38/ansible_community.general.gitlab_runner_payload.zip/ansible_collections/community/general/plugins/modules/gitlab_runner.py", line 519, in main
AttributeError: 'bool' object has no attribute '_attrs'
fatal: [<REDACTED> -> localhost]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1726081189.0393121-194-134545082989152/AnsiballZ_gitlab_runner.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.gitlab_runner', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.gitlab_runner', _modlib_path=modlib_path),\n  File \"<frozen runpy>\", line 226, in run_module\n  File \"<frozen runpy>\", line 98, in _run_module_code\n  File \"<frozen runpy>\", line 88, in _run_code\n  File \"/tmp/ansible_community.general.gitlab_runner_payload_ing1rl38/ansible_community.general.gitlab_runner_payload.zip/ansible_collections/community/general/plugins/modules/gitlab_runner.py\", line 527, in <module>\n  File \"/tmp/ansible_community.general.gitlab_runner_payload_ing1rl38/ansible_community.general.gitlab_runner_payload.zip/ansible_collections/community/general/plugins/modules/gitlab_runner.py\", line 519, in main\nAttributeError: 'bool' object has no attribute '_attrs'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

Code of Conduct

ansibullbot commented 1 month ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 1 month ago

cc @Lunik @SamyCoenen @Shaps @lgatellier @marwatk @metanovii @nejch @scodeman @sh0shin @suukit @waheedi @zanssa click here for bot help