feat(user_management): add the user_management role
Copied from the environment repository's local role. The files are identical except: galaxy namespace mortis; comments and the README scrubbed of environment references (issue numbers, playbooks, inventory groups and group_vars paths, the provisioning roles by name, the history of the variable rename), with the technical explanation kept; the README asks for the role by its FQCN, names the run order generically (after the step that creates the guest, before mortis.kamino.sshd) and its example play uses mortis.kamino.user_management; the molecule scenario calls the role by its FQCN, follows the Kamino scenario conventions (kamino-molecule header, scenario.test_sequence) and no longer sets ANSIBLE_COLLECTIONS_PATH or ANSIBLE_ROLES_PATH. No variable, default, argument_specs entry or task logic changed: the key-only root fallback, the handback and user_management_lock_root_password (default false) are as in the source, and prepare/verify are unchanged. The role uses ansible.posix (authorized_key), already a galaxy.yml dependency, so galaxy.yml is unchanged. The README Roadmap no longer lists wave 1; CLAUDE.md names the role for the next release. Not verified locally: the molecule scenario (it runs on the kamino-molecule runner in CI) and ansible-test sanity (runs on push). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
3d7a4356e6
commit
2713753039
18 files changed
+706
-2
No files matched your search
@@ -0,0 +1,28 @@
|
||||
---
|
||||
- name: Test connection as configured user
|
||||
ansible.builtin.wait_for_connection:
|
||||
timeout: 5
|
||||
register: user_management_connection
|
||||
# Failure is the signal to fall back to root below; the "Verify root access" ping
|
||||
# fails the play if root is unreachable too.
|
||||
ignore_errors: true
|
||||
|
||||
# Key only, never a password: the root fallback uses the same deploy key as every other
|
||||
# connection (ansible_ssh_private_key_file). It is for a new LXC: the provisioning step gives root
|
||||
# that key and stock Ubuntu sshd (PermitRootLogin prohibit-password) admits root by key on first
|
||||
# boot. A new VM never needs it (cloud-init creates the configured user with the key; root does not
|
||||
# get it), and a fresh Proxmox node gets the key from the operator. Once mortis.kamino.sshd has run
|
||||
# (PermitRootLogin no on ubuntu_lxc/ubuntu_vm), root is refused: a host whose configured user broke
|
||||
# later is recovered from its Proxmox node (pct enter, qm terminal or the console), not here.
|
||||
# tasks/handback.yml switches the connection back to the configured user after it is created.
|
||||
- name: Fallback to root if user connection failed
|
||||
when: user_management_connection is failed
|
||||
block:
|
||||
- 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.
|
||||
ansible.builtin.set_fact: # noqa: var-naming[no-role-prefix]
|
||||
ansible_ssh_user: root
|
||||
|
||||
- name: Verify root access
|
||||
ansible.builtin.ping:
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
- name: Ensure sudo is installed
|
||||
when: user_management_install_sudo
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name: sudo
|
||||
state: present
|
||||
update_cache: true
|
||||
cache_valid_time: 3600
|
||||
|
||||
# -------------------------
|
||||
# PRIMARY ANSIBLE USER
|
||||
# -------------------------
|
||||
- name: Ensure ansible user is managed
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: "{{ user_management_ansible_user.name }}"
|
||||
shell: "{{ user_management_ansible_user.shell }}"
|
||||
groups: "{{ user_management_ansible_user.groups | default(omit) }}"
|
||||
# ansible-core 2.21 rejects append without groups, so append only goes with groups
|
||||
append: "{{ true if user_management_ansible_user.groups is defined else omit }}"
|
||||
create_home: "{{ user_management_ansible_user.create_home }}"
|
||||
password: "{{ user_management_ansible_user.password_hash }}"
|
||||
state: "{{ user_management_ansible_user.state }}"
|
||||
remove: "{{ user_management_remove_home }}"
|
||||
|
||||
# -------------------------
|
||||
# OTHER MANAGED USERS
|
||||
# -------------------------
|
||||
- name: Manage additional users
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: "{{ item.name }}"
|
||||
shell: "{{ item.shell | default('/bin/bash') }}"
|
||||
groups: "{{ item.groups | default(omit) }}"
|
||||
# ansible-core 2.21 rejects append without groups (also for state absent): pass it only with groups
|
||||
append: "{{ (item.append | default(true)) if item.groups is defined else omit }}"
|
||||
create_home: "{{ item.create_home | default(true) }}"
|
||||
password: "{{ item.password_hash | default(omit) }}"
|
||||
comment: "{{ item.comment | default(omit) }}"
|
||||
system: "{{ item.system | default(false) }}"
|
||||
state: "{{ item.state | default('present') }}"
|
||||
remove: "{{ user_management_remove_home }}"
|
||||
loop: "{{ user_management_other_users }}"
|
||||
loop_control:
|
||||
label: "{{ item.name }}"
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
- name: Check if /etc/cloud exists
|
||||
ansible.builtin.stat:
|
||||
path: /etc/cloud
|
||||
register: user_management_cloud_dir
|
||||
check_mode: false
|
||||
become: true
|
||||
|
||||
- name: Disable cloud-init permanently after first boot
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/cloud/cloud-init.disabled
|
||||
content: ""
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
become: true
|
||||
when: user_management_cloud_dir.stat.exists
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
# The root fallback in bootstrap.yml sets ansible_ssh_user as a fact, and a fact lasts for the
|
||||
# whole playbook run, not only this play. mortis.kamino.sshd later refuses root (PermitRootLogin
|
||||
# no), so a later play that still connected as root would only get in while ssh's ControlPersist
|
||||
# connection lived. Switch back to the configured user, now that it exists with its keys, and
|
||||
# prove the new connection works. No meta: reset_connection: it ignores `when` (so it ran on
|
||||
# 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).
|
||||
- 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 }}"
|
||||
|
||||
- name: Verify access as the configured user
|
||||
ansible.builtin.wait_for_connection:
|
||||
timeout: 30
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
# Locks root's password by replacing the shadow field with "!": no password logs root in,
|
||||
# and the old hash is gone, not just prefixed with "!" as usermod -L would leave it (a container
|
||||
# created with a shared password would otherwise keep that password's hash, open to an offline
|
||||
# crack from the container or a backup of it). The user module compares the fields without their
|
||||
# leading "!", so a second run reports ok. Only the password is touched: shell, home and
|
||||
# authorized_keys are not passed and stay as they are.
|
||||
#
|
||||
# This does not break the root fallback in tasks/bootstrap.yml: it logs root in with the deploy
|
||||
# key, never a password, and sshd with UsePAM yes (the Ubuntu default and the mortis.kamino.sshd
|
||||
# setting) does not treat a "!" hash as a locked account for key authentication. Recovery from the
|
||||
# host (pct enter, console as root) needs no password either.
|
||||
#
|
||||
# Not for a host whose root password is a real login (a Proxmox node: web UI and PAM realm), hence
|
||||
# the default false.
|
||||
- name: Refuse to lock the primary user's own password
|
||||
ansible.builtin.assert:
|
||||
that: user_management_ansible_user.name != 'root'
|
||||
fail_msg: >-
|
||||
user_management_lock_root_password is true but the primary user is root: locking would remove the
|
||||
password the role sets for it (and its become password). Connect as a non-root user or leave it false.
|
||||
quiet: true
|
||||
|
||||
- name: Lock root's password
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: root
|
||||
password: "!"
|
||||
update_password: always
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
- name: Combine all managed users
|
||||
no_log: true # the ansible user's password_hash is in this list; set_fact prints it at -v
|
||||
ansible.builtin.set_fact:
|
||||
user_management_all_users: >-
|
||||
{{
|
||||
[user_management_ansible_user]
|
||||
+ user_management_other_users
|
||||
}}
|
||||
tags: always
|
||||
|
||||
- name: Extract managed usernames
|
||||
ansible.builtin.set_fact:
|
||||
user_management_managed_usernames: >-
|
||||
{{ user_management_all_users | map(attribute='name') | list }}
|
||||
tags: always
|
||||
|
||||
- name: Bootstrap access (optional)
|
||||
when: user_management_bootstrap
|
||||
ansible.builtin.import_tasks: bootstrap.yml
|
||||
tags: always
|
||||
|
||||
- name: Manage system users
|
||||
ansible.builtin.import_tasks: create_users.yml
|
||||
tags: create_users
|
||||
|
||||
- name: Set SSH keys for created users
|
||||
ansible.builtin.import_tasks: ssh_keys.yml
|
||||
tags: ssh_keys
|
||||
|
||||
- name: Lock root's password (optional)
|
||||
when: user_management_lock_root_password
|
||||
ansible.builtin.import_tasks: lock_root.yml
|
||||
tags: lock_root_password
|
||||
|
||||
- name: Hand the connection back to the configured user
|
||||
when:
|
||||
- user_management_bootstrap
|
||||
- user_management_connection is failed
|
||||
- not ansible_check_mode # nothing was created in check mode, so the user cannot log in yet
|
||||
ansible.builtin.import_tasks: handback.yml
|
||||
tags: always
|
||||
|
||||
- name: Disable cloud-init management (if present)
|
||||
when: user_management_disable_cloud_init
|
||||
ansible.builtin.import_tasks: disable_cloud_init.yml
|
||||
tags: disable_cloud_init
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
- name: Ensure .ssh directory exists
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "/home/{{ item.name }}/.ssh"
|
||||
state: directory
|
||||
owner: "{{ item.name }}"
|
||||
group: "{{ item.name }}"
|
||||
mode: "0700"
|
||||
loop: "{{ user_management_all_users }}"
|
||||
loop_control:
|
||||
label: "{{ item.name }}"
|
||||
when:
|
||||
- item.state | default('present') == 'present'
|
||||
- not item.system | default(false)
|
||||
|
||||
- name: Enforce authorized_keys authoritatively
|
||||
become: true
|
||||
ansible.posix.authorized_key:
|
||||
user: "{{ item.name }}"
|
||||
key: "{{ (user_management_default_authorized_keys + (item.authorized_keys | default([]))) | join('\n') }}"
|
||||
state: present
|
||||
exclusive: "{{ user_management_authoritative }}"
|
||||
loop: "{{ user_management_all_users }}"
|
||||
when:
|
||||
- item.state | default('present') == 'present'
|
||||
- ( user_management_default_authorized_keys + (item.authorized_keys | default([])) ) | length > 0
|
||||
loop_control:
|
||||
label: "{{ item.name }}"
|
||||
Reference in new issue
Block a user