Ansible

Running Scripts With the Ansible Cron Module

Ansible is a simple tool used to automate various tasks and manual IT processes. It acts as a node manager over SSH and can share automation across entire organizations like automated daily tasks, software installation, deployment, configuration management, and many more.

Ansible Modules

Ansible contains modules which are a piece of the script run by ansible on behalf of the user to interact with the system and perform specific tasks involving checks or automation. These modules may interact with a remote or local system to make desired changes. Ansible contains a large set of modules, well over 800.

In this article, we shed some light on the cron module and its usage. This module, alongside others, may be used from the CLI (terminal) or in a playbook task. It should be noted that the modules can be written in any text editor of users’ choice as long as they support the YAML format and returns JSON format data.

Cron Module

In various Linux operating systems, a cron is a tool used to run a set of scripts or commands at a predefined time. This scheduled run is often referred to as a “cron job.”

This module may be used as a standalone tool on Linux without the need for Ansible; however, for ease of user in terms of provision and platform, Ansible is preferred as it can make our accessibility easy, thereby giving us tons of other features in the form of various modules.

For instance, to manage various cron jobs, we would have to write the code manually on the terminal; however, with Ansible, we can get rid of the tedious work and reduce errors substantially by using the playbook to set up the cron jobs for servers we wish to configure.

As mentioned before, Ansible uses modules on the terminal or in the playbook. The cron module can manage entries of the crontab by using the playbook.

The cron module manages the crontab, specifically the crontab entries. It has several parameters, which are briefly discussed below.

Parameters

String as a name: this is the string that acts as a name for a given crontab. This parameter should be unique. In case it is not, we will have our previous cron task being replaced by the newer ones with the same name.

String as a state: this string specifies whether the feature is present or absent. For instance, we want to remove an old task that is no longer required. We can remove the job prefixed as “#Ansible: previous job” from our crontab. Now the state will show up as absent.

String as a job: this string is usually the command we are about to execute. It is not necessarily required if our state is absent; however, in the case of a present state, we need the job entry on our crontab. This string cannot have spaces.

String as a user: this string is for the specific user whose crontab is to be modified.

Time: this string includes the time stamp containing various parameters related to time like a week, day, month, year, hour, minute. Its entry may be written as “0,7,6 ** ls-alh>/dev/null” to imply the job runs at the 6th and 7th hours.

Special time. This parameter contains special time values like annually, weekly, hourly, monthly, reboot, etc., to make the jobs more periodic.

Examples

Let us make a new entry on our Linux system as an Ansible playbook.

$ cd~
$ mkdir ansible-workspace
$ cd ansible-workspace

Next, we name our Ansible book (it should be noted that the playbook has .YAML extension).

$ vim crontest.yml

With this, the following should open.

Now that we are in the playbook, we may add the following contents.

-hosts: all
 become: true
 tasks:
    -name: “cronmodule demonstration”
     Ansible.builtin.cron
     name: “test
     state: present
     weekday: “*
 month: “*
 day: “*
 hour: “*
     minute: “*
 Job: “cache=clear

Now, run this playbook by typing the following command into the Linux shell:

ansible-playbook testbook.yml

The code described above uses cronjob to schedule a specific time to perform its job. In this case, the job is to clear the data from background applications. This memory is referred to as cache, and it is advised to clean it regularly, so our system can perform without any unnecessary load. The cronjob described above has hosts set to “all.” This means that all remote systems will have the cronjob performed on them, and the condition remains true as long as the playbook is being executed.

The third line describes the task of the cronjob, the name of the task is displayed as “cronjob demonstration,” which will appear in the Ansible execution log. The next line displays the built-in module “ansible. builtin.cron”

Several time strings are marked as “*” which denotes the default operation of cron. The above data shows that the cronjob will be performed on Wednesday at 5 AM of the current month; the job is clearing the cache.

Advantages

The cron module offers several advantages to us, including.

  • No memory of occupation while running.
  • Easier transfer of control to the user when cronjob is executing.
  • Even if the cronjob does not execute for some unknown reason, it will initiate all over again within the designated time string.

Conclusion

In this article, we went over the cron module for Ansible, responsible for the time schedules of several jobs we want to assign to our system. It is an important core command when it comes to automating several jobs over a specific time frame. We hope you were able to grasp the working of the cron module and clear your queries after reading this article.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.