Logging Playbook Output - Are There Better Ways?

Logging Playbook Output - Are There Better Ways?

Hello,

so far I have tried different ways to log the output of a single Ansible Playbook execution to a file.
In business environments you usually want to upload the logfile to some platform (e.g. jira) or send it to someone.
And of course you want to automate this process, too.

my current approach

Right now the most feasible method for me is to do it like this:

  • create a wrapper bash script around the ansible-playbook commands
  • redirect the output of the ansible-playbook command to a log file
  • create a second playbook that uploads the logfile to jira
  • start that second playbook after the primary playbook in the wrapper script

other approaches

I have seen people register every task and write that to a log with copy or lineinfile, but that seems very cumbersome to me and makes roles harder to maintain.
I know the log_path setting, but that appends all playbook outputs and is only helpful for looking at past playbook executions.
And I think I have used the log_plays plugin which did not really do what I had in mind, it also creates a dir for every host.

So - are there better ways to do it?
How do you log your Ansible runs?

Regards

Hi,

I would suggest looking into using an appropriate callback plugin. The log_plays one is probably a good starting point.

More info on callback plugins: https://docs.ansible.com/ansible/latest/plugins/callback.html

Hope that helps.

2 Likes

I tend to use script to wrap around calls to ansible-playbook. It preserves formatting and coloring. But, I keep all the logs locally, I don’t upload them anywhere. That would require some additional bash scripting for example.

Another solution is using AWX or Ansible Semaphore or similar which by design catch Ansible output and keep the logs. ansible-navigator can also catch Ansible output in json formatted logs.

I use ara to record ansible playbooks: GitHub - ansible-community/ara: ARA Records Ansible and makes it easier to understand and troubleshoot.

There is a live demo if you are curious: https://demo.recordsansible.org/

2 Likes

I’m also a happy user of ARA, it’s part of the standard kit when deploying Ansible somewhere at $work

Also, to add to @rfc2549’s post, there’s also a collection to install ARA with Ansible :slight_smile: