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

Support multiple playbooks using results.JSONParse

sestegra opened this issue · comments

When executing multiple playbooks in playbook.AnsiblePlaybookCmd, the JSON parsing doesn't work.

For instance, I tested with examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go.

--- a/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go
+++ b/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go
@@ -33,7 +33,7 @@ func main() {
        )
 
        playbook := &playbook.AnsiblePlaybookCmd{
-               Playbooks:         []string{"site.yml"},
+               Playbooks:         []string{"site.yml", "site.yml"},
                Exec:              execute,
                ConnectionOptions: ansiblePlaybookConnectionOptions,
                Options:           ansiblePlaybookOptions,

The result is following.

panic: Unmarshall error
        invalid character '{' after top-level value

goroutine 1 [running]:
main.main()
        /workspace/go-ansible/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go:50 +0x2bd
exit status 2

playbook.AnsiblePlaybookCmd uses Playbooks fields as an array of playbook's name, so I suggest to have an array of AnsiblePlaybookJSONResults as results.JSONParse output.

FYI, JSON output is cumulative.
See related issue
ansible-collections/ansible.posix#105

hi @sestegra !
Thank you very much for reporting that bug.
I am going to check #77 and prepare a new release.

Regards!

Hi @sestegra
I reviewed your PR #77 before prepare the release and I made some changes to your proposal.
1 - I kept the original JSONParse function because I do not want to introduce a breaking change, and I neither want to do a major release.
2 - I created a new function ParseJSONResultStream inspired by your JSONParse.
2.1 - The function ParseJSONResultStream just receives one parameter, an io.Reader. I removed the first parameter, the []string. With that, the function responsibility is only to parse a json stream and, in case anyone needs to enrich the result, it should be done outside the parsing.
2.2 - Another change is that ParseJSONResultStream returns a (*AnsiblePlaybookJSONResults, error) instead of ([]AnsiblePlaybookJSONResults, error). As I could see, when you execute multiple playbook files, json stdoutcallback behaves returning a json output after each file execution, but each json output appends to previous response Plays its playbook execution result. Then, the output returned by the last playbook execution contains the whole execution results, and that is the *AnsiblePlaybookJSONResults returned by the function.

Thanks!