Software >> Automation >> Ansible >> Modules >> cron >> Examples

 

cron


## DOCUMENTATION

online help: https://docs.ansible.com/ansible-core/2.11/collections/ansible/builtin/cron_module.html
offline help: ansible-doc cron


## STATUS BEFORE

[root@rhel7 ~]# date
Thu Feb  3 10:28:17 +08 2022

[root@rhel7 ~]# crontab -l

[root@rhel7 ~]# ls -lah /etc/cron.d
total 32K
drwxr-xr-x.   2 root root  71 Feb  3 10:27 .
drwxr-xr-x. 172 root root 12K Feb  3 09:22 ..
-rw-r--r--.   1 root root 128 Feb 14  2019 0hourly
-rw-r--r--.   1 root root 108 Sep 21  2020 raid-check
-rw-r--r--.   1 root root 459 Apr 15  2020 sa-update
-rw-------.   1 root root 235 Jan 20  2020 sysstat

[root@rhel8 ~]# date
Thu Feb  3 10:28:45 +08 2022

[root@rhel8 ~]# crontab -l

[root@rhel8 ~]# ls -alh /etc/cron.d
total 24K
drwxr-xr-x.   2 root root  39 Feb  3 10:28 .
drwxr-xr-x. 168 root root 12K Feb  3 09:26 ..
-rw-r--r--.   1 root root 128 Jun 13  2019 0hourly
-rw-r--r--.   1 root root 108 Jun 10  2021 raid-check
[root@rhel8 ~]#


## THE PLAYBOOK

[ansible@mgmt7 ~]$ cat ansible-cron.yml
---
- name: cron module example 1
  hosts: all
  become: True
  gather_facts: no
  tasks:
  - name: (1) Ensure cron job exist at specific minute
    cron:
       name: "My cron job"
       minute: "5"
       hour: "10"
       day: "3"
       month: "2"
       weekday: "3"
       job: "ls -lah > /dev/null"
  - name: (2) create a cron file under /etc/cron.d
    cron:
       name: "yum auto update"
       minute: "0"
       hour: "0"
       weekday: "6"
       user: "root"
       job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
       cron_file: ansible_yum-auto-update



## RUN THE PLAYBOOK

[ansible@mgmt7 ~]$ ansible-playbook -i hosts_rhel ansible-cron.yml

PLAY [cron module example 1] *****************************************************

TASK [(1) Ensure cron job exist at specific minute] ******************************
changed: [rhel7_int]
changed: [rhel8_int]

TASK [(2) create a cron file under /etc/cron.d] **********************************
changed: [rhel7_int]
changed: [rhel8_int]

PLAY RECAP ***********************************************************************
rhel7_int                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rhel8_int                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


## STATUS AFTER

[root@rhel7 ~]# date
Thu Feb  3 10:34:53 +08 2022

[root@rhel7 ~]# crontab -l
#Ansible: My cron job
5 10 3 2 3 ls -lah > /dev/null

[root@rhel7 ~]# ls -lah /etc/cron.d
total 36K
drwxr-xr-x.   2 root root 102 Feb  3 10:31 .
drwxr-xr-x. 172 root root 12K Feb  3 09:22 ..
-rw-r--r--.   1 root root 128 Feb 14  2019 0hourly
-rw-r--r--.   1 root root  83 Feb  3 10:31 ansible_yum-auto-update
-rw-r--r--.   1 root root 108 Sep 21  2020 raid-check
-rw-r--r--.   1 root root 459 Apr 15  2020 sa-update
-rw-------.   1 root root 235 Jan 20  2020 sysstat

[root@rhel7 ~]# cat /etc/cron.d/ansible_yum-auto-update
#Ansible: yum auto update
0 0 * * 6 root YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate


[root@rhel8 ~]# date
Thu Feb  3 10:35:52 +08 2022

[root@rhel8 ~]# crontab -l
#Ansible: My cron job
5 10 3 2 3 ls -lah > /dev/null

[root@rhel8 ~]# ls -alh /etc/cron.d
total 28K
drwxr-xr-x.   2 root root  70 Feb  3 10:31 .
drwxr-xr-x. 168 root root 12K Feb  3 09:26 ..
-rw-r--r--.   1 root root 128 Jun 13  2019 0hourly
-rw-r--r--.   1 root root  83 Feb  3 10:31 ansible_yum-auto-update
-rw-r--r--.   1 root root 108 Jun 10  2021 raid-check

[root@rhel8 ~]# cat /etc/cron.d/ansible_yum-auto-update
#Ansible: yum auto update
0 0 * * 6 root YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate


## PLAYBOOK TO REVERSE THE CHANGES

[ansible@mgmt7 ~]$ cat ansible-cron-reset.yml
---
- name: cron module example 2
  hosts: all
  become: True
  gather_facts: no
  tasks:
  - name: (1) remove cron job by name
    cron:
       name: "My cron job"
       state: absent
  - name: (2) removes a cron file under /etc/cron.d
    cron:
       name: "yum auto update"
       cron_file: ansible_yum-auto-update
       user: root
       state: absent



[ansible@mgmt7 ~]$ ansible-playbook -i hosts_rhel ansible-cron-reset.yml

PLAY [cron module example 2] *****************************************************

TASK [(1) remove cron job by name] ***********************************************
changed: [rhel7_int]
changed: [rhel8_int]

TASK [(2) removes a cron file under /etc/cron.d] *********************************
changed: [rhel7_int]
changed: [rhel8_int]

PLAY RECAP ***********************************************************************
rhel7_int                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
rhel8_int                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0