EX294 3.3: Privilege escalation
Give the automation user passwordless sudo with a validated sudoers
drop-in, see validate stop a broken one, and run one task as another
user in a play that becomes root.
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 and sshpass.
Work as greg, in /home/greg/ansible: its ansible.cfg reads the
inventory inventory, logs in as devops and has become = true.
The managed nodes node1 (10.99.1.11) and node2 (10.99.1.12) are
AlmaLinux 10 containers reached over SSH by name. On each, devops
accepts greg's key but has no sudo rights yet, so ansible all -a id
fails for now. root and ansible (in wheel, so sudo asks for the
password) have the password Lab-Pass-1, as does devops. For a root
shell on a node, sudo podman exec -it node1 bash.
Tasks
- Write
/home/greg/ansible/sudo.yml, which givesdevopspasswordless sudo on every managed node with a validated drop-in,/etc/sudoers.d/devops, mode 0440, and run it. Thenansible all -a idshowsuid=0(root)on every node. - Copy it to
/home/greg/ansible/typo.ymland put a typo in the rule (for example, leave out the colon afterNOPASSWD). Runningtypo.ymlmust fail atvalidate, and sudo must still work on the nodes. - Write
/home/greg/ansible/whoami.yml: a play withbecome: truewhosecommand: whoamitask hasbecome_user: devopsand printsdevopsfor every node.
Hints
- Until devops has sudo, log in as someone who has it:
remote_user: rootwith-k, orremote_user: ansiblewith-k -K. ansible.builtin.copywithcontent,mode: "0440"andvalidate: /usr/sbin/visudo -cf %s; the rule needs its trailing newline.registerthe result and print.stdoutwithdebug, or run the playbook with-v.