Schedule Playbook with crontab

4,889 views
Skip to first unread message

Phil F

unread,
Jul 12, 2017, 12:03:51 PM7/12/17
to Ansible Project
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:

---
- hosts: CISCO_SWITCHES_ALL
  connection: local

  vars:
    backup_root: /home/ansibleuser/Configs


  tasks:
    - name: run show running-config on remote devices
      ios_command:
        commands: show running
        timeout: 20
      register: config

    - name: ensure backup folder is created
      file:
        path: "{{ backup_root }}"
        state: directory
      run_once: yes

    - name: ensure device folder is created
      file:
        path: "{{ backup_root }}/{{ inventory_hostname }}"
        state: directory

    - copy:
        content: "{{ config.stdout[0] }}"
        dest: "{{ backup_root }}/{{ inventory_hostname }}/running-config_{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"

Does anyone have an idea how I can generate more debug output than just "ERROR! Extraneous options or arguments" ?

Even better if someone knows how I can fix this !!

Kind regards,

Phil.



Brian Coca

unread,
Jul 12, 2017, 12:08:25 PM7/12/17
to Ansible Project
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


--
----------
Brian Coca

mail.ph...@gmail.com

unread,
Jul 13, 2017, 1:59:59 PM7/13/17
to Ansible Project

mail.ph...@gmail.com

unread,
Jul 13, 2017, 1:59:59 PM7/13/17
to Ansible Project
Thank you so much Brian, it works perfectly.


On Wednesday, July 12, 2017 at 6:08:25 PM UTC+2, Brian Coca wrote:

Day Man

unread,
Sep 24, 2017, 12:11:09 PM9/24/17
to Ansible Project

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!

Brian Coca

unread,
Sep 25, 2017, 9:31:27 AM9/25/17
to Ansible Project
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 Coca

Dayo Ajibola

unread,
Sep 25, 2017, 9:40:49 AM9/25/17
to ansible...@googlegroups.com
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 



--
----------
Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7duAqto9MEtkmj%3D0VYR%3DDFqUQ-aQ1rDdBzas_rqe_xW3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Sep 25, 2017, 9:42:58 AM9/25/17
to Ansible Project
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.





--
----------
Brian Coca

Dayo Ajibola

unread,
Sep 25, 2017, 9:47:25 AM9/25/17
to ansible...@googlegroups.com
you are right, Thanks






--
----------
Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

andres pinina

unread,
Jun 24, 2018, 8:21:06 AM6/24/18
to Ansible Project
crontab -e


On Monday, September 25, 2017 at 9:40:49 AM UTC-4, Dayo Ajibola wrote:
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 
On Mon, Sep 25, 2017 at 7:31 AM, Brian Coca <bc...@redhat.com> wrote:
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 Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages