Logging Ansible
It’s important to have some kind of logging when things go wrong , and when they go right too .Somehow Ansible seems to log (to syslog (LOG_INFO facility))
Given this very basic playbook:
if you run something like: ( -vvv critical as otherwise, nothing goes to syslog)
ansible-playbook -i hosts demo.yml -**vvv**
You will get some thing like:
which us quite neat , but what happens when things go wrong?, im gonna change the command I’m running to “decho” instead of “echo” , as we all know that’s gonna trigger some kind of error:
and we get this output:
That’s not cool , we know that command failed , but It’d be nice to know the Exception , something along the lines of “command doesn’t exist” or whatever OSError we get.
From the log files we can tell the the file that’s logging ins ansible-basic.py so let’s take a look.
The run_command function is the one that ultimately be called (by command: for example) , it looks something like this
But the real problem of it is that for some reason it isn’t logging to syslog when it fails let’s see if there’s some catch for it.
First of all when the commands execute , there’s a wrapper around syslog.syslog(LOG_INFO,msg) called log() and as this is inside the same class we call it as self.log(), and this is invoked when the os.pipes are triggered:
But when things failed:
There’s no syslog/log/_log_to_syslog call at all , that means that we will never see the failure into the log file , we get to see it onto the normal output as we gonna get the fail_json object with the details , but i wanna see it in the log file , maybe to plot it somehow later on , so this is super simple to add , something like:
So basically add a call to self.log() with a string and that’s it!
Now we get logs like this through syslog:
That’s something i would like to have as default , Maybe there’s another way to do it , but I haven’t found one yet.