2.3 KiB
EX294 9.2: Services
Start, enable, stop and mask services, deploy a unit of your own, and set a default target, all so that a second run changes nothing.
Before you start
This VM is the control node. The lab creates the user greg (password
Lab-Pass-1, sudo without a password) 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, node2 in dbservers, and this VM
itself in control, as localhost ansible_connection=local.
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. rpcbind runs on both. They
share this VM's clock, so chronyd can be enabled there but never runs
(systemd skips it); it runs here, on the control node, where the lab has
stopped and disabled it. For a root shell on a node, sudo podman exec -it node1 bash.
Tasks
- Make sure
chronydis running and enabled on the control node and enabled on node1 and node2, andrpcbindstopped and masked on the nodes.systemctl is-enabled chronyd rpcbindon a node showsenabledandmasked, also after a reboot. - Write
/home/greg/ansible/hello.ymlto deploy a oneshot unithello.serviceon every node that writeshello from systemdto the journal, and enable and start it.journalctlshows the line, and a second run reportschanged=0. - Write
/home/greg/ansible/target.ymlto set the default target of the database server tomulti-user.target.systemctl get-defaultagrees, and the task isokon a second run.
Hints
ansible.builtin.servicewithstate: startedandenabled: true;ansible.builtin.systemd_servicewithmasked: true. Installchronyon the nodes first. On the nodes,state: startedfor chronyd reportschangedevery time, because systemd skips it there.- A oneshot unit that should count as started needs
RemainAfterExit=yes, anddaemon_reload: truebefore it starts. systemctl get-defaultin acommandtask withchanged_when: false, oransible.builtin.filewithstate: linkon/etc/systemd/system/default.target.- Check with
ansible all -a 'systemctl is-enabled chronyd rpcbind'.