- 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 autofs
      ansible.builtin.apt:
        name:
          - autofs
          - cifs-utils
        state: present

    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.auth"
        content: |
          username={{ samba_user }}
          password={{ samba_password }}
        mode: '0600'
        owner: root
        group: root
    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.m-server"
        content: |
          docker_data  -fstype=nfs   m-server.home.lan:/docker_data
          downloads  -fstype=nfs   m-server.home.lan:/downloads
          movies  -fstype=nfs   m-server.home.lan:/movies
          shows  -fstype=nfs   m-server.home.lan:/shows
          music  -fstype=nfs   m-server.home.lan:/music
        mode: '0600'
        owner: root
        group: root
      when: inventory_hostname != 'm-server.home.lan'
    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.amd"
        content: |
          backup  -fstype=nfs   amd.home.lan:/backup

        mode: '0600'
        owner: root
        group: root
      when: inventory_hostname == 'm-server.home.lan'
    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.rack"
        content: |
          docker_data  -fstype=nfs   rack.home.lan:/docker_data
        mode: '0600'
        owner: root
        group: root
      when: inventory_hostname != 'rack.home.lan'

    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.rpi5"
        content: |
          docker_data  -fstype=nfs   rpi5.home.lan:/docker_data
        mode: '0600'
        owner: root
        group: root
      when: inventory_hostname != 'rpi5.home.lan'

    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-movies"
    #     content: |
    #       movies  -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/movies
    #     mode: '0600'
    #     owner: root
    #     group: root

    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-movies"
    #     content: |
    #       movies --fstype=nfs,rw nas.home.lan:/movies
    #     mode: '0600'
    #     owner: root
    #     group: root
    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-music"
    #     content: |
    #       music --fstype=nfs,rw nas.home.lan:/music
    #     mode: '0600'
    #     owner: root
    #     group: root
    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-shows"
    #     content: |
    #       shows --fstype=nfs,rw nas.home.lan:/shows
    #     mode: '0600'
    #     owner: root
    #     group: root        
    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-music"
    #     content: |
    #       music   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/music
    #     mode: '0600'
    #     owner: root
    #     group: root

    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-music"
    #     content: |
    #       music   --fstype=nfs,rw ://nas.home.lan/music
    #     mode: '0600'
    #     owner: root
    #     group: root

    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas-shows"
    #     content: |
    #       shows   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/shows
    #     mode: '0600'
    #     owner: root
    #     group: root

    # - name: Creating a file with content
    #   ansible.builtin.copy:
    #     dest: "/etc/auto.nas"
    #     content: |
    #       nas-data   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/Data
    #       nas-docker-data   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/docker_data
    #       nas-photo -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/Photo
    #       nas-public   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/Public
    #       nas-install   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/install
    #       nas-downloads   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw    ://nas.home.lan/downloads
    #       nas-games   -fstype=cifs,credentials=/etc/auto.auth,dir_mode=0755,file_mode=0755,uid=jd,rw  ://nas.home.lan/qda_2
    #     mode: '0600'
    #     owner: root
    #     group: root

    - name: Creating a file with content
      ansible.builtin.copy:
        dest: "/etc/auto.nas"
        content: |
          docker-data   --fstype=nfs,rw nas.home.lan:/docker_data
          data   --fstype=nfs,rw nas.home.lan:/Data
          qda_1   --fstype=nfs,rw nas.home.lan:/qda_1
          photo  --fstype=nfs,rw nas.home.lan:/Photo
          install  --fstype=nfs,rw nas.home.lan:/install
          games  --fstype=nfs,rw nas.home.lan:/games
          movies --fstype=nfs,rw nas.home.lan:/movies
          music --fstype=nfs,rw nas.home.lan:/music
          shows --fstype=nfs,rw nas.home.lan:/shows
          xxx --fstype=nfs,rw nas.home.lan:/xxx
          proxmox --fstype=nfs,rw nas.home.lan:/proxmox
          live --fstype=nfs,rw nas.home.lan:/live
        mode: '0600'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'
    # - name: Reconfigure autofs Server
    #   ansible.builtin.lineinfile:
    #     path: /etc/auto.master
    #     regexp: "^/media/nas.*"
    #     insertafter: '^/media/nas'
    #     line: "/media/nas   /etc/auto.nas --timeout 360 --ghost"

    # - name: Reconfigure autofs Server
    #   ansible.builtin.lineinfile:
    #     path: /etc/auto.master
    #     regexp: "^/media/data/music/nas.*"
    #     line: /media/data/music/nas   /etc/auto.nas-music --timeout 360 --ghost
    # - name: Reconfigure autofs Server
    #   ansible.builtin.lineinfile:
    #     path: /etc/auto.master
    #     regexp: "^/media/data/movies/nas.*"
    #     line: /media/data/movies/nas   /etc/auto.nas-movies --timeout 360 --ghost

    # - name: Reconfigure autofs Server
    #   ansible.builtin.lineinfile:
    #     path: /etc/auto.master
    #     regexp: "^/media/data/shows/nas.*"
    #     line: /media/data/shows/nas   /etc/auto.nas-shows --timeout 360 --ghost


    - name: Reconfigure autofs Server
      ansible.builtin.lineinfile:
        path: /etc/auto.master
        regexp: "^/media/nas.*"
        line: /media/nas   /etc/auto.nas --timeout 360 --ghost

    - name: Reconfigure autofs Server
      ansible.builtin.lineinfile:
        path: /etc/auto.master
        regexp: "^/media/m-server.*"
        line: /media/m-server  /etc/auto.m-server --timeout 360 --ghost

    - name: Reconfigure autofs Server
      ansible.builtin.lineinfile:
        path: /etc/auto.master
        regexp: "^/media/rack.*"
        line: /media/rack  /etc/auto.rack --timeout 360 --ghost

    - name: Reconfigure autofs Server
      ansible.builtin.lineinfile:
        path: /etc/auto.master
        regexp: "^/media/rpi5.*"
        line: /media/rpi5  /etc/auto.rpi5 --timeout 360 --ghost
    - name: Reconfigure autofs Server
      ansible.builtin.lineinfile:
        path: /etc/auto.master
        regexp: "^/media/amd.*"
        line: /media/amd  /etc/auto.amd --timeout 360 --ghost

    - name: Restart docker service
      ansible.builtin.service:
        name: autofs
        state: restarted
