apenella / go-ansible

Go-ansible is a Go package that enables the execution of ansible-playbook or ansible commands directly from Golang applications. It supports a wide range of options for each command, enabling smooth integration of Ansible functionality into your projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with unreachable host

weichenqi opened this issue · comments

Hi @apenella
When I excute with command----(1.1.1.1 is unreachable host for test)
/opt/homebrew/bin/ansible-playbook --extra-vars '{"host_name":"10.200.75.152,10.200.75.136,1.1.1.1"}' --inventory 10.200.75.136,10.200.75.152,1.1.1.1 --user root nstall-nginx.yml
is ok.
but with go-ansible i got a panic:
panic: Error during command execution: ansible-playbook error: parser error
tips:when i del 1.1.1.1, it just ok with go-ansible

code:

func ansiblePlaybookTask(_, f, inventory string, jsonvars map[string]interface{}){
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
//Connection: "local",
User: "root",
}

ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: inventory,
//Inventory: "/root/inventory",
ExtraVars: jsonvars,
}

playbook := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{f},
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
StdoutCallback: "json",
}

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

thanks!

Hi, @weichenqi!
I have been trying to reproduce what you described, but I couldn't.
What I propose you is to print the ansible-playbook command using the String method and check whether the command is the expected one.

On the other hand, I recommend using the AddExtraVars method instead of assign a variable to AnsiblePlaybookCmd's ExtraVars attribute.

Let me know if you have any update!
Thank you!

Hi, @apenella
If I just modified code,
err := playbook.Run(context.TODO()) if err != nil { panic(err) }
to
playbook.Run(context.TODO())
the program can run normally with 1.1.1.1,
image

Hi, @weichenqi
Thanks for the update.

As you can see on the example json-stdout-ansibleplaybook, instead of panic after Run you could do a soft management of the error, such as notify the error, and then parse the results.

err = playbook.Run(context.TODO())
if err != nil {
	fmt.Println(err.Error())
}

res, err = results.JSONParse(buff.Bytes())
if err != nil {
	panic(err)
}

fmt.Println(res.String())

In that case, the output received is:

$  go run json-stdout-ansibleplaybook.go 
Error during command execution: ansible-playbook error: parser error

Command executed: /home/aleix/.local/bin/ansible-playbook --inventory 1.1.1.1, --user apenella site.yml

exit status 4
[1.1.1.1] (Gathering Facts)     Data could not be sent to remote host "1.1.1.1". Make sure this host can be reached over ssh: ssh: connect to host 1.1.1.1 port 22: Connection timed out

Host: 1.1.1.1
 Changed: 0 Failures: 0 Ignored: 0 Ok: 0 Rescued: 0 Skipped: 0 Unreachable: 1

I hope that this could help you!
Thanks!

Yes, I see, it’s for better compatibility errors。Thanks!

Great @weichenqi
I am going to close the issue