Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real time stdout #166

Open
simo-berto98 opened this issue Jan 14, 2025 · 3 comments
Open

Real time stdout #166

simo-berto98 opened this issue Jan 14, 2025 · 3 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@simo-berto98
Copy link

simo-berto98 commented Jan 14, 2025

Hi,
first of all, I wanted to congratulate you on the excellent library you have created.
I wanted to ask you if there are any examples of reading playbook logs in real-time.
Let me explain my need, I need to read logs generated in real-time and push them to a rabbitmq queue. Then they will be consumed asynchronously and saved to a database.
At the end of the script I send the ‘recap’ of the execution via your ParseJSONResultsStream() method.

Thank you very much
Simone

@apenella
Copy link
Owner

Hi @simo-berto98!

Thank you so much! I'm glad to hear that the go-ansible library is proving useful for you. :)

I assume that by 'reading the playbook logs,' you are referring to accessing the console output during execution, am I correct?

If you configure your execution to use the JSON stdout callback, it won't be possible to read logs in real-time, as Ansible only provides the output at the end of the execution.

However, you can read the Ansible output in real-time using other stdout callback methods. Keep in mind, though, that parsing the output may be more challenging in such cases.

If your goal is to send the output to a RabbitMQ queue, one approach is to create a custom driver that implements the io.Writer interface. You can then set the DefaultExecute to use this driver as its writer by using the WithWrite method.
By default, the output mechanism writes each captured line from the Ansible playbook output to os.Stdout. Replacing the writer with your custom implementation will redirect the output to your desired destination.

I hope this helps!
Aleix

@apenella apenella added question Further information is requested good first issue Good for newcomers labels Jan 15, 2025
@simo-berto98
Copy link
Author

Thank you for your reply.
So apart from reading the buffer directly, there is no other way to get some extra metadata?

@apenella
Copy link
Owner

The go-ansible library captures the stdout of the Ansible process and allows you to handle it. You can take advantage of the TransformerFuncs to add metadata to each line. TransformerFuncs enables you to customize the output before sending it to the writer.

This example illustrates how to use transformers and shows how to create a custom transformer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants