EX294 9.6: Task scheduling
Schedule a user's cron job, add and remove a job in /etc/cron.d, and
deploy a systemd timer.
Before you start
This VM is the control node. The lab creates the user greg (password
Lab-Pass-1) with an SSH key, and installs ansible-core. Work as greg,
in /home/greg/ansible: its ansible.cfg reads the inventory inventory,
logs in as devops and becomes root. node1 is in the group webservers
and node2 in dbservers.
The managed nodes node1 (10.99.1.11) and node2 (10.99.1.12) are
AlmaLinux 10 containers, prepared as in chapter 3.1: devops takes greg's
key and may use sudo without a password. Both run crond, and node1 has a
user student. For a root shell on a node, sudo podman exec -it node1 bash.
Tasks
- Schedule
logger "EX294 in progress"forstudentevery 2 minutes on the web servers.crontab -l -u studentshows it, and after a few minutesjournalctl -t studenton node1 has the messages. - Add a job to
/etc/cron.d/backupon every node that archives/etcat 02:00 every Sunday, as root. The file has the line withroot. Then remove it again from the web servers withstate: absent; it's gone there, and stays on the database servers. - Deploy a systemd timer
cleanup.timeron the database servers that runs a cleanup daily.systemctl list-timerslists it with a next run time.
Hints
ansible.builtin.cronwithname,user,minute: "*/2",job; for/etc/cron.d,cron_fileanduser: root, andweekday: "0".state: absentfinds the job by itsname, so use the same one.- A timer is two units: the oneshot service that does the work and the
timer with
OnCalendar=daily. Enable and start the timer, afterdaemon_reload: true.