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

homebrew: now breaks when passing a `name` containing the tap (`tapname/formulaname`) #8063

Open AliSoftware opened 8 months ago

AliSoftware commented 8 months ago

Summary

Before https://github.com/ansible-collections/community.general/pull/7870 landed, providing a full name (that contains /) for a formula to install via community.general.homebrew worked without any issue, and automatically installed the corresponding tap first if needed (like e.g. the brew install buildkite/buildkite/buildkite-agent command line would).

But since that change, the plugin now crashes when trying to call brew info --json=v2 {{name}} here as, in such cases, that command would error with:

Error: No available formula or cask with the name "buildkite/buildkite/buildkite-agent".
Please tap it and then try again: brew tap buildkite/buildkite

This is a change in behavior that is a bit unfortunate and broke a couple of our playbooks.


PS: Even if we can kinda work around it by adding community.general.homebrew_tap in our playbook before calling community.general.homebrew, that means we'd have to do the work of extracting the tap name from the full name string we pass in our playbooks; this was not necessary before, and could become cumbersome to do it ourselves (in our playbook, as opposed to have the plugin do it and keep the previous behavior) especially if we passed multiple formulae with compound names as part of the name attribute, or when the brew formulas we want our playbook to install are provided more dynamically or via external variables.

Issue Type

Bug Report

Component Name

homebrew

Ansible Version

$ ansible --version

ansible [core 2.16.4]
  config file = None
  configured module search path = ['/Users/olivierhalligon/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/9.3.0/libexec/lib/python3.12/site-packages/ansible
  ansible collection location = /Users/olivierhalligon/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/opt/homebrew/Cellar/ansible/9.3.0/libexec/bin/python)
  jinja version = 3.1.3
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general

# /opt/homebrew/Cellar/ansible/9.3.0/libexec/lib/python3.12/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 8.4.0  

Configuration

$ ansible-config dump --only-changed

CONFIG_FILE() = None
PAGER(env: PAGER) = less

OS / Environment

macOS 14.3.1

$ uname -a

Darwin a8c-MBP-M1.local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64

Steps to Reproduce

- name: Install Buildkite Agent
  community.general.homebrew:
    name: buildkite/buildkite/buildkite-agent
    state: latest

Or with some dynamic values

- name: Install homebrew packages
  community.general.homebrew:
    name: '{{ manifest.brew }}'
    state: latest

Expected Results

The fomulae gets installed or updated even if they are provided with their full name (tap+name) and the tap has not been installed yet on the target machine—just like before that recent change, or like when running brew install from the command line.

changed: [default] => {
    "changed": true,
    "changed_pkgs": [
        "buildkite/buildkite/buildkite-agent"
    ],
    "invocation": {
        "module_args": {
            "install_options": [],
            "name": [
                "buildkite/buildkite/buildkite-agent"
            ],
            "path": "/usr/local/bin:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin",
            "state": "latest",
            "update_homebrew": false,
            "upgrade_all": false,
            "upgrade_options": []
        }
    },
    "msg": "Package upgraded: buildkite/buildkite/buildkite-agent",
    "unchanged_pkgs": []
}

Actual Results

JSON parsing failure — due to brew info --json=v2 erroring if passed a full formula name while the tap it belongs to wasn't installed yet.

    hostmgr-builder.macos-image: TASK [buildkite : Install Buildkite Agent] *************************************
    hostmgr-builder.macos-image: An exception occurred during task execution. To see the full traceback, use -vvv. The error was: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    hostmgr-builder.macos-image: fatal: [default]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.64.11 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/Users/builder/.ansible/tmp/ansible-tmp-1709731907.861258-32966-114558580960583/AnsiballZ_homebrew.py\", line 107, in <module>\r\n    _ansiballz_main()\r\n  File \"/Users/builder/.ansible/tmp/ansible-tmp-1709731907.861258-32966-114558580960583/AnsiballZ_homebrew.py\", line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/Users/builder/.ansible/tmp/ansible-tmp-1709731907.861258-32966-114558580960583/AnsiballZ_homebrew.py\", line 47, in invoke_module\r\n    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.homebrew', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.homebrew', _modlib_path=modlib_path),\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py\", line 210, in run_module\r\n    return _run_module_code(code, init_globals, run_name, mod_spec)\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py\", line 97, in _run_module_code\r\n    _run_code(code, mod_globals, init_globals,\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py\", line 87, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 982, in <module>\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 967, in main\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 464, in run\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 539, in _run\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 727, in _upgrade_packages\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 667, in _upgrade_current_package\r\n  File \"/var/folders/gt/3grbfxs13w53jdjmg4f_c6_r0000gn/T/ansible_community.general.homebrew_payload_5rrckkf_/ansible_community.general.homebrew_payload.zip/ansible_collections/community/general/plugins/modules/homebrew.py\", line 491, in _current_package_is_installed\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py\", line 346, in loads\r\n    return _default_decoder.decode(s)\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py\", line 337, in decode\r\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\r\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py\", line 355, in raw_decode\r\n    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\r\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
    hostmgr-builder.macos-image:
    hostmgr-builder.macos-image: PLAY RECAP *********************************************************************
    hostmgr-builder.macos-image: default                    : ok=14   changed=11   unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
    hostmgr-builder.macos-image:
==> hostmgr-builder.macos-image: Provisioning step had errors: Running the cleanup provisioner, if present...
Build 'hostmgr-builder.macos-image' errored after 8 minutes 15 seconds: Error executing Ansible: Non-zero exit status: exit status 2

Code of Conduct

ansibullbot commented 8 months 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 8 months ago

cc @Akasurde @danieljaouen @indrajitr @kyleabenson @martinm82 click here for bot help

AliSoftware commented 8 months ago

!component =plugins/modules/homebrew.py

ansibullbot commented 8 months 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 8 months ago

cc @andrew-d click here for bot help

felixfontein commented 8 months ago

CC @joao-victor-silva