How to execute ansible playbook from crontab?

You can use cron jobs to run your playbooks.
Ansible calls ssh with -tt switch to force TTY, so it should work nice.
Just check the following:

  • job user has access to ssh private keys
  • job environment is correct (PYTHONPATH, etc...)
  • there is no package mixture on the host (e.g. ansible installed via pip and apt at the same time)

Also check this handy comment about quiet option absence in ansible:

There's a trick for crontab: run ansible-playbook as follows:

*/15 * * * *    if ! out=`ansible-playbook yourplaybook.yaml`; then echo $out; fi

This way you get complete output, but only if ansible exited with a non-zero status.


It will also work, I am using this to check an agent status and starting it if it is stopped.

*/5 * * * * ansible-playbook -i /root/playbooks/agent /root/playbooks/agent.yml



We can run as konstantin-suvorov's answer as well as

type

# whereis ansible-playbook
  /usr/bin/ansible-playbook

and run with the path displayed in the output

we can run with this in crontab

#crontab -e


*/15 * * * * /usr/bin/ansible-playbook /home/user1/yourplaybook.yml

It worked like charm

Tags:

Ansible