- name: Install nfs-common
  ansible.builtin.apt:
    name: nfs-common
    state: present
  become: true
  when: ansible_os_family == "Debian"

- name: Ensure NFS mount directories exists
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: directory
    mode: '0755'
    owner: jarvis
    group: jarvis
  become: true
  loop: "{{ nfs_shares }}"
  loop_control:
    label: "Directory: {{ item.path }}"
  when: ansible_os_family == "Debian"

- name: Create local repogroup matching TrueNAS GID 568
  ansible.builtin.group:
    name: repogroup
    gid: 568
    state: present
  become: true

- name: Add jarvis to repogroup
  ansible.builtin.user:
    name: jarvis
    groups:
      - repogroup
    append: true
  become: true

- name: Mount an NFS volume (root, because kernel mount)
  ansible.posix.mount:
    src: "{{ item.src }}"
    path: "{{ item.path }}"
    opts: "vers=4.2,proto=tcp,_netdev"
    state: mounted
    fstype: nfs
  become: true
  loop: "{{ nfs_shares }}"
  loop_control:
    label: "Mounted: {{ item.src }}"
  when: ansible_os_family == "Debian"

- name: Set mount permissions so jarvis (repogroup member) can write
  ansible.builtin.file:
    path: "{{ item.path }}"
    mode: '0770'
    owner: root
    group: repogroup
  become: true
  loop: "{{ nfs_shares }}"
  loop_control:
    label: "Permission fix: {{ item.path }}"
  when: ansible_os_family == "Debian"
