0

I have set up some crons in Ansible, and I want to log when they start, finish, and if they fail.

How I could do it?

I don’t know so much about Ansible.

3 Answers 3

1

Ansible is an automation tool, so it can automate things you can do by hands.

But as far as I know no existing cron-like services support sending logs to elasticsearch (Kibana is just a viewer, btw) out of the box.

So at first, you should create some wrapper script that will execute your actual job and send it's result to elasticsearch.

An then schedule this wrapper script via Ansible.

Or choose another route, setup syslog forwarding to logstash/elasticsearch on your target box to collect all logs, then just filter your desired cron jobs.

1
  • Thanks, before wrapping my script, I will give a try to eric coutu answer creating a callback plugin
    – xmartinez
    Mar 2, 2018 at 5:45
0

I don't think there is anything to do with ansible after you add cron. How about writing log of cron on file or send it by otherway maybe through el and view on kibana?

0

I would suggest checking out ansible callback plugins - they are python modules that implement callback functions that ansible will call during playbook execution. In fact, the messages ansible-playbook logs to your terminal is actually implemented as a callback plugin.

I have written callback plugins for logging to elasticsearch, influxdb, redis - they are quite powerful for hooking the results of your ansible runs up to other tools. Unfortunately I can not share them here as they were implemented during my day job.

Here are the callback plugins bundled with ansible by default: https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/callback - it's possible you may be able to use one of them to send results to logstash, which would feed them into Elasticsearch for viewing with Kibana.

3
  • Thanks for your response I will try it, it seems that is just what I need!
    – xmartinez
    Mar 2, 2018 at 5:47
  • Hmm... Ansible callbacks can log only things that happen during playbook execution. But OPs requirement is to to log when they start, finish, and if they fail. Mar 2, 2018 at 6:05
  • I was assuming that the cron job itself was an ansible-playbook call. If the OP is using ansible to create a cron job that executes a non-ansible command this would not work. However the OP could convert the non-ansible cron command to be run by an ansible playbook and benefit from callbacks plugins.
    – Emma
    Mar 5, 2018 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.