feat: add user role

This commit is contained in:
transcaffeine 2025-01-12 10:24:03 +01:00
parent 60b36db8a7
commit ec0bc675bd
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
4 changed files with 58 additions and 0 deletions

16
roles/user/README.md Normal file
View File

@ -0,0 +1,16 @@
# `finallycoffee.base.user` ansible role
## Examples
```yaml
- hosts: all
roles:
- role: finallycoffee.base.user
vars:
users:
- name: root
- name: alice
- name: bob
state: present
- name: eve
state: absent
```

View File

@ -0,0 +1,2 @@
---
users: []

View File

@ -0,0 +1,32 @@
---
- name: Ensure user '{{ user.name }}' is {{ user.state }}
ansible.builtin.user:
name: "{{ user.name }}"
state: "{{ user.state }}"
system: "{{ user.system | default(false, true) }}"
home: "{{ user.home | default(omit, true) }}"
create_home: "{{ user.create_home | default(true, true) }}"
comment: "{{ user.comment | default(user.gecos | default(omit, true), true) }}"
- name: Ensure SSH authorized keys for '{{ user.name }}' are {{ user.state }}
when:
- user.state == 'present'
- user.authorized_keys | default([]) | length > 0
block:
- name: Ensure .ssh directory for user '{{ user.name }}' exists
ansible.builtin.file:
path: "{{ user.home | default('/home/' + user.name) + '/.ssh' }}"
state: "directory"
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: "0700"
- name: Ensure key is up to date
ansible.posix.authorized_key:
user: "{{ user.name }}"
state: "{{ key.state | default('present', true) }}"
key: "{{ key.key }}"
comment: "{{ user.name }}-{{ key.comment }}"
loop: "{{ user.authorized_keys }}"
loop_control:
loop_var: key
label: "{{ user.name }}-{{ key.comment }}"

View File

@ -0,0 +1,8 @@
---
- name: Ensure users are configured
ansible.builtin.include_tasks:
file: "configure-user.yml"
loop: "{{ users }}"
loop_control:
loop_var: user
label: "{{ user.name }}"