community.general.golang_package module – Manage Go packages with go install

Note

This module is part of the community.general collection (version 13.1.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: community.general.golang_package.

New in community.general 13.1.0

Synopsis

  • Manage Go packages with go install.

  • Packages are installed as binaries into the Go binary directory (GOBIN).

  • Uninstalling a package removes its binary from GOBIN.

Requirements

The below requirements are needed on the host that executes this module.

  • Go >= 1.16

Parameters

Parameter

Comments

executable

path

Path to the go binary.

If not specified, the module looks for go in PATH.

name

list / elements=string / required

The name of a Go package to install.

Must be a full Go package path, for example golang.org/x/tools/cmd/stringer.

A version can be specified inline using the @ suffix, for example golang.org/x/tools/cmd/stringer@v0.29.0. This allows pinning different versions when installing multiple packages.

The inline @version syntax cannot be combined with the version parameter.

state

string

The desired state of the Go package.

Choices:

  • "present" ← (default)

  • "absent"

  • "latest"

version

string

The version to install, for example v1.55.2.

The version must include the v prefix as required by Go module versioning.

Can only be used when name contains a single package without an inline @version.

When not specified and state=present, the latest version is installed for new packages.

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: none

Returns details on what has changed (or possibly needs changing in check_mode), when in diff mode.

Notes

Note

  • The Go binary directory is determined by go env GOBIN. To customize the install location, set the GOBIN environment variable using the task’s environment directive.

  • Go does not provide a native uninstall command. When state=absent, the module removes the binary file from GOBIN directly.

Examples

- name: Install stringer
  community.general.golang_package:
    name: golang.org/x/tools/cmd/stringer

- name: Install golangci-lint at a specific version
  community.general.golang_package:
    name: github.com/golangci/golangci-lint/cmd/golangci-lint
    version: v1.55.2

- name: Install multiple packages at specific versions
  community.general.golang_package:
    name:
      - golang.org/x/tools/cmd/stringer@v0.29.0
      - github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2

- name: Install multiple packages
  community.general.golang_package:
    name:
      - golang.org/x/tools/cmd/stringer
      - golang.org/x/tools/cmd/goimports

- name: Remove stringer
  community.general.golang_package:
    name: golang.org/x/tools/cmd/stringer
    state: absent

- name: Update golangci-lint to the latest version
  community.general.golang_package:
    name: github.com/golangci/golangci-lint/cmd/golangci-lint
    state: latest

- name: Install into a custom directory
  community.general.golang_package:
    name: golang.org/x/tools/cmd/stringer
  environment:
    GOBIN: /usr/local/bin

Authors

  • Shreyash Bhosale (@shrbhosa)