docs(user_management): state gather_facts, key exclusivity and known limits

From review of the extraction. Documentation only; behaviour is unchanged.
- The play needs gather_facts: false, or a fresh guest is unreachable before
  the root fallback runs (README requirement and example, argument spec).
- user_management_authoritative skips a user whose merged key list is empty,
  so emptying the list keeps that user's existing keys.
- user_management_become_password_hashed is required unless
  user_management_ansible_user is overridden.
- Known limits: molecule does not exercise the fallback or the handback, the
  handback reconnects as ansible_user, a partial tag run on a fresh host
  fails at the handback, and the primary user can be set absent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-11-16 17:21:38 +00:00
1 parent a1645040ae
commit da87a7e141
2 files changed
+16 -7

No files matched your search

+10 -5
View File
@@ -10,9 +10,10 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb
- Use it by its fully qualified name, `mortis.kamino.user_management`.
- Collection `ansible.posix` (`authorized_key`).
- `user_management_become_password_hashed` must be defined for the primary user (`| mandatory` in the `user_management_ansible_user` default). Keep it vaulted, for example in host vars.
- `user_management_become_password_hashed` must be defined for the primary user (`| mandatory` in the `user_management_ansible_user` default), unless `user_management_ansible_user` is overridden. Keep it vaulted, for example in host vars.
- Set `gather_facts: false` on 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_connection` as the configured user, and on failure switches `ansible_ssh_user` to `root` and 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 the `sshd` role 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's `authorized_keys` first. After the `sshd` role has run, root login is refused (`PermitRootLogin: no` on `ubuntu_lxc` and `ubuntu_vm`, `prohibit-password` on `pve`), so a host whose configured user broke later is recovered from its Proxmox node: `pct enter` for an LXC, `qm terminal` or the console for a VM, the node's own console for a Proxmox host.
- Runs before the `sshd` role, 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 sets `user_management_disable_cloud_init: false`.
- Runs before the `sshd` role, 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 set `user_management_disable_cloud_init: false`.
- The role has no declared dependencies.
## Role variables
@@ -24,13 +25,13 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb
| `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 |
| `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 | no | Password hash read by the primary user default |
| `user_management_become_password_hashed` | none | yes, unless `user_management_ansible_user` is overridden | Password hash read by the primary user default |
## Tags
@@ -41,6 +42,7 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb
```yaml
- name: Provision users
hosts: containers
gather_facts: false
roles:
- role: mortis.kamino.user_management
vars:
@@ -60,4 +62,7 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb
- `tasks/ssh_keys.yml` sets the `.ssh` directory's group to the user's name, and the user tasks set no primary `group`, 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_hashed` as `raw` with `no_log`, so the vaulted value is never printed.
- `user_management_lock_root_password` locks root's password after the users and keys are written: `ansible.builtin.user` sets the shadow field to `!` (`update_password: always`), so the old hash is removed rather than kept behind a `!` prefix as `usermod -L` would 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 is `pct enter` from the Proxmox node, which needs no password. The key-only root fallback still works on a locked root, because sshd with `UsePAM yes` (the Ubuntu default and the `sshd` role'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 with `passwd root` by 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.
- 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, where `wait_for_connection` always succeeds, so the root fallback and the handback are not exercised by it.
- The handback switches the connection back to `ansible_user`, not to `user_management_ansible_user.name`. When the primary user's name is overridden to something other than `ansible_user`, the handback reconnects as `ansible_user`.
- The bootstrap and the handback carry the `always` tag. 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 `state` from being set to `absent`, which (with `user_management_remove_home` true) deletes the account Ansible connects as.
@@ -5,8 +5,9 @@ argument_specs:
description:
- >-
Builds the list of managed users, optionally falls back to root with the deploy key when the configured user cannot connect (and hands the
connection back to that user once it exists),
creates or removes users, writes C(authorized_keys) and disables cloud-init.
connection back to that user once it exists), installs sudo,
creates or removes users, writes C(authorized_keys), optionally locks root's password and disables cloud-init.
- Run it in a play with C(gather_facts) set to false. On a fresh guest the configured user cannot log in until the fallback has created it.
- Sets the facts C(user_management_all_users) and C(user_management_managed_usernames).
options:
user_management_bootstrap:
@@ -29,6 +30,9 @@ argument_specs:
default: true
description:
- Make C(authorized_keys) exclusive, so keys that are not configured are removed.
- >-
Applies only to users with at least one configured key (the defaults plus their own). A user whose merged key list
is empty is skipped, so emptying the list does not remove that user's existing keys.
user_management_lock_root_password:
type: bool
default: false