2.1 KiB
EX294 9.8: Users and groups
Create users from a variables file on the right hosts, in the right groups and with hashed passwords, keep the playbook idempotent, and give a group sudo rights.
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. user_list.yml there is the
variables file:
users:
- name: fred
job: developer
uid: 3001
- name: wilma
job: manager
uid: 3002
- name: barney
job: developer
uid: 3003
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, from /etc/sudoers.d/devops.
For a root shell on a node, sudo podman exec -it node1 bash.
Tasks
- Write
/home/greg/ansible/users.ymlto create the users fromuser_list.yml, with their UIDs: developers on the web servers in the groupdevelopers, managers on the database servers inopsmgr, all with the passwordRedhat123, hashed.idshows the groups, andssh fred@node1with the password works. - Make
users.ymlreportchanged=0on a second run. - Let the
developersgroup run any command with sudo without a password, with a validated drop-in.sudo -n whoamias a developer on node1 printsroot.
Hints
vars_files, aloopoverusersandwhen: item.job == 'developer';ansible.builtin.groupbeforeansible.builtin.userwithgroupsandappend: true.password_hash('sha512')makes the hash;update_password: on_createor a fixed salt keeps a second run from changing it.ansible.builtin.copywithvalidate: /usr/sbin/visudo -cf %s, to a file of its own (not/etc/sudoers.d/devops, which Ansible's own login needs).- Test a login with
ssh -o PubkeyAuthentication=no fred@node1.