Schedule Playbook with crontab

Hi,

I am attempting to schedule a playbook to run every night to backup Cisco network device configs using crontab.

The playbook is a simple ios_command task executing show run on the Cisco Devices.

The playbook works fine when I run it manually and I have got to the point where crontab is runs the playbook.

However when crontab runs the playbook the playbook is failing and the only output I can see is this:(from ansible.log)

2017-07-12 17:52:01,981 p=6113 u=root | ERROR! Extraneous options or arguments
2017-07-12 17:54:02,232 p=6122 u=root | ERROR! Extraneous options or arguments
2017-07-12 17:56:01,525 p=6131 u=root | ERROR! Extraneous options or arguments

As you can see this is every 2 minutes and for testing purposes crontab is running the playbook every 2 minutes.

crontab:
*/2 * * * * /usr/bin/ansible /home/ansibleuser/ansible/playbooks/ansible-playbook device_bak_2.yml

playbook which runs perfectly manually:

bin/ansible is for Adhoc tasks, you want bin/ansible-playbook is for
executing playbooks

this makes no sense:
/usr/bin/ansible /home/ansibleuser/ansible/playbooks/ansible-playbook
device_bak_2.yml

I think you want:
/usr/bin/ansible-playbook /home/ansibleuser/ansible/playbooks/device_bak_2.yml

Thank you so much Brian, it works perfectly.

I am newbie at this, i know how to setup ansible playbooks and run them but i dont know how to schedule a task like backing up cisco configs like you are trying to do here. I dont know Crontab, how do i set it up and also does it use scripts as well? Please provide details. thank you!

ansible-playbook is a 'unix command line utility' you can schedule it
in cron as you would do any other, just pass it the options it needs:

0 4 * * * /usr/bin/ansible-playbook /path/to/mybackupplay.yml ....

Also, you can make your play an 'executable script' by setting the x
permission bits on the playbook file and having ansible-playbook in
the shebang:

#!/usr/bin/ansible-playbook

- hosts: localhost
  ....

Brian,
Please pardon my question it may sound stupid but i really want to learn this but dont have a grasp of this yet as i am very new to linux,
where do i place this command " 0 4 * * * /usr/bin/ansible-playbook /path/to/mybackupplay.yml … " is it going to be a file or i run it on the command line in ubuntu. maybe an example would make this a little more clear to me. maybe a simple yaml script and a cron job to run the yaml script on a schedule. if this is not too much to ask i will appreciate it a lot. thanks

That was a 'cron tab entry' .. not really an ansible thing, but a cron
one. Before attempting such a task, you should get familiar with cron.

you are right, Thanks

crontab -e