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

Update documents with an example of how to retrieve output #11

Closed apenella closed 3 years ago

apenella commented 4 years ago

hey Aleix,

when you have time can you please update docs with an example on how to retrive output?

thanks Luca

Originally posted by @lucabodd in https://github.com/apenella/go-ansible/pull/9#issuecomment-678966714

apenella commented 4 years ago

@lucabodd I will do it as soon as I can :)

apenella commented 4 years ago

Hi @lucabodd I described the json result output on https://github.com/apenella/go-ansible#results. There is also an example on https://github.com/apenella/go-ansible/tree/master/examples/simple-ansibleplaybook-json. As you will see, you only need to set AnsiblePlaybookCmd's StdoutCallback value to json.

playbook := &ansibler.AnsiblePlaybookCmd{
        Playbook:          "site.yml",
        ConnectionOptions: ansiblePlaybookConnectionOptions,
        Options:           ansiblePlaybookOptions,
        ExecPrefix:        "Go-ansible example",
        StdoutCallback:    "json",
    }

Thank you!

lucabodd commented 4 years ago

Hi @apenella

First of all thank you for your time! How about if I want to take, for example, stdout_lines or some other attributes from json output?

thank you! Luca

apenella commented 4 years ago

Hi @lucabodd unfortunately there is no way to format the json's StdoutCallbackResultsFunc output or access to json data using the DefaultExecutor and StdoutCallbackResultsFunc.

You could write your own executor based on DefaultExecutor and call result.JSONParse instead of StdoutCallbackResultsFunc, to manage the ansible's result. JSONParser returns an AnsiblePlaybookJSONResults, which is the result object of unmarshalling ansible's json output.

That is the goroutine skeleton to manage the results that your executor should has.

    go func() {

        json, err := result.JSONParse(cmdReader)
        if err != nil {
            execErrChan <- err
        }

                // TODO your output

        execDoneChan <- int8(0)
    }()

I hope that it could help you.

Thank you!

sharuuu commented 4 years ago

@apenella I too was looking for the reading the stdout to retrieve a specific field. I was trying to follow what you had suggested above but could not get it working(am a new bee with GoLang). @lucabodd did you mange to get it working?!

It would be of great help if this can be added to the examples.

lucabodd commented 4 years ago

Hello @sharuuu actually I forked the library to suit my needs. If you want to check it out take a look here https://github.com/lucabodd/go-ansible

apenella commented 4 years ago

@sharuuu @lucabodd I defined the Executor as an interface to flexiblize the library usage. I will write an example with a custom executor that parses the JSON output. I will take a look to @lucabodd's fork to get ideas to improve and make more usable go-ansible library. Thanks!

sharuuu commented 4 years ago

@lucabodd thanks for the link I was able to get it working for my use case.

@apenella thanks for considering the request, one thought to improve upon @lucabodd solution would be to return stream instead of plain string, that can give handle to console output as it occurs rather than have to wait for complete execution of a play book.

apenella commented 4 years ago

@sharuuu thank you very much. Let me check how @lucabodd manages the output and how I could implement it here.

apenella commented 3 years ago

Hi @lucabodd @sharuuu I am working on https://github.com/apenella/go-ansible/pull/13, I have updated how the JSON output is managed by JSONStdoutCallbackResults. As you proposed, I do not manipulate the output within ResultsFunc and the json returned by ansible-playbook is wrote on the io.Writer passed to ResultsFunc. Then, when the command finishes, you could manipulate and format the io.Writer content as you need. There is also provided a JSONParser on results packages that could help you to manage the ansible-playbook json.

There is an example here: https://github.com/apenella/go-ansible/tree/v0.6.0-dev/examples/simple-ansibleplaybook-json

apenella commented 3 years ago

pull request #13 has been merged