126 lines
5 KiB
YAML
126 lines
5 KiB
YAML
---
|
|
# Create, update or remove the admin accounts listed in
|
|
# inventories/group_vars/all/users.yml, on every host they target.
|
|
#
|
|
# ansible-playbook playbooks/add_user.yml -CD # dry run first
|
|
# ansible-playbook playbooks/add_user.yml -l lxc
|
|
#
|
|
# Never touches root, accounts not in the list, or system accounts (uid < 1000).
|
|
- name: Manage admin accounts
|
|
hosts: all
|
|
gather_facts: true
|
|
gather_subset: [min]
|
|
vars:
|
|
add_user_present: "{{ add_user_managed | default([]) | selectattr('state', '==', 'present') | list }}"
|
|
add_user_absent: "{{ add_user_managed | default([]) | selectattr('state', '==', 'absent') | list }}"
|
|
|
|
tasks:
|
|
- name: Read existing accounts
|
|
ansible.builtin.getent:
|
|
database: passwd
|
|
|
|
- name: Keep the users that target this host, minus system accounts
|
|
ansible.builtin.set_fact:
|
|
add_user_managed: "{{ add_user_managed | default([]) + [{'state': 'present', 'sudo': 'nopasswd'} | combine(item)] }}"
|
|
loop: "{{ users }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
when:
|
|
- inventory_hostname in query('ansible.builtin.inventory_hostnames', item.hosts | default('all'))
|
|
- item.login not in getent_passwd or getent_passwd[item.login][1] | int >= 1000
|
|
|
|
- name: Warn about system accounts left alone
|
|
ansible.builtin.debug:
|
|
msg: "Skipping {{ item.login }}: it is a system account here (uid {{ getent_passwd[item.login][1] }})."
|
|
loop: "{{ users }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
when:
|
|
- inventory_hostname in query('ansible.builtin.inventory_hostnames', item.hosts | default('all'))
|
|
- item.login in getent_passwd and getent_passwd[item.login][1] | int < 1000
|
|
|
|
- name: Check the user list
|
|
ansible.builtin.assert:
|
|
that:
|
|
- item.state in ['present', 'absent']
|
|
- item.sudo in ['nopasswd', false]
|
|
- item.state == 'absent' or item.ssh_keys | default([]) | length > 0
|
|
- item.state == 'present' or item.login != ansible_user
|
|
fail_msg: >-
|
|
{{ item.login }}: state must be present/absent, sudo nopasswd/false,
|
|
ssh_keys non-empty, and the account Ansible connects with
|
|
({{ ansible_user }}) can't be removed.
|
|
quiet: true
|
|
loop: "{{ add_user_managed | default([]) }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
|
|
- name: Install sudo
|
|
ansible.builtin.package:
|
|
name: sudo
|
|
state: present
|
|
when: add_user_present | selectattr('sudo', '==', 'nopasswd') | list | length > 0
|
|
|
|
# Before the accounts: a shell that isn't installed means no login at all
|
|
# (on the k3s nodes un-oeil is the only way in).
|
|
- name: Install the login shells users ask for
|
|
ansible.builtin.apt:
|
|
name: "{{ add_user_present | selectattr('shell', 'defined') | map(attribute='shell')
|
|
| map('basename') | reject('in', ['bash', 'sh', 'dash']) | unique | list }}"
|
|
state: present
|
|
when: add_user_present | selectattr('shell', 'defined') | map(attribute='shell')
|
|
| map('basename') | reject('in', ['bash', 'sh', 'dash']) | list | length > 0
|
|
|
|
- name: Create or update accounts
|
|
ansible.builtin.user:
|
|
name: "{{ item.login }}"
|
|
# Only set the shell when asked, or for a new account (useradd's default is /bin/sh).
|
|
shell: "{{ item.shell | default(omit if item.login in getent_passwd else '/bin/bash') }}"
|
|
groups: "{{ item.groups | default(omit) }}"
|
|
append: "{{ true if item.groups is defined else omit }}"
|
|
create_home: true
|
|
state: present
|
|
loop: "{{ add_user_present }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
|
|
- name: Set authorized keys (exactly the listed ones)
|
|
ansible.posix.authorized_key:
|
|
user: "{{ item.login }}"
|
|
key: "{{ item.ssh_keys | join('\n') }}"
|
|
exclusive: true
|
|
state: present
|
|
loop: "{{ add_user_present }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
# In a dry run a new account doesn't exist yet, so there's nothing to compare.
|
|
when: not (ansible_check_mode and item.login not in getent_passwd)
|
|
|
|
- name: Allow passwordless sudo
|
|
ansible.builtin.copy:
|
|
dest: /etc/sudoers.d/{{ item.login }}
|
|
content: "{{ item.login }} ALL=(ALL) NOPASSWD: ALL\n"
|
|
owner: root
|
|
group: root
|
|
mode: "0440"
|
|
validate: /usr/sbin/visudo -cf %s
|
|
loop: "{{ add_user_present | selectattr('sudo', '==', 'nopasswd') }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
|
|
- name: Remove sudo rules that are no longer wanted
|
|
ansible.builtin.file:
|
|
path: /etc/sudoers.d/{{ item.login }}
|
|
state: absent
|
|
loop: "{{ add_user_absent + add_user_present | rejectattr('sudo', '==', 'nopasswd') }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|
|
|
|
- name: Remove accounts marked absent
|
|
ansible.builtin.user:
|
|
name: "{{ item.login }}"
|
|
state: absent
|
|
remove: true
|
|
loop: "{{ add_user_absent }}"
|
|
loop_control:
|
|
label: "{{ item.login }}"
|