2.4 KiB
EX294 9.5: File content and archives
Change one line of a file whatever it says now, keep a block of lines up to date, and archive a directory on every node and fetch the archives.
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 community.general collection isn't
installed yet.
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. Their /etc/ssh/sshd_config
files say different things about PermitRootLogin. (Drop-ins of the
node image in /etc/ssh/sshd_config.d/ still allow root logins, and they
win over sshd_config, so sshd -T keeps saying permitrootlogin yes;
leave them.) For a root shell on a node, sudo podman exec -it node1 bash.
Tasks
-
Write
/home/greg/ansible/sshd.ymlto setPermitRootLogin noin/etc/ssh/sshd_configon every node, whatever the line says now, and restart sshd only if it changed.grep ^PermitRootLoginshows one line, and a second run doesn't restart sshd. -
Add this three-line block to
/etc/motdon every node, between markers:This node is managed by Ansible. Changes made by hand will be lost. Questions: [email protected]Then change the last line to
Questions: [email protected]and run it again. The file has one block, with the new line, between the markers. -
Archive
/etcon every node as/root/etc.tar.bz2, and fetch them all to the control node, under/home/greg/ansible/backups/.backups/node1/root/etc.tar.bz2and the other exist, andtar tjflists them.
Hints
ansible.builtin.lineinfilewith aregexpthat matches the line whatever its value,validate: /usr/sbin/sshd -t -f %s, and a handler for the restart.ansible.builtin.blockinfilefinds its own block again by its markers.ansible-galaxy collection install 'community.general:>=11,<12'(12 and later need a newer ansible-core) forcommunity.general.archive(format: bz2);ansible.builtin.fetchwithdest: backups/.tar tjfhere needs thebzip2package.