
Go-ansible is a Go library for executing Ansible commands (such as ansible-playbook, ansible-inventory, ansible-galaxy, and ansible) directly from Go applications. It acts as a wrapper around the Ansible CLI, enabling seamless integration of Ansible automation into Go projects.
Go-ansible was created to address the challenge of managing complex Ansible playbooks that require a large number of extra variables. Rather than reimplementing Ansible’s logic, the library provides an abstraction layer over the existing Ansible commands, streamlining their execution from a Go application.
Originally, the library was a wrapper around the ansible-playbook command and was tightly coupled with the Stevedore project. However, thanks to the contributions, feedback, and advice from the community, it has evolved into a general-purpose solution. Today, go-ansible supports the execution of various commands, including ansible-playbook, ansible, and ansible-galaxy, and allows fine-grained control over settings and output management to accommodate diverse use cases.
ansible-playbook, ansible, ansible-galaxy, and ansible-inventory, directly from Go applications.Additionally, the go-ansible library provides a set of examples that demonstrate how to use the library in distinct scenarios. It is recommended to review the examples section.
The following section walks you through an example, showing how to set up and execute an Ansible playbook step by step using go-ansible. The same approach can be applied to other Ansible commands, such as ansible or ansible-inventory. If you want to see a complete example to run an Ansible playbook from Go, check out the ansibleplaybook-simple example.
Note Before proceeding, ensure you have installed the latest version of the go-ansible library. If not, please refer to the Installation section for instructions.
To create an application that launches the ansible-playbook command, you need to create an AnsiblePlaybookCmd struct. This struct generates the Ansible command to be run. Then, you need to execute the command using an executor. In this example, you will use the DefaultExecute executor provided by the go-ansible library.
To execute ansible-playbook commands, first define the necessary connection, playbook, and privilege escalation options.
Start by creating the AnsiblePlaybookOptions struct:
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Become: true,
Connection: "local",
Inventory: "127.0.0.1,",
}
Finally, create the AnsiblePlaybookCmd struct that generates the command to execute the playbook site.yml using the ansible-playbook command:
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml", "site2.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)
Once the AnsiblePlaybookCmd is defined, provide the command to an executor to run the command.
We will use the DefaultExecute struct, provided by the go-ansible library, to execute the ansible-playbook command. It requires a Commander responsible for generating the command to be executed. In this example, you will use the AnsiblePlaybookCmd previously defined.
// PlaybookCmd is the Commander responsible for generating the command to execute
exec := execute.NewDefaultExecute(
execute.WithCmd(playbookCmd),
execute.WithErrorEnrich(playbook.NewAnsiblePlaybookErrorEnrich()),
)
Once you have defined the DefaultExecute, execute the Ansible command using the following code:
err := exec.Execute(context.Background())
if err != nil {
// Manage the error
}
By default, the DefaultExecute uses the DefaultResults struct to manage the output of the command execution. The DefaultResults struct handles the output as plain text.
──
── PLAY [all] *********************************************************************
──
── TASK [Gathering Facts] *********************************************************
── ok: [127.0.0.1]
──
── TASK [simple-ansibleplaybook] **************************************************
── ok: [127.0.0.1] => {
── "msg": "Your are running 'simple-ansibleplaybook' example"
── }
──
── PLAY RECAP *********************************************************************
── 127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
──
── Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds
⚠️ Important:
- The
masterbranch may contain unreleased or pre-released features. It is recommended to use the stable releases available in the releases page.- Major version upgrades (e.g., 1.x to 2.x) introduce breaking changes. Please review the changelog and the relevant upgrade guides before upgrading:
- Upgrade guide to 1.x
- Upgrade guide to 2.x
- Update your import paths to match the correct module version (e.g.,
github.com/apenella/go-ansible/v2for version 2.x).
To install the release candidate version:
go get github.com/apenella/go-ansible/v2@v2.4.1
To install the previous stable version:
go get github.com/apenella/go-ansible
There are a few concepts that you need to understand before using the go-ansible library. These concepts are essential to effectively use the library and to understand the examples and usage references provided in this document.
An executor is a component that executes commands and handles the results from the execution output. The library includes the DefaultExecute executor, which is a ready-to-go implementation of an executor. If the DefaultExecute does not meet your requirements, you can also create a custom executor.
To know more about the DefaultExecute, refer to that section.
A command generator or a commander is responsible for generating the command to be executed. The AnsiblePlaybookCmd and AnsibleAdhocCmd structs are examples of command generators. That concept has been introduced in the major version 2.0.0.
A results handler or a results outputer is responsible
$ claude mcp add go-ansible \
-- python -m otcore.mcp_server <graph>