feat: add user role
This commit is contained in:
parent
60b36db8a7
commit
ec0bc675bd
16
roles/user/README.md
Normal file
16
roles/user/README.md
Normal 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
|
||||
```
|
2
roles/user/defaults/main.yml
Normal file
2
roles/user/defaults/main.yml
Normal file
@ -0,0 +1,2 @@
|
||||
---
|
||||
users: []
|
32
roles/user/tasks/configure-user.yml
Normal file
32
roles/user/tasks/configure-user.yml
Normal 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 }}"
|
8
roles/user/tasks/main.yml
Normal file
8
roles/user/tasks/main.yml
Normal 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 }}"
|
Loading…
x
Reference in New Issue
Block a user