RHCE 9.0 Practice Exam: Automating Cron Job Scheduling with Ansible
π Introduction
In this RHCE 9.0 Ansible exam practice guide, we will automate the configuration of scheduled tasks using the cron module in Ansible. We will set up a cron job that runs every 2 minutes and logs a message using the logger
command.
This guide includes:
- How to create a cron job using Ansible
- How to schedule tasks under a specific user account
- How to verify cron job creation
- How to troubleshoot common cron job issues
π‘ What You Will Learn
β
How to create cron jobs using Ansible
β
How to run scheduled tasks under a specific user
β
How to verify cron job execution using crontab -l
β
How to troubleshoot cron job issues
π 1. Task Requirements
π 1.1. Problem Statement
- Create an Ansible playbook named
/home/greg/ansible/cron.yml
. - Run the playbook on the
test
host group. - Schedule a cron job that runs every 2 minutes.
- The cron job must run as user
natasha
. - Verify that the cron job is created successfully.
The cron job should execute the following command:
logger "EX200 in progress"
π 2. Writing the Ansible Playbook
π 2.1. Create the Playbook File
$ vim /home/greg/ansible/cron.yml
π Playbook Breakdown (Step-by-Step)
---
- name: Configure Cron Job for User Natasha
hosts: test
become: yes
tasks:
- name: Ensure cron job is present
ansible.builtin.cron:
name: "EX200 Logging Job"
minute: "*/2"
job: 'logger "EX200 in progress"'
user: natasha
π Explanation:
become: yes
β Ensures the playbook runs with elevated privileges.ansible.builtin.cron
β Manages cron jobs via Ansible.name: "EX200 Logging Job"
β Identifies the cron job.minute: "*/2"
β Runs every 2 minutes.job: 'logger "EX200 in progress"'
β Logs a message to system logs.user: natasha
β Runs the cron job as usernatasha
.
π 3. Running the Playbook
$ ansible-navigator run /home/greg/ansible/cron.yml -m stdout
π Expected Output
PLAY [Configure Cron Job for User Natasha] ****************************************
TASK [Ensure cron job is present] ************************************************
changed: [node2]
PLAY RECAP ************************************************************************
node2 : ok=1 changed=1 unreachable=0 failed=0
β
The cron job has been successfully created for user natasha
.
π 4. Verifying Cron Job Creation
$ ansible test -a 'crontab -l -u natasha'
π Expected Output
node2 | CHANGED | rc=0 >>
#Ansible: EX200 Logging Job
*/2 * * * * logger "EX200 in progress"
β
This confirms that the cron job is correctly added to the crontab of user natasha
.
π 5. Checking Cron Job Execution
Since the cron job logs messages to the system log, verify execution using:
$ ansible test -a 'tail -n 5 /var/log/messages'
π Expected Output (Varies by system)
node2 | CHANGED | rc=0 >>
Feb 19 14:02:01 node2 natasha[1234]: EX200 in progress
Feb 19 14:04:01 node2 natasha[1235]: EX200 in progress
Feb 19 14:06:01 node2 natasha[1236]: EX200 in progress
β Every 2 minutes, a new log entry should appear.
π 6. Common Issues & Troubleshooting
π΄ Issue 1: Cron Job Not Appearing in crontab -l
β Solution:
If the cron job is missing, rerun the playbook with --force-handlers
:
$ ansible-playbook /home/greg/ansible/cron.yml --force-handlers
Ensure the user exists:
$ ansible test -m shell -a 'id natasha'
π΄ Issue 2: Cron Job Not Executing
β Solution:
Restart the cron service:
$ ansible test -m service -a 'name=crond state=restarted'
Check if the cron service is running:
$ ansible test -a 'systemctl status crond'
π΄ Issue 3: Logs Not Appearing in /var/log/messages
β Solution:
If rsyslog
is disabled, enable it:
$ ansible test -m service -a 'name=rsyslog state=started enabled=yes'
Check system logs manually:
$ ansible test -a 'journalctl -u crond --no-pager'
π 7. Summary
- Created an Ansible playbook (
cron.yml
) to configure a cron job. - Configured a cron job that runs every 2 minutes for user
natasha
. - Verified that the cron job exists using
crontab -l
. - Checked execution logs using
tail /var/log/messages
. - Troubleshot common cron job issues.
π Congratulations! You have successfully automated cron job scheduling with Ansible for RHCE 9.0! π
π’ If you found this guide helpful, share it with your RHCE 9.0 study group! π’
π₯ Good luck on your RHCE 9.0 exam! π₯