Bobby Kimble
Bobby Kimble

Reputation: 103

How to execute ansible playbook from crontab?

Is it possible to execute an ansible playbook from crontab? We have a playbook that needs to run at a certain time ever day, but I know that cron doesn't like ssh.

Tower has a built in scheduling engine, but we are not interested in using Tower. How are other people scheduling ansible playbooks?

Upvotes: 10

Views: 36300

Answers (6)

aboyum
aboyum

Reputation: 53

You could do that, but you have to first tell where ansible.cfg is and then start the playbook, and for everything you also have to use full path. Like this: crontab -e to edit, crontab -l to list

# Softwareupgrade
1 5 28 6 * ANSIBLE_CONFIG=/home/xxx/ansible/software-upgrade/ansible.cfg /usr/bin/ansible-playbook -i /home/xxx/ansible/software-upgrade/hosts /home/xxx/ansible/software-upgrade/upgrade.yml

If you also would like to view the ansible output, you must also redirect that to a file, like this:

# Softwareupgrade
1 5 28 6 * ANSIBLE_CONFIG=/home/xxx/ansible/software-upgrade/ansible.cfg /usr/bin/ansible-playbook -i /home/xxx/ansible/software-upgrade/hosts /home/xxx/ansible/software-upgrade/upgrade.yml >> /home/xxx/ansible/software-upgrade/logfile.log 2>&1

That work as long as ansible doesn't return "0" Unfortunately it becomes very long lines of text in crontab, but it works fine, and you may execute stuff early in the morning or on a regular basis.

Upvotes: 0

Sagar Shinde
Sagar Shinde

Reputation: 51

we can create wrapper script in bash like below

ANSIBLE_PLAYBOOK=`which ansible_playbook`
cd main_dir_where_playbook_is_located
$ANSIBLE_PLAYBOOK playbook.yaml > log_file

save above as run.sh and schedule it like normal cron.

Upvotes: 0

Javeed Shakeel
Javeed Shakeel

Reputation: 3427

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

Upvotes: 2

Hasan S Syed
Hasan S Syed

Reputation: 21

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


Upvotes: 2

Benoit Galati
Benoit Galati

Reputation: 493

You can achieve the same behavior Konstantin Suvorov mentioned in his answer by using croncape.

*/15 * * * *    croncape ansible-playbook yourplaybook.yaml

Upvotes: 1

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68279

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.

Upvotes: 19

Related Questions