GO Ansible is a Go module designed to programmatically run Ansible playbooks on Linux systems. It supports:
- Automated Playbook Resolution: Resolves file patterns (glob) and validates playbook existence.
- Temporary File Management: Manages temporary files (SSH private keys and Vault passwords) securely.
- Galaxy Integration: Installs roles and collections from Ansible Galaxy with extensive configuration.
- Flexible Configuration: Customizes Ansible commands (inventories, extra vars, SSH/user options, verbosity).
- Enhanced Environment Variables: Sets variables like
ANSIBLE_CONFIGbased on provided configurations. - Debug and Traceability: Provides detailed command tracing in debug mode for easier troubleshooting.
- Playbook Resolution: Supports file names and glob patterns for playbook identification.
- Temporary Files: Manages SSH keys and Vault passwords, cleaning them up automatically.
- Inventory Management: Supports inline (
localhost,) and file-based inventories.
- Command Building: Constructs commands with options (check/diff modes, user settings, forks).
- Extra Vars Management: Passes multiple variables to playbooks with
--extra-vars. - Verbose Logging: Offers configurable verbosity (up to
-vvvv) for detailed logs.
- Roles and Collections: Installs Galaxy roles and collections using a configuration file.
- Customization: API keys, server URLs, certificate ignoring, timeouts, dependencies handling.
- Upgrades: Provides options to upgrade existing Galaxy collections.
- Command Tracing: Debug mode prints every executed command for tracing.
- Context-Based Execution: Uses Go’s
context.Contextfor command management and cancellation. - Error Reporting: Wraps errors with contextual information using
github.com/pkg/errors.
Ensure you have Go (version 1.23 or higher) installed, then run:
go mod downloadThe module serves as a foundation for executing Ansible playbooks in applications. Example:
- Create and Configure a Playbook Instance:
package main
import (
"context"
"log"
"github.com/arillso/go.ansible/ansible"
)
func main() {
pb := ansible.NewPlaybook()
pb.Config.Playbooks = []string{"site.yml"}
pb.Config.Inventories = []string{"localhost,"}
pb.Config.PrivateKey = "your ssh private key..."
pb.Config.VaultPassword = "your vault password..."
if err := pb.Exec(context.Background()); err != nil {
log.Fatalf("Execution failed: %v", err)
}
}- Command Construction and Execution:
The module automatically constructs commands and manages dependencies (like Galaxy installations).
The repository includes tests covering functionalities such as playbook resolution, temporary files, and extra vars:
go test -v ./...- Continuous Integration: GitHub Actions automatically run tests on push and pull requests.
- Code Quality: Makefile targets for formatting and linting ensure best practices.
- Pre-commit Hooks: Automatically fix issues like trailing whitespace and line endings.
Licensed under the MIT License.
(c) 2022, Arillso