Closed apenella closed 3 years ago
@lucabodd I will do it as soon as I can :)
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!
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
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!
@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.
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
@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!
@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.
@sharuuu thank you very much. Let me check how @lucabodd manages the output and how I could implement it here.
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
pull request #13 has been merged
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