---
# ---
# - Remove unwanted users
# ---
- name: "(samba-remove-user.yml) Check if samba user exists for removable system user"
  ansible.builtin.shell: pdbedit -w -L | awk -F":" '{ print $1 }' | grep -q '{{ item.name }}'
  register: samba_remove_system_users_present
  changed_when: "samba_remove_system_users_present.rc == 0"
  failed_when: "samba_remove_system_users_present.rc > 1"
  with_items:
    - "{{ remove_samba_users }}"
  loop_control:
    label: "{{ item.name }}"
  tags:
    - system-user
    - samba-user

- name: (samba-remove-user.yml) Remove (old) system users from samba
  ansible.builtin.shell: >
    smbpasswd -s -x {{ item.item.name }}
  with_items:
    - "{{ samba_remove_system_users_present.results }}"
  loop_control:
    label: "{{ item.item.name }}"
  when:
    - item.changed
  tags:
    - system-user
    - samba-user

- name: (samba-remove-user.yml) Remove users from system
  ansible.builtin.user:
    name: "{{ item.name }}"
    state: absent
  with_items:
    - "{{ remove_samba_users }}"
  loop_control:
    label: "{{ item.name }}"
  tags:
    - system-user
    - samba-user

- name: (samba-remove-user.yml) Remove home directory from deleted users
  ansible.builtin.file:
    path: "{{ base_home | default('/home', true) }}/{{ item.name }}"
    state: absent
  with_items:
    - "{{ remove_samba_users }}"
  loop_control:
    label: "{{ item.name }}"
  tags:
    - system-user
    - samba-user
