The sweep now fails the step when a timestamp cannot be parsed or a container it removes is still there, while tolerating one another run's sweep removed first. Its 30-minute bound and the job's timeout point at each other, since a shorter bound would remove a live run's containers. The concurrency comments of Lint and Build now say that runs on one ref queue rather than cancel, and the scenarios' comment names the run script as what removes their containers. The workflow test checks the label filters of the sweep and cleanup steps by name. Assisted-by: Claude Code:claude-opus-5-5
user_management
Manages the primary ansible user and additional users, their authorized_keys, sudo and cloud-init, with a key-only fallback to root when the configured user cannot connect (a fresh guest).
Supported platforms
Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Debian trixie (Proxmox VE nodes), listed in meta/main.yml; only 24.04 is converged by a test. The role installs sudo with apt.
Requirements and run order
- Use it by its fully qualified name,
mortis.kamino.user_management. - Collection
ansible.posix(authorized_key). user_management_become_password_hashedmust be defined for the primary user (| mandatoryin theuser_management_ansible_userdefault), unlessuser_management_ansible_useris overridden. Keep it vaulted, for example in host vars.- Set
gather_facts: falseon the play. On a fresh guest the configured user cannot log in yet, so fact gathering would mark the host unreachable before the role's root fallback runs. The role uses no facts. - The bootstrap tries
wait_for_connectionas the configured user, and on failure switchesansible_ssh_usertorootand pings, with the same deploy key (ansible_ssh_private_key_file); no password is ever used. Once the users and keys are written it switches back to the configured user and checks the new connection (tasks/handback.yml; skipped in check mode), because the fact would otherwise last for the rest of the run and later plays would connect as root after thesshdrole of this Collection refused it. Where the fallback applies: a new LXC, whose root gets the deploy key from the provisioning step and whose stock Ubuntu sshd admits root by key on first boot. A VM never needs it: cloud-init creates the configured user with the key (ciuser/sshkeys), and root is not given the key. A freshly installed Proxmox node needs the deploy key in root'sauthorized_keysfirst. After thesshdrole has run, root login is refused (PermitRootLogin: noonubuntu_lxcandubuntu_vm,prohibit-passwordonpve), so a host whose configured user broke later is recovered from its Proxmox node:pct enterfor an LXC,qm terminalor the console for a VM, the node's own console for a Proxmox host. - Runs before the
sshdrole, after the step that creates the guest (LXC or VM), and first in a play for Proxmox nodes, which have no provisioning step. A play for Proxmox nodes should setuser_management_disable_cloud_init: false. - The role has no declared dependencies.
Role variables
meta/argument_specs.yml is the source of truth for types and descriptions.
| Name | Default | Required | Description |
|---|---|---|---|
user_management_bootstrap |
true |
no | Run the key-only root fallback |
user_management_install_sudo |
true |
no | Install sudo |
user_management_disable_cloud_init |
true |
no | Create /etc/cloud/cloud-init.disabled when /etc/cloud exists |
user_management_authoritative |
true |
no | Make authorized_keys exclusive for each user with at least one configured key; a user whose merged key list is empty is skipped, so their existing keys stay |
user_management_lock_root_password |
false |
no | Replace root's password hash with ! (no password login, old hash removed); fails when the primary user is root |
user_management_ansible_user |
name {{ ansible_user }}, hash user_management_become_password_hashed, group sudo |
no | Primary user, always managed; overriding replaces the whole dict |
user_management_default_authorized_keys |
[] |
no | Keys for every managed user |
user_management_other_users |
[] |
no | Additional users; state: absent removes one |
user_management_remove_home |
true |
no | Remove home directories of absent users, passed as remove to every user task |
user_management_become_password_hashed |
none | yes, unless user_management_ansible_user is overridden |
Password hash read by the primary user default |
Tags
always (user list facts, bootstrap and handback), create_users, ssh_keys, lock_root_password, disable_cloud_init.
Example play
- name: Provision users
hosts: containers
gather_facts: false
roles:
- role: mortis.kamino.user_management
vars:
user_management_other_users:
- name: alice
groups: [sudo]
authorized_keys:
- "ssh-ed25519 <public-key> alice"
Notes
- The role sets the facts
user_management_all_users(primary user first, thenuser_management_other_users) anduser_management_managed_usernames. Both are set under thealwaystag, and the task that builds the first isno_log. user_management_remove_homeis applied to every user task, so it also passesremove: truefor present users, which only matters forstate: absent.- Per-user
authorized_keysare added touser_management_default_authorized_keys. Keys are written only for users whosestateis present and whose merged list is non-empty. Withuser_management_authoritativetrue other keys are removed. tasks/ssh_keys.ymlhardcodes the.sshpath as/home/<name>/.sshand skips creating it for users withsystem: true, but theauthorized_keytask still runs for them (it uses the user's real home), so a system user with keys gets them written.tasks/ssh_keys.ymlsets the.sshdirectory's group to the user's name, and the user tasks set no primarygroup, so it assumes each user's primary group has the same name as the user (the default user-private group).- The spec lists
user_management_become_password_hashedasrawwithno_log, so the vaulted value is never printed. user_management_lock_root_passwordlocks root's password after the users and keys are written:ansible.builtin.usersets the shadow field to!(update_password: always), so the old hash is removed rather than kept behind a!prefix asusermod -Lwould leave it, and reports ok once it is there. Nothing else of root changes (shell, home,authorized_keys). It is meant for guests whose root password is not needed (for example containers created with a shared root password): recovery ispct enterfrom the Proxmox node, which needs no password. The key-only root fallback still works on a locked root, because sshd withUsePAM yes(the Ubuntu default and thesshdrole's setting) does not refuse key logins for a!hash. Leave it false on a Proxmox node, where root's password is the web UI and PAM login. The role fails when it is true and the primary user is root, since the lock would disable that user's password. Unlocking is not automated: setting the input back to false leaves the lock in place, and there is no old password to restore (set a new one withpasswd rootby hand).- Molecule:
molecule/default, run by the Collection's CI, with a stale user to remove, a planted extra key to drop and root's password set before converge, then locked by the role. The scenario uses the docker connection, wherewait_for_connectionalways succeeds, so the root fallback and the handback are not exercised by it. - The root fallback requires the primary user's name to equal
ansible_user(set as a variable): the role fails before switching to root when they differ, because the user it would create is not the one Ansible connects as. The handback then switches to that user. - The bootstrap and the handback carry the
alwaystag. Running only some tags (for example--tags ssh_keys) on a fresh host falls back to root, creates no user, and then fails the host when the handback cannot log in as the configured user. - Nothing stops the primary user's
statefrom being set toabsent, which (withuser_management_remove_hometrue) deletes the account Ansible connects as.