fix(user_management): hand back to the created user and refuse a mismatched fallback
Build / build (pull_request) Successful in 13s
Lint / lint (pull_request) Successful in 34s
Molecule / molecule (pull_request) Successful in 10m5s

The handback switched the connection to ansible_user, while the role creates
user_management_ansible_user.name. When the two differed, the root fallback
created a user nobody connects as, and the handback then failed after the
changes made as root, or later plays connected as the wrong user.

The fallback now asserts, before switching to root, that the primary user's
name equals ansible_user (set as a variable), and the handback switches to
the user the role created. No change where the names are equal (the default).

Also name user_management as a user of ansible.posix in the galaxy.yml comment.

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:27:39 +00:00
1 parent da87a7e141
commit ce537fc6b4
5 files changed
+20 -4

No files matched your search

@@ -3,5 +3,6 @@ minor_changes:
- user_management - New role. Manages the primary ansible user and additional users (a user with ``state=absent`` is removed),
their ``authorized_keys`` (exclusive by default), sudo and cloud-init. When the configured user cannot connect (a
fresh guest) it falls back to root with the same deploy key, never a password, and hands the connection back to
the configured user once it exists. The optional ``user_management_lock_root_password`` (default false) replaces
the configured user once it exists; the fallback refuses to run when the primary user's name is not
``ansible_user``. The optional ``user_management_lock_root_password`` (default false) replaces
root's password hash with ``!`` and leaves root's keys and shell alone.
+1 -1
View File
@@ -19,7 +19,7 @@ homepage: https://git.rkdr.net/mortis/kamino
issues: https://git.rkdr.net/mortis/kamino/issues
# Lower bounds are the versions CI tests against; no upper bound, so a consumer's
# newer pin does not conflict. community.general is used by system_baseline and ufw,
# ansible.posix by system_baseline.
# ansible.posix by system_baseline and user_management.
dependencies:
community.general: ">=14.0.0"
ansible.posix: ">=2.2.0"
+1 -1
View File
@@ -63,6 +63,6 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb
- 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. 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 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 `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.
+14
View File
@@ -18,6 +18,20 @@
- name: Fallback to root if user connection failed
when: user_management_connection is failed
block:
# The fallback only helps when the user it creates is the user Ansible connects as. When the
# primary user's name differs from ansible_user (or ansible_user is not set as a variable),
# the handback could not log back in, or later plays would connect as a user the inventory
# does not name. Stop here, before anything runs as root.
- name: Refuse the fallback when the primary user is not the connection user
ansible.builtin.assert:
that: user_management_ansible_user.name == (ansible_user | default(''))
fail_msg: >-
The configured user cannot connect and the root fallback would create
'{{ user_management_ansible_user.name }}', but Ansible connects as
'{{ ansible_user | default('(ansible_user is not set as a variable)') }}'. Set ansible_user to the primary
user's name, or create the connection user before this role runs.
quiet: true
- name: Switch to root with the deploy key
# Connection var on purpose: as a fact it overrides the connection for the rest of the run,
# until tasks/handback.yml switches it back.
+2 -1
View File
@@ -7,10 +7,11 @@
# every host every run), placed after the switch it reset the configured user's socket rather
# than root's, and it is not needed: the ssh plugin keys its ControlPersist socket on host, port
# and user, so the switched user gets a new login anyway (wait_for_connection also resets first).
# The user is the one this role just created; bootstrap.yml has asserted it equals ansible_user.
- name: Switch back to the configured user
# Connection var on purpose: it overrides the connection for the rest of the run.
ansible.builtin.set_fact: # noqa: var-naming[no-role-prefix]
ansible_ssh_user: "{{ ansible_user }}"
ansible_ssh_user: "{{ user_management_ansible_user.name }}"
- name: Verify access as the configured user
ansible.builtin.wait_for_connection: