Ansible: Scheduling jobs using AT and CRON

Ansible_Logo

Ansible provides a means to schedule jobs to be executed on a future time, weather it is a one time job or ones to be repeated at regular interval. This is done using the modules “ansible.builtin.cron” OR “ansible.posix.at” based on the requirement.

Below is a playbook that will execute a command “touch /root/test1.txt” 2 minutes after ansible executes the playbook on the managed node 192.168.48.129 (centosMYOBvm)

Let us check the status of the managed node

[root@centosMYOBvm ~]# systemctl status atd
Unit atd.service could not be found.
[root@centosMYOBvm ~]# ls -l /root/test1.txt
ls: cannot access ‘/root/test1.txt’: No such file or directory

Now let us create and run a playbook to install “at” package and add a file in root directory

[root@centos9vm ~]# cat schedule.yml

===== ==
– – –
– name: Playbook to install and run AT commands
    hosts: 192.168.48.129
    tasks:
        – name: Task to install at module in destination node
            ansible.builtin.dnf:
                name: at
                state: present

        – name: Tast to start atd service
            ansible.builtin.service:
                name: atd
                state: started
                enabled: yes

        – name: Task to run the AT command
            ansible.builtin.at:
                command: touch /root/test1.txt
                count: 2
                units: minutes

===== ==

[root@centos9vm ~]# ansible-navigator run -m stdout schedule.yml

==== ===

PLAY [Playbook to install and run AT commands] *********************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]

TASK [Task to install at module in destination node] ***************************
changed: [192.168.48.129]

TASK [Tast to start atd service] ***********************************************
changed: [192.168.48.129]

TASK [Task to run the AT command] **********************************************
changed: [192.168.48.129]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ==

Let us verify the result in the managed node.

[root@centosMYOBvm ~]# systemctl status atd

=== ===
● atd.service – Deferred execution scheduler
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; preset: enabled)
Active: active (running) since Wed 2024-06-12 12:38:24 IST; 4s ago
=== ==

[root@centosMYOBvm ~]# atq
1 Wed Jun 12 12:40:00 2024 a root
[root@centosMYOBvm ~]# date
Wed Jun 12 12:38:35 PM IST 2024
[root@centosMYOBvm ~]# ls -l /root/test1.txt
ls: cannot access ‘/root/test1.txt’: No such file or directory
[root@centosMYOBvm ~]# ls -l /root/test1.txt
-rw-r–r–. 1 root root 0 Jun 12 12:40 /root/test1.txt

——————-******———————-

Now let us move to scheduling repetitive jobs using CRON. In the below example we will use ansible to install crontabs and add an entry in crontab

[root@centos9vm ~]# cat cron_schedule.yml

== ===
– – –
– name: Playbook to install and add cron jobs
    hosts: 192.168.48.129
    tasks:
        – name: Task to install cron and start the service
            ansible.builtin.dnf:
                name: crontabs
                state: present

        – name: Task to start cron service
            ansible.builtin.service:
                name: crond
                state: started
                enabled: yes

        – name: Add a cron job to wtire Hi to /root/test1.txt
            ansible.builtin.cron:
                name: Write to test1
                minute: “10”
                hour: “19”
                job: echo “Hi” > /root/test1.txt

=== ===

Let us verify if cron is already installed in the managed node.

[root@centosMYOBvm ~]# systemctl status crond
Unit crond.service could not be found.

==== ===

Now let us run the playbook

[root@centos9vm ~]# ansible-navigator run -m stdout cron_schedule.yml

==== ===

PLAY [Playbook to install and add cron jobs] ***********************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]

TASK [Task to install cron and start the service] ******************************
changed: [192.168.48.129]

TASK [Task to start cron service] **********************************************
changed: [192.168.48.129]

TASK [Add a cron job to wtire Hi to /root/test1.txt] ***************************
ok: [192.168.48.129]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ===

Now let us verify back in the managed node:

[root@centosMYOBvm ~]# systemctl status crond

===== ==
● crond.service – Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; preset: enabled)
Active: active (running) since Wed 2024-06-12 19:24:58 IST; 38s ago

====== ==

[root@centosMYOBvm ~]# crontab -l

===== ===
#Ansible: Write to test1
10 19 * * * echo “Hi” > /root/test1.txt

===== ===