# apenella/go-ansible architecture

> Architecture diagram of the GitHub repository apenella/go-ansible, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apenella/go-ansible
- Repository: https://github.com/apenella/go-ansible

## Overview

#### 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: [`pkg/execute/defaultExecute.go`](https://github.com/apenella/go-ansible/blob/master/pkg/execute/defaultExecute.go) (DefaultExecute), [`pkg/execute/interface.go`](https://github.com/apenella/go-ansible/blob/master/pkg/execute/interface.go) (Execution contracts), [`pkg/execute/result/default/defaultResult.go`](https://github.com/apenella/go-ansible/blob/master/pkg/execute/result/default/defaultResult.go) (Result handlers), [`pkg/execute/stdoutcallback/interface.go`](https://github.com/apenella/go-ansible/blob/master/pkg/execute/stdoutcallback/interface.go) (Stdout callback decorators)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_commands["Command generation"]
  node_playbook_cmd["Playbook command<br/>commander"]
  node_adhoc_cmd["Ad-hoc command<br/>commander<br/>[ansibleAdhocCmd.go]"]
  node_inventory_cmd["Inventory command<br/>commander"]
  node_galaxy_install["Galaxy installers<br/>commander"]
  node_playbook_errors["Playbook error enrichment<br/>error adapter"]
end

subgraph group_execution["Execution pipeline"]
  node_contracts{{"Execution contracts<br/>interfaces<br/>[interface.go]"}}
  node_default_execute["DefaultExecute<br/>executor<br/>[defaultExecute.go]"]
  node_process_adapter["Process adapter<br/>exec abstraction<br/>[interface.go]"]
  node_process["Context-bound process<br/>process runtime<br/>[exec.go]"]
  node_result_handlers["Result handlers<br/>output consumers<br/>[defaultResult.go]"]
  node_transformers["Output transformers<br/>extension seam<br/>[transformer.go]"]
  node_stdout_callbacks["Stdout callback decorators<br/>executor decorator<br/>[interface.go]"]
  node_configuration["Configuration decorator<br/>executor decorator"]
  node_workflow["Workflow executor<br/>orchestrator<br/>[workflowExecute.go]"]
end

subgraph group_vault["Vault support"]
  node_variable_vaulter["Variable vaulter<br/>vault API<br/>[variableVaulter.go]"]
  node_password_resolution["Password resolution<br/>password provider"]
end

subgraph group_runtime["External runtime"]
  node_ansible_cli{{"Ansible CLI binaries<br/>external programs"}}
  node_ansible_targets(("Ansible integrations<br/>external systems"))
end

node_consumer(("Go consumer<br/>library user"))

node_consumer -->|"builds"| node_playbook_cmd
node_consumer -->|"builds"| node_adhoc_cmd
node_consumer -->|"builds"| node_inventory_cmd
node_consumer -->|"composes"| node_workflow
node_playbook_cmd -->|"implements commander"| node_contracts
node_adhoc_cmd -->|"implements commander"| node_contracts
node_inventory_cmd -->|"implements commander"| node_contracts
node_galaxy_install -->|"implements commander"| node_contracts
node_default_execute -->|"uses commander"| node_contracts
node_default_execute -->|"launches through"| node_process_adapter
node_process_adapter -->|"creates"| node_process
node_process -->|"executes"| node_ansible_cli
node_default_execute -->|"streams output to"| node_result_handlers
node_result_handlers -->|"applies"| node_transformers
node_stdout_callbacks -->|"decorates"| node_default_execute
node_stdout_callbacks -->|"selects parser"| node_result_handlers
node_configuration -->|"decorates"| node_default_execute
node_workflow -->|"runs sequentially"| node_default_execute
node_playbook_errors -.->|"enriches failures"| node_default_execute
node_variable_vaulter -->|"resolves password"| node_password_resolution
node_ansible_cli -->|"contacts"| node_ansible_targets

