fix(user_management): hand back to the created user and refuse a mismatched fallback
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:
1 parent
da87a7e141
commit
ce537fc6b4
5 files changed
+20
-4
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user