- name: Setup autofs
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  block:
    - name: Include vault
      ansible.builtin.include_vars:
        file: jaydee.yml

    - name: Install nfs-server
      ansible.builtin.apt:
        name:
          - nfs-kernel-server
        state: present

    - name: Ensure nfsd thread count is set
      community.general.ini_file:
        path: /etc/nfs.conf
        section: nfsd
        option: threads
        value: "1"
        no_extra_spaces: true
        mode: '0644'
      when: inventory_hostname == 'rpi5.home.lan'

    - name: Mount and bind a volume
      ansible.posix.mount:
        path: /srv/nfs/downloads
        src: /media/m-server/downloads
        opts: bind
        state: mounted
        fstype: none
      when: inventory_hostname == 'm-server.home.lan'

    - name: Mount and bind a volume
      ansible.posix.mount:
        path: /srv/nfs/backup
        src: /mnt/raid/backup
        opts: bind
        state: mounted
        fstype: none
      when: inventory_hostname == 'amd.home.lan'

    - name: Mount and bind a volume
      ansible.posix.mount:
        path: /srv/nfs/docker_data
        src: /share/docker_data
        opts: bind
        state: mounted
        fstype: none
      when: inventory_hostname != 'amd.home.lan'

    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/fstab
        regexp: "^/share/{{ volume }} .*"
        line: "/media/m-server/{{ volume }}  /srv/nfs/{{ volume }}    none    bind    0       0"
      when: inventory_hostname == 'm-server.home.lan'
      loop_control:
        loop_var: volume
      loop: 
        - downloads
        - music
        - movies
        - shows

    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/fstab
        regexp: "^/share/{{ volume }} .*"
        line: "/share/{{ volume }}  /srv/nfs/{{ volume }}    none    bind    0       0"
      when: inventory_hostname == 'm-server.home.lan'
      loop_control:
        loop_var: volume
      loop: 
        - docker_data

    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/exports
        regexp: "^/srv/nfs .*"
        line: "/srv/nfs         192.168.77.0/24(rw,sync,no_subtree_check,crossmnt,fsid=0) 192.168.80.0/24(rw,sync,no_subtree_check,crossmnt,fsid=0)"

    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/exports
        regexp: "^/srv/nfs/docker_data .*"
        line: "/srv/nfs/docker_data 192.168.77.0/24(rw,sync,no_subtree_check) 192.168.80.0/24(rw,sync,no_subtree_check)"
      when: inventory_hostname != 'amd.home.lan'

    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/exports
        regexp: "^/srv/nfs/{{ volume }} .*"
        line: "/srv/nfs/{{ volume }} 192.168.77.0/24(rw,sync,no_subtree_check) 192.168.80.0/24(rw,sync,no_subtree_check)"
      when: inventory_hostname == 'm-server.home.lan'
      loop_control:
        loop_var: volume
      loop: 
        - downloads
        - music
        - movies
        - shows



    - name: Reconfigure nfs exports
      ansible.builtin.lineinfile:
        path: /etc/exports
        regexp: "^/srv/nfs/backup .*"
        line: "/srv/nfs/backup 192.168.77.0/24(rw,sync,no_subtree_check) 192.168.80.0/24(rw,sync,no_subtree_check)"
      when: inventory_hostname == 'amd.home.lan'
    - name: Restart nfs service
      ansible.builtin.service:
        name: nfs-server
        state: restarted
