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
881 stars 141 forks source link

warning messages are included on json data when is used json stdout_callback #27

Closed apenella closed 3 years ago

apenella commented 3 years ago

Which ansible version are you using? ansible 2.10.5 config file = ??/.ansible.cfg configured module search path = ['??/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = ??/.local/lib/python3.8/site-packages/ansible executable location = ??/.local/bin/ansible python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]

Which go version are you using? go version go1.15.6 linux/amd64

Which go-ansbile version are you using? 0.7.0

What did you see? When is run a playbook using the json stdout_callbak, and there is a warning message on the output (with warning messages I mean those messages that start with [WARNING]), that message is included on json data and then the parsing done by JSONParse fails.

exit status 4                                                                                  
panic: Unmarshall error                     
        invalid character '[' after top-level value

On the other hand, some warning messages returned by ansible-playbook in one line on terminal are seen as multiple line messages by go-ansible

Thats an example:

How to reproduce the issue? To reproduce the issue I used simple-ansibleplaybook-json example. I forced a connection error updating ansiblePlaybookConnectionOptions definition as is shown below:

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

You could see the scanned text by go-ansible updating ansiblePlaybookJSONResults as is shown below:

    for scanner.Scan() {
        line := scanner.Text()
        if !skipLine(line) {
                        // println for debuggin purpose
                        fmt.Println("-- #" + line + "#")
            fmt.Fprintf(w, "%s", line)
        }
    }

The issue is related to #23

apenella commented 3 years ago

On DefaultExecutor is created a multireader for stdout and stderr, then the stdout and stderr output is written on same writer and that causes an invalid json outcome when there is a warning message during playbook execution.

Here there is where is defined the multireader: https://github.com/apenella/go-ansible/blob/master/execute/defaultExecute.go#L85

I am going to separete stdout and stderr to avoid this kind of errors.