homeserver/roles/mgrote_users/tasks/main.yml

48 lines
1.4 KiB
YAML
Raw Normal View History

---
2024-10-08 16:45:12 +02:00
- name: Set groups as list
2024-10-08 16:42:02 +02:00
ansible.builtin.set_fact:
groups_as_list: "{{ ((( item.groups ) | list) | sort) | unique }}"
loop: "{{ users }}"
when: item.groups is defined
2024-10-08 16:45:12 +02:00
- name: Ensure groups exist
ansible.builtin.group:
2024-10-08 16:42:02 +02:00
name: "{{ item }}"
state: present
2024-10-08 16:42:02 +02:00
loop: '{{ groups_as_list }}'
2024-10-08 16:42:38 +02:00
when: groups_as_list is defined
2024-10-08 16:49:30 +02:00
no_log: true
2024-10-08 16:45:12 +02:00
- name: Ensure users exist
ansible.builtin.user:
name: "{{ item.username }}"
uid: "{{ item.uid | default(omit) }}"
shell: "{{ item.shell | default('/bin/bash') }}"
password: "{{ item.password }}"
update_password: "{{ item.update_password | default(omit) }}"
groups: "{{ item.groups | default(omit) }}"
createhome: "{{ item.createhome | default('yes') }}"
state: "{{ item.state | default('present') }}"
loop: '{{ users }}'
2024-10-08 16:49:30 +02:00
no_log: true
2024-10-08 16:45:12 +02:00
- name: Ensure user ssh-keys exist
ansible.posix.authorized_key:
user: "{{ item.username }}"
key: "{{ item.public_ssh_key }}"
state: present
when: item.public_ssh_key is defined
loop: '{{ users }}'
2024-10-08 16:49:30 +02:00
no_log: true
2024-10-08 16:45:12 +02:00
- name: Ensure users are added to sudoers
ansible.builtin.lineinfile:
dest: /etc/sudoers
state: present
regexp: '^{{ item.username }} '
line: "{{ item.username }} ALL=(ALL) {{ 'NOPASSWD:' if (item.allow_passwordless_sudo | d(false)) else '' }}ALL"
validate: 'visudo -cf %s'
when: item.allow_sudo|default(false) and item.allow_sudo is defined
loop: '{{ users }}'
2024-10-08 16:49:30 +02:00
no_log: true