click node_playbook_cmd "https://github.com/apenella/go-ansible/blob/master/pkg/playbook/ansiblePlaybookCmd.go"
click node_adhoc_cmd "https://github.com/apenella/go-ansible/blob/master/pkg/adhoc/ansibleAdhocCmd.go"
click node_inventory_cmd "https://github.com/apenella/go-ansible/blob/master/pkg/inventory/ansibleInventoryCmd.go"
click node_galaxy_install "https://github.com/apenella/go-ansible/blob/master/pkg/galaxy/role/install/ansibleGalaxyRoleInstallCmd.go"
click node_contracts "https://github.com/apenella/go-ansible/blob/master/pkg/execute/interface.go"
click node_default_execute "https://github.com/apenella/go-ansible/blob/master/pkg/execute/defaultExecute.go"
click node_process_adapter "https://github.com/apenella/go-ansible/blob/master/pkg/execute/exec/interface.go"
click node_process "https://github.com/apenella/go-ansible/blob/master/pkg/execute/exec/exec.go"
click node_result_handlers "https://github.com/apenella/go-ansible/blob/master/pkg/execute/result/default/defaultResult.go"
click node_transformers "https://github.com/apenella/go-ansible/blob/master/pkg/execute/result/transformer/transformer.go"
click node_stdout_callbacks "https://github.com/apenella/go-ansible/blob/master/pkg/execute/stdoutcallback/interface.go"
click node_configuration "https://github.com/apenella/go-ansible/blob/master/pkg/execute/configuration/ansibleWithConfigurationSettingsExecute.go"
click node_workflow "https://github.com/apenella/go-ansible/blob/master/pkg/execute/workflow/workflowExecute.go"
click node_playbook_errors "https://github.com/apenella/go-ansible/blob/master/pkg/playbook/ansiblePlaybookErrorEnrich.go"
click node_variable_vaulter "https://github.com/apenella/go-ansible/blob/master/pkg/vault/variableVaulter.go"
click node_password_resolution "https://github.com/apenella/go-ansible/blob/master/pkg/vault/password/resolve/readPasswordResolve.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_playbook_cmd,node_adhoc_cmd,node_inventory_cmd,node_galaxy_install,node_playbook_errors toneBlue
class node_contracts,node_default_execute,node_process_adapter,node_process,node_result_handlers,node_transformers,node_stdout_callbacks,node_configuration,node_workflow toneAmber
class node_variable_vaulter,node_password_resolution toneMint
class node_ansible_cli,node_ansible_targets toneRose
class node_consumer toneNeutral
```

## Components

### Command generation

Typed options are translated into Ansible CLI argv.

- [Playbook command](https://github.com/apenella/go-ansible/blob/master/pkg/playbook/ansiblePlaybookCmd.go): commander · Builds ansible-playbook argv from typed options. · `pkg/playbook/ansiblePlaybookCmd.go`
- [Ad-hoc command](https://github.com/apenella/go-ansible/blob/master/pkg/adhoc/ansibleAdhocCmd.go): commander · Builds ansible argv. · `pkg/adhoc/ansibleAdhocCmd.go`
- [Inventory command](https://github.com/apenella/go-ansible/blob/master/pkg/inventory/ansibleInventoryCmd.go): commander · Builds ansible-inventory argv. · `pkg/inventory/ansibleInventoryCmd.go`
- [Galaxy installers](https://github.com/apenella/go-ansible/blob/master/pkg/galaxy/role/install/ansibleGalaxyRoleInstallCmd.go): commander · Builds role and collection installation commands. · `pkg/galaxy/role/install/ansibleGalaxyRoleInstallCmd.go`
- [Playbook error enrichment](https://github.com/apenella/go-ansible/blob/master/pkg/playbook/ansiblePlaybookErrorEnrich.go): error adapter · Adds Ansible-specific exit-code context. · `pkg/playbook/ansiblePlaybookErrorEnrich.go`

### Execution pipeline

Composable execution, process, output, and workflow layers.

- [Execution contracts](https://github.com/apenella/go-ansible/blob/master/pkg/execute/interface.go): interfaces · Commander and executor compatibility boundary. · `pkg/execute/interface.go`
- [DefaultExecute](https://github.com/apenella/go-ansible/blob/master/pkg/execute/defaultExecute.go): executor · Coordinates argv, process launch, output, and error enrichment. · `pkg/execute/defaultExecute.go`
- [Process adapter](https://github.com/apenella/go-ansible/blob/master/pkg/execute/exec/interface.go): exec abstraction · Cmder and Executabler isolate os/exec and support custom runners. · `pkg/execute/exec/interface.go`
- [Context-bound process](https://github.com/apenella/go-ansible/blob/master/pkg/execute/exec/exec.go): process runtime · Receives environment and run-directory injection. · `pkg/execute/exec/exec.go`
- [Result handlers](https://github.com/apenella/go-ansible/blob/master/pkg/execute/result/default/defaultResult.go): output consumers · Default lines plus JSON and JSONL callback parsing. · `pkg/execute/result/default/defaultResult.go`
- [Output transformers](https://github.com/apenella/go-ansible/blob/master/pkg/execute/result/transformer/transformer.go): extension seam · Customizes consumed output representations. · `pkg/execute/result/transformer/transformer.go`
- [Stdout callback decorators](https://github.com/apenella/go-ansible/blob/master/pkg/execute/stdoutcallback/interface.go): executor decorator · Sets ANSIBLE_STDOUT_CALLBACK and selects matching parsers. · `pkg/execute/stdoutcallback/interface.go`
- [Configuration decorator](https://github.com/apenella/go-ansible/blob/master/pkg/execute/configuration/ansibleWithConfigurationSettingsExecute.go): executor decorator · Converts Ansible settings into process environment variables. · `pkg/execute/configuration/ansibleWithConfigurationSettingsExecute.go`
- [Workflow executor](https://github.com/apenella/go-ansible/blob/master/pkg/execute/workflow/workflowExecute.go): orchestrator · Runs executors sequentially, stopping or aggregating failures. · `pkg/execute/workflow/workflowExecute.go`

### Vault support

Local Ansible-vault value encryption.

- [Variable vaulter](https://github.com/apenella/go-ansible/blob/master/pkg/vault/variableVaulter.go): vault API · Encrypts local values using resolved passwords. · `pkg/vault/variableVaulter.go`
- [Password resolution](https://github.com/apenella/go-ansible/blob/master/pkg/vault/password/resolve/readPasswordResolve.go): password provider · Resolves environment, file, literal, or ordered passwords. · `pkg/vault/password/resolve/readPasswordResolve.go`

### External runtime

Installed Ansible programs and their integrations.

- **Ansible CLI binaries**: external programs · ansible-playbook, ansible, ansible-inventory, and ansible-galaxy.
- **Ansible integrations**: external systems · SSH targets and Ansible Galaxy may be contacted by the CLI.

### Other components

- **Go consumer**: library user · Composes command generators, executors, and decorators.

## Connections

- 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
