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

Extra vars issue in playbooks

BorisKoz opened this issue · comments

This is a duplicate (almost) of a previously closed issue 107 noted here.
The issue was closed, but yet it remains unresolved. The problem remains (in playbooks at least) as follows:

  • the func used to generate --extra-vars is
    func (o *AnsiblePlaybookOptions) generateExtraVarsCommand() (string, error) {
    and does not encapsulate with '
  • the playbook.String() generates the --extra-vars with
    str = fmt.Sprintf("%s %s %s", str, ExtraVarsFlag, fmt.Sprintf("'%s'", extraVars))
    where extraVars is generated by o.generateExtraVarsCommand() so it does encapsulate the json string with ' afterwards
  • the playbook.Command() on the other hand
    func (o *AnsiblePlaybookOptions) GenerateCommandOptions() ([]string, error) {
    is used in playbook.Run() and DOES NOT encapsulate the --extra-vars string with '. So the runtime will fail, as the JSON string will not be detectable from the command line.

The fix seems pretty obvious - add the missing ', maybe with cmd = append(cmd, fmt.Sprintf("'%s'", extraVars)) in

cmd = append(cmd, extraVars)

Hi @BorisKoz!
Thank you for opening that issue!

Issue #107 was closed because there was a misunderstanding between the purpose of methods Command and String.

Could you tell me if you are trying to print the output of the method Command?
In that case, you will see that quotes are missing because the []string returned by the Command method is ready to be executed, and does not provide those quotes. If you justt want to print the output of the command, use the method String instead.

Thanks!

An honest mistake in my part. My json preparation was a bit off, and as the .Run() was using .Command(), which failed, but the .String() ran fine when masted into cmd, so I looked for the differences, which seemed to only be the ' eliminators. Thank you for replying, after checking out again, my json builder was a little bit off.
Sorry.)