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.
MIT License
905 stars 143 forks source link

adhoc command StdoutCallback as json raise err. #62

Closed wahello closed 3 years ago

wahello commented 3 years ago

I have some code as following:


func CheckAssetsConnectivityManual(ctx context.Context, adminUser, password, hosts string) {

    buff := new(bytes.Buffer)

    ansibleConnectionOptions := &options.AnsibleConnectionOptions{
        User: adminUser,
    }

    ansibleAdhocOptions := &adhoc.AnsibleAdhocOptions{
        Inventory:  hosts,
        ModuleName: "ping",
        //ModuleName: "setup",
    }

    // install sshpass
    ansibleAdhocOptions.AddExtraVar("ansible_password", password)

    executor := execute.NewDefaultExecute(
        execute.WithWrite(io.Writer(buff)),
    )

    adhoc := &adhoc.AnsibleAdhocCmd{
        Pattern:           "all",
        Options:           ansibleAdhocOptions,
        ConnectionOptions: ansibleConnectionOptions,
        Exec:              executor,
        StdoutCallback:    "json",
    }

    err := adhoc.Run(ctx)
    if err != nil {
        log.Println(err.Error())
    }
    log.Println(" ------ ===== buff: ", string(buff.Bytes()))
    res, err := results.JSONParse(buff.Bytes())
    if err != nil {
        log.Println(err.Error())
    }
    log.Println(" ---- res : ", res.String())

}

res, err := results.JSONParse(buff.Bytes())

panic.

apenella commented 3 years ago

hi @wahello! I updated simple-ansibleadhoc to use json stdout callback, and it finishes properly. (simple-ansibleadhoc has been updated on my local host)

❯ go run simple-ansibleadhoc.go 
ansible all  --inventory 127.0.0.1, --module-name ping  --connection local
[127.0.0.1] (ping)

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

Can you share the panic's output?

Thanks!

apenella commented 3 years ago

hi @wahello! There is any update about you issue?

Thanks!

apenella commented 3 years ago

@wahello since there is any update about your issue, I am going to close it. In case you need anything else, just open a new issue! Thanks