Remove a Cron Job
In this step, you will learn how to remove unwanted cron jobs using the Ansible Cron module. Managing the complete lifecycle of cron jobs is an important skill for system administrators and DevOps engineers.
Why Remove Cron Jobs
There are several reasons why you might need to remove cron jobs:
- The scheduled task is no longer needed
- You want to replace an existing job with a new configuration
- You need to clean up the system to improve maintainability
- The task needs to be temporarily disabled
Ansible makes it easy to remove cron jobs in a systematic and repeatable way across your infrastructure.
Understanding the State Parameter
The Ansible Cron module uses the state
parameter to control whether a cron job should be present or absent:
state: present
- This is the default. It ensures the cron job exists with the specified configuration.
state: absent
- This ensures the cron job does not exist, removing it if it was previously created.
Modifying the Ansible Playbook
Let's modify our playbook to remove the first cron job we created (named my_cron_job
). Open the file /home/labex/project/cron_module_playbook.yaml
in the WebIDE and replace its contents with the following:
- hosts: localhost
tasks:
- name: Remove the cron job
cron:
name: my_cron_job
state: absent
The key elements of this playbook are:
name: my_cron_job
- This identifies the cron job to be removed by its name.
state: absent
- This tells Ansible to ensure the cron job does not exist.
Note that we only need to specify the name of the cron job to identify it for removal. The other parameters (like minute, hour, job, etc.) are not needed.
Running the Modified Playbook
Now, run the modified Ansible playbook to remove the cron job. In the terminal, execute the following command:
cd /home/labex/project
ansible-playbook cron_module_playbook.yaml
You should see output similar to this:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Remove the cron job] *****************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The changed: [localhost]
line indicates that the cron job was successfully removed.
Verifying the Cron Job Removal
To verify that the cron job has been removed, check the crontab on the system:
crontab -l
You should now see only the second cron job in the output:
#Ansible: custom_cron_job
0 9 * * * /home/labex/project/script.sh
The my_cron_job
entry has been removed, as requested in our playbook.
Idempotence in Ansible
One of the key features of Ansible is idempotence - the property that applying the same operation multiple times gives the same result as applying it once. This is especially useful for tasks like removing cron jobs.
Let's run the playbook again to demonstrate this:
ansible-playbook cron_module_playbook.yaml
You should see output similar to:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Remove the cron job] *****************************************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Notice that the second time, the output shows ok
instead of changed
for the removal task. This is because the cron job was already removed, so no changes were needed.
Managing Multiple Cron Jobs
In real-world scenarios, you might need to manage multiple cron jobs in a single playbook. You can include multiple cron tasks, each with its own configuration:
- hosts: localhost
tasks:
- name: Remove first cron job
cron:
name: job1
state: absent
- name: Create second cron job
cron:
name: job2
minute: "0"
hour: "12"
job: /path/to/script.sh
- name: Update third cron job
cron:
name: job3
minute: "*/10"
job: /path/to/another/script.sh
This approach allows you to manage the complete lifecycle of all your cron jobs in a single, version-controlled playbook.
Congratulations! You have successfully learned how to remove cron jobs using the Ansible Cron module. This completes the basic lifecycle management of cron jobs: creation, modification, and removal.