Categorygithub.com/paquesid/go-ansible
modulepackage
0.4.1
Repository: https://github.com/paquesid/go-ansible.git
Documentation: pkg.go.dev

# README

go-ansible

Go-ansible is a package for running Ansible playbooks from Golang. It only supports to run ansible-playbook with the most of its options.

To run a ansible-playbook command you must define three objectes:

  • AnsiblePlaybookCmd object is the main object which defines the ansible-playbook command and how to execute it.
  • AnsiblePlaybookOptions object has those parameters described on Options section within ansible-playbook's man page, and which defines how should be the ansible-playbook execution behavior and where to find execution configuration
  • AnsiblePlaybookConnectionOptions object has those parameters described on Connections Options section within ansible-playbook's man page, and which defines how to connect to hosts.

Executor

Go-ansible package has its own and default executor implementation which runs the ansible-playbookcommand and prints its output with a prefix on each line. Whenever is required, you could write your own executor implementation and set it on AnsiblePlaybookCmd object, it will expect that the executor implements Executor interface.

type Executor interface {
	Execute(command string, args []string, prefix string) error
}

Its possible to define your own executor and set it on AnsiblePlaybookCmd.

type MyExecutor struct {}
func (e *MyExecutor) Execute(command string, args []string, prefix string) error {
    fmt.Println("I am doing nothing")

    return nil
}

playbook := &ansibler.AnsiblePlaybookCmd{
    Playbook:          "site.yml",
    ConnectionOptions: ansiblePlaybookConnectionOptions,
    Options:           ansiblePlaybookOptions,
    Exec:              &MyExecutor{},
}

When you run the playbook using your dummy executor, the output received is the next one.

$ go run myexecutor-ansibleplaybook.go
I am doing nothing

Example

When is needed to run an ansible-playbook from your Golang application using go-ansible package, you must define a AnsiblePlaybookCmd,AnsiblePlaybookOptions, AnsiblePlaybookConnectionOptions as its shown below.

AnsiblePlaybookConnectionOptions where is defined how to connect to hosts.

ansiblePlaybookConnectionOptions := &ansibler.AnsiblePlaybookConnectionOptions{
	Connection: "local",
}

AnsiblePlaybookOptions where is defined which should be the ansible-playbook execution behavior and where to find execution configuration.

ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
    Inventory: "127.0.0.1,",
}

AnsiblePlaybookCmd where is defined the command execution.

playbook := &ansibler.AnsiblePlaybookCmd{
    Playbook:          "site.yml",
    ConnectionOptions: ansiblePlaybookConnectionOptions,
    Options:           ansiblePlaybookOptions,
    ExecPrefix:        "Go-ansible example",
}

Once the AnsiblePlaybookCmd is already defined it could be run it using the Run method.

err := playbook.Run()
if err != nil {
    panic(err)
}

The result of the ansible-playbook execution is shown below.

Go-ansible example =>
Go-ansible example =>  PLAY [all] *********************************************************************
Go-ansible example =>
Go-ansible example =>  TASK [Gathering Facts] *********************************************************
Go-ansible example =>  ok: [127.0.0.1]
Go-ansible example =>
Go-ansible example =>  TASK [simple-ansibleplaybook] **************************************************
Go-ansible example =>  ok: [127.0.0.1] =>
Go-ansible example =>    msg: Your are running 'simple-ansibleplaybook' example
Go-ansible example =>
Go-ansible example =>  PLAY RECAP *********************************************************************
Go-ansible example =>  127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Go-ansible example =>
Go-ansible example =>  Playbook run took 0 days, 0 hours, 0 minutes, 1 seconds
Duration: 1.816272213s

# Packages

No description provided by the author

# Functions

AnsibleForceColor change to a forced color mode.

# Constants

AnsibleForceColorEnv is the environment variable which forces color mode.
AnsiblePlaybookBin is the ansible-playbook binary file value.
AskBecomePassFlag is ansble-playbook's ask for become user password flag.
AskPassFlag is ansble-playbook's ask for connection password flag.
BecomeFlag is ansble-playbook's become flag.
BecomeMethodFlag is ansble-playbook's become method flag.
BecomeUserFlag is ansble-playbook's become user flag.
ConnectionFlag is the connection flag for ansible-playbook.
ExtraVarsFlag is the extra variables flag for ansible-playbook.
FlushCacheFlag is the flush cache flag for ansible-playbook.
InventoryFlag is the inventory flag for ansible-playbook.
LimitFlag is the limit flag for ansible-playbook.
ListHostsFlag is the list hosts flag for ansible-playbook.
ListTagsFlag is the list tags flag for ansible-playbook.
ListTasksFlag is the list tasks flag for ansible-playbook.
PrivateKeyFlag is the private key file flag for ansible-playbook.
SyntaxCheckFlag is the syntax check flag for ansible-playbook.
TagsFlag is the tags flag for ansible-playbook.
TimeoutFlag is the timeout flag for ansible-playbook.
UserFlag is the user flag for ansible-playbook.
VaultPasswordFileFlag is the vault password file flag for ansible-playbook.

# Structs

AnsiblePlaybookCmd object is the main object which defines the `ansible-playbook` command and how to execute it.
AnsiblePlaybookConnectionOptions object has those parameters described on `Connections Options` section within ansible-playbook's man page, and which defines how to connect to hosts.
AnsiblePlaybookOptions object has those parameters described on `Options` section within ansible-playbook's man page, and which defines which should be the ansible-playbook execution behavior.
AnsiblePlaybookPrivilegeEscalationOptions object has those parameters described on `Privilege Escalation Options` section within ansible-playbook's man page, and which controls how and which user you become as on target hosts.
DefaultExecute is a simple definition of an executor.
MockExecute defines a simple executor for testing purposal.

# Interfaces

Executor is and interface that should be implemented for those item which could run ansible playbooks.