Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible creating working cronjobs

Tags:

cron

ansible

I want to setup cronjobs on various servers at the same time for Data Mining. I was also already following the steps in Ansible and crontabs but so far nothing worked. Whatever i do, i get the Error Message:

ERROR: cron is not a legal parameter at this level in an Ansible Playbook

I have: Ansible 1.8.1

And for some unknown reasons, my Modules are located in: /usr/lib/python2.6/site-packages/ansible/modules/

I would like to know which precise steps i have to follow to let Ansible install a new cronjob in the crontab file.

  1. How precisely must a playbook look like to install a cronjob?
  2. What is the command line to start this playbook?

I'm asking this odd question because the documentation of cron is insufficient and the examples are not working. Maybe my installation is wrong too, which I want to test out with a working example of cron.

like image 344
malefitz leroy Avatar asked Jan 19 '15 11:01

malefitz leroy


3 Answers

I've got (something very much like) this in a ./roles/cron/tasks/main.yml file:

- name: Creates weekly backup cronjob
  cron: minute="20" hour="5" weekday="sun"
        name="Backup mysql tables (weekly schedule)"
        cron_file="mysqlbackup-WeeklyBackups"
        user="root"
        job="/usr/local/bin/mysqlbackup.WeeklyBackups.sh"
  tags:
    - mysql
    - cronjobs

The shell script listed in the 'job' was created a little earlier in the main.yml file.

This task will create a file in /etc/cron.d/mysqlbackup-WeeklyBackups:

#Ansible: Backup mysql tables (weekly schedule)
20 5 * * sun root /usr/local/bin/mysqlbackup.WeeklyBackups.sh
like image 137
Alister Bulman Avatar answered Nov 19 '22 18:11

Alister Bulman


---
- hosts: servers
  tasks:
   - name: "Cronjob Entry"
     cron:
      name: "### recording mixing/compressing/ftping scripts"
      minute: 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57
      hour: "*"
      day: "*"
      month: "*"
      weekday: "*"
      job: /usr/share/astguiclient/AST_CRON_audio_1_move_mix.pl

I am also getting below output.

#Ansible: ### recording mixing/compressing/ftping scripts
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /usr/share/astguiclient/AST_CRON_audio_1_move_mix.pl
like image 27
Srimay Mohanty Avatar answered Nov 19 '22 17:11

Srimay Mohanty


If you're setting it up to run on the Crontab of the user:

- name: Install Batchjobs on crontab
  cron:
    name: "Manage Disk Space"
    minute: "30"
    hour: "02"
    weekday: "0-6"
    job: "home/export/manageDiskSpace.sh > home/export/manageDiskSpace.sh.log 2>&1"
    #user: "admin"
    disabled: "no"
  become_user: "{{ admin_user }}"
  tags:
   - cronjobs

Reference [1]: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/cron_module.html#examples

like image 3
leroneb Avatar answered Nov 19 '22 16:11

leroneb