go.ansible#
Go package ansible that builds and runs ansible-galaxy and
ansible-playbook commands from a Config struct. It calls the Ansible
binaries on PATH; Ansible itself is not part of the module. All facts on this
page come from go.mod, ansiblePlaybook.go, example_test.go and
CHANGELOG.md in https://github.com/arillso/go.ansible.
Facts#
Property |
Value |
|---|---|
Module path |
|
Package name |
|
Latest release |
|
Go version ( |
|
Dependencies |
none outside the standard library |
API reference |
|
License |
MIT |
Installation#
go get github.com/arillso/go.ansible/v2@v2.0.0
import ansible "github.com/arillso/go.ansible/v2"
Example#
Runs Galaxy requirements and a playbook, logs the masked command lines first and
checks the Ansible exit code. Compiles against v2.0.0 and main.
package main
import (
"context"
"errors"
"log"
"os"
"time"
ansible "github.com/arillso/go.ansible/v2"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
pb := ansible.NewPlaybook()
pb.Config.Playbooks = []string{"site.yml"}
pb.Config.Inventories = []string{"inventory.yml"}
pb.Config.GalaxyFile = "requirements.yml"
pb.Config.ExtraVars = []string{"env=staging"}
pb.Config.Limit = "web"
pb.Config.VaultPassword = os.Getenv("VAULT_PASSWORD")
// Preview the command lines; secrets and extra-vars are masked.
cmds, err := pb.CommandStrings(ctx)
if err != nil {
log.Fatal(err)
}
for _, c := range cmds {
log.Println(c)
}
if err := pb.Exec(ctx); err != nil {
var ae *ansible.AnsibleError
if errors.As(err, &ae) && ae.ExitCode == ansible.ExitCodeUnreachable {
log.Fatalf("hosts unreachable: %v", ae)
}
log.Fatal(err)
}
}
More runnable examples are in example_test.go and on pkg.go.dev.
Exported API#
Identifier |
Description |
|---|---|
|
Returns a |
|
Resolves playbooks, writes temporary secret files, runs the commands in
order and removes the temporary files. Cancelling |
|
Returns the command lines |
|
Fields |
|
All options, see Config Fields. |
|
Returned (wrapped) when an Ansible command exits non-zero. Fields
|
|
|
Execution Order#
Exec runs up to four commands:
ansible --versionifShowVersionis set.ansible-galaxy role installandansible-galaxy collection installifGalaxyFileorGalaxyRequirementsFileis set. The file must exist.ansible-playbookwith allPlaybooksas positional arguments.
Before that, Exec checks:
Playbooksis not empty. Each entry is a file path, a glob pattern or a collection playbook (namespace.collection.name); paths and globs must match existing files.Each
Inventoriesentry is a readable file, or an inline inventory containing a comma (localhost,). Without inventories,ansible-playbookuses its configured inventory.PrivateKeycontains PEMBEGIN/ENDmarkers.
PrivateKey and VaultPassword are written to files with mode 0600 in
TempDir (CRLF converted to LF, trailing newline added) and removed
after the run. Errors are wrapped with %w, so
errors.As and errors.Is (including context.DeadlineExceeded) work.
Config Fields#
Each field’s doc comment names the flag or environment variable it sets.
docs/option-mapping.md in the repository groups them by mechanism.
Group |
Fields |
|---|---|
General |
|
SSH |
|
Playbook |
|
Vault |
|
Facts |
|
Galaxy |
|
Other |
|
Deprecated |
|
Note
main contains fixes not yet released in v2.0.0 (CHANGELOG.md,
section Unreleased). Among them: NoColor, StrategyPlugin,
CallbacksEnabled, PollInterval, AnyErrorsFatal and
SSHTransferMethod are passed as ANSIBLE_* environment variables;
v2.0.0 passes them as CLI flags that ansible-playbook rejects.
Development#
make test
CI calls reusable workflows from arillso/.github: ci-go and ci-lint
on pull requests, security-code and Trivy nightly, release and SBOM on tag push
(tag.yml).
See also
action.playbook - GitHub Action built on this library
docker.ansible - container image with
ansible-playbook