Files
ansible-role-unix-users/tasks/remove_unix_user.yml
T
volker.raschekandCopilot afd4288ebd
Lint Markdown files / markdown-lint (push) Successful in 14s
Molecule / Molecule (push) Successful in 5m19s
Ansible Linter / ansible-lint (push) Successful in 4m22s
refactor: use the yml extension for every yaml file
The repository mixed both extensions, the molecule scenario used yml while the role itself used yaml. The yml
extension is the one ansible-galaxy and molecule generate, so it is applied throughout and across the sibling roles.

The include_tasks calls in tasks/main.yml and tasks/create_unix_user.yml name their target explicitly and were
adjusted accordingly, otherwise the role would no longer find the included task files.

Co-authored-by: Copilot <copilot@github.com>
2026-09-10 22:06:43 +02:00

38 lines
1.4 KiB
YAML

---
- name: "Define home directory for unix user: {{ unix_user.key }}"
ansible.builtin.set_fact:
_unix_users_home: "{{ unix_user.value.home | default('/home/' + unix_user.key) }}"
# userdel cannot remove a btrfs subvolume. Such a home is deleted afterwards via the btrfs_subvolume module.
- name: "Remove unix user: {{ unix_user.key }}"
ansible.builtin.user:
name: "{{ unix_user.key }}"
state: absent
remove: "{{ not (unix_user.value.btrfs | default(false)) }}"
- name: "Remove btrfs home of unix user: {{ unix_user.key }}"
when: unix_user.value.btrfs is defined and
unix_user.value.btrfs
block:
- name: "Stat home directory"
ansible.builtin.stat:
path: "{{ _unix_users_home }}"
register: _unix_users_home_stat
# findmnt fails on a missing path, so the device is only determined as long as the home directory exists.
- name: "Delete btrfs subvolume of an existing home directory"
when: _unix_users_home_stat.stat.exists
block:
- name: "Find btrfs device"
ansible.builtin.command:
cmd: /bin/bash -c "findmnt -no SOURCE -T {{ _unix_users_home }} | sed 's/\[.*\]//'"
register: _unix_users_btrfs_device
changed_when: false
- name: "Delete btrfs subvolume: {{ _unix_users_home }}"
community.general.btrfs_subvolume:
filesystem_device: "{{ _unix_users_btrfs_device.stdout }}"
name: "{{ _unix_users_home }}"
state: absent