RHCE 9.0 Practice Exam: Automating Cron Job Scheduling with Ansible

Expertise in Cloud, Networking & DevOps
Photo by Marc PEZIN / Unsplash

πŸ“Œ 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

  1. Create an Ansible playbook named /home/greg/ansible/cron.yml.
  2. Run the playbook on the test host group.
  3. Schedule a cron job that runs every 2 minutes.
  4. The cron job must run as user natasha.
  5. 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 user natasha.

πŸ“œ 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! πŸ”₯

Read more