How apenella/go-ansible works
Purpose and entry points
go-ansible is a Go v2 library that constructs and runs installed Ansible CLI programs; it is not a standalone application or server. Consumers compose command generators with executors, usually beginning with pkg/playbook/ansiblePlaybookCmd.go and pkg/execute/defaultExecute.go. Ready-made execution facades also exist for playbooks, ad-hoc commands, and inventory.
Command-generation boundary
Command packages translate typed Go options into argv for external Ansible binaries, separating CLI construction from process execution:
• Playbooks: pkg/playbook/ansiblePlaybookCmd.go, pkg/playbook/ansiblePlaybookOptions.go
• Ad-hoc ansible: pkg/adhoc/ansibleAdhocCmd.go, pkg/adhoc/ansibleAdhocOptions.go
• Inventory: pkg/inventory/ansibleInventoryCmd.go
• Galaxy role/collection installation: pkg/galaxy/role/install/ansibleGalaxyRoleInstallCmd.go, pkg/galaxy/collection/install/ansibleGalaxyCollectionInstallCmd.go
These components implement the shared commander contract defined under pkg/execute/interface.go.
Execution pipeline
DefaultExecute is the central control path: it obtains argv from a commander, starts the process with context support, connects stdout/stderr, delegates output consumption, and can enrich errors. Paths: pkg/execute/defaultExecute.go, pkg/execute/defaultExecuteOptions.go, pkg/execute/interface.go.
The OS-process boundary is abstracted behind Executabler/Cmder, permitting mocked or custom runners rather than coupling command generators to os/exec: pkg/execute/exec/interface.go, pkg/execute/exec/exec.go, pkg/execute/exec/cmd.go. Execution supports run-directory and environment injection; cancellation propagates through Go context.Context.
Output and callback adaptation
Result handlers consume process output and write transformed representations. Default line-oriented output is in pkg/execute/result/default/defaultResult.go; JSON and Ansible ansible.posix.jsonl parsing live in pkg/execute/result/json/JSONStdoutCallbackResults.go and pkg/execute/result/json/JSONLEventStdoutCallbackResults.go. Transformers provide a narrow output-customization seam at pkg/execute/result/transformer/transformer.go.
Stdout-callback decorators configure ANSIBLE_STDOUT_CALLBACK plus the matching result handler, bridging Ansible callback plugin protocols to Go consumers: pkg/execute/stdoutcallback/interface.go, pkg/execute/stdoutcallback/json.go, pkg/execute/stdoutcallback/ansiblePosixJsonl.go.
Execution decorators and orchestration
Execution behavior is layered through executor-compatible decorators:
• Ansible configuration settings become process environment variables: pkg/execute/configuration/ansibleConfiguration.go, pkg/execute/configuration/ansibleWithConfigurationSettingsExecute.go.
• Timing wraps any executor: pkg/execute/measure/executorTimeMeasurement.go.
• Sequential workflows run executors in order, stopping on failure by default or aggregating errors when configured to continue: pkg/execute/workflow/workflowExecute.go.
This makes Galaxy installation followed by playbook execution a composable workflow rather than a special control path.
Secrets and error semantics
Playbook execution can add Ansible-specific exit-code context through pkg/playbook/ansiblePlaybookErrorEnrich.go. Vault support is local value encryption, not vault-server integration: pkg/vault/variableVaulter.go and pkg/vault/encrypt/encryptString.go use password providers from environment, file, literal text, or ordered resolution (pkg/vault/password/resolve/readPasswordResolve.go).
Runtime and integration environment
The runtime requires Go applications plus reachable Ansible CLI binaries (ansible-playbook, ansible, ansible-inventory, ansible-galaxy); Ansible itself may contact SSH targets and Ansible Galaxy. Docker Compose-based examples provide disposable Ansible and SSH environments, e.g. examples/ansibleplaybook-ssh/docker-compose.yaml and examples/ansibleplaybook-docker-execution/docker/runner/Dockerfile. Container users must handle child-process reaping/init correctly.
Main entry points: defaultExecute.go DefaultExecute · execute/interface.go Execution contracts · defaultResult.go Result handlers · stdoutcallback/interface.go Stdout callback decorators
Components 19
Command generation
Typed options are translated into Ansible CLI argv.
- Playbook command · Builds ansible-playbook argv from typed options.
pkg/playbook/ansiblePlaybookCmd.go - Ad-hoc command · Builds ansible argv.
pkg/adhoc/ansibleAdhocCmd.go - Inventory command · Builds ansible-inventory argv.
pkg/inventory/ansibleInventoryCmd.go - Galaxy installers · Builds role and collection installation commands.
pkg/galaxy/role/install/ansibleGalaxyRoleInstallCmd.go - Playbook error enrichment · Adds Ansible-specific exit-code context.
pkg/playbook/ansiblePlaybookErrorEnrich.go
Execution pipeline
Composable execution, process, output, and workflow layers.
- Execution contracts · Commander and executor compatibility boundary.
pkg/execute/interface.go - DefaultExecute · Coordinates argv, process launch, output, and error enrichment.
pkg/execute/defaultExecute.go - Process adapter · Cmder and Executabler isolate os/exec and support custom runners.
pkg/execute/exec/interface.go - Context-bound process · Receives environment and run-directory injection.
pkg/execute/exec/exec.go - Result handlers · Default lines plus JSON and JSONL callback parsing.
pkg/execute/result/default/defaultResult.go - Output transformers · Customizes consumed output representations.
pkg/execute/result/transformer/transformer.go - Stdout callback decorators · Sets ANSIBLE_STDOUT_CALLBACK and selects matching parsers.
pkg/execute/stdoutcallback/interface.go - Configuration decorator · Converts Ansible settings into process environment variables.
pkg/execute/configuration/ansibleWithConfigurationSettingsExecute.go - Workflow executor · Runs executors sequentially, stopping or aggregating failures.
pkg/execute/workflow/workflowExecute.go
Vault support
Local Ansible-vault value encryption.
- Variable vaulter · Encrypts local values using resolved passwords.
pkg/vault/variableVaulter.go - Password resolution · Resolves environment, file, literal, or ordered passwords.
pkg/vault/password/resolve/readPasswordResolve.go
External runtime
Installed Ansible programs and their integrations.
- Ansible CLI binaries · ansible-playbook, ansible, ansible-inventory, and ansible-galaxy.
- Ansible integrations · SSH targets and Ansible Galaxy may be contacted by the CLI.
Other components
- Go consumer · Composes command generators, executors, and decorators.
Connections 21
- Go consumer → Playbook command: builds
- Go consumer → Ad-hoc command: builds
- Go consumer → Inventory command: builds
- Go consumer → Workflow executor: composes
- Playbook command → Execution contracts: implements commander
- Ad-hoc command → Execution contracts: implements commander
- Inventory command → Execution contracts: implements commander
- Galaxy installers → Execution contracts: implements commander
- DefaultExecute → Execution contracts: uses commander
- DefaultExecute → Process adapter: launches through
- Process adapter → Context-bound process: creates
- Context-bound process → Ansible CLI binaries: executes
- DefaultExecute → Result handlers: streams output to
- Result handlers → Output transformers: applies
- Stdout callback decorators → DefaultExecute: decorates
- Stdout callback decorators → Result handlers: selects parser
- Configuration decorator → DefaultExecute: decorates
- Workflow executor → DefaultExecute: runs sequentially
- Playbook error enrichment → DefaultExecute: enriches failures
- Variable vaulter → Password resolution: resolves password
- Ansible CLI binaries → Ansible integrations: contacts