---
- name: Install nfs-utils
  ansible.builtin.dnf:
    name: nfs-utils
    state: present

- name: Create NFS mount points
  ansible.builtin.file:
    path: "{{ item.local_path }}"
    state: directory
    mode: '0755'
  loop: "{{ nfs_client_mounts }}"

- name: Write NFS entries in fstab (sans monter immédiatement)
  ansible.posix.mount:
    src: "{{ nfs_server_ip }}:{{ item.remote_export }}"
    path: "{{ item.local_path }}"
    fstype: nfs
    # Options pour extinctions fréquentes :
    #   soft                         → échoue proprement (EIO) si serveur injoignable
    #   timeo=30 / retrans=3         → 3s timeout, 3 essais avant EIO
    #   _netdev                      → montage après réseau disponible seulement
    #   nofail                       → n'empêche pas le boot si NFS absent
    #   x-systemd.automount          → monté à la demande (premier accès), pas au boot
    #   x-systemd.mount-timeout=30   → 30s max pour réussir le montage à la demande
    opts: "nfsvers=4,soft,timeo=30,retrans=3,_netdev,nofail,x-systemd.automount,x-systemd.mount-timeout=30"
    state: present
    dump: 0
    passno: 0
  loop: "{{ nfs_client_mounts }}"
  notify: Reload systemd and activate automounts

- name: Reload systemd et activer les automounts NFS
  ansible.builtin.systemd:
    daemon_reload: true

- name: Enable and start NFS automount units
  ansible.builtin.systemd:
    name: "{{ item.local_path | replace('/', '-') | regex_replace('^-', '') }}.automount"
    enabled: true
    state: started
  loop: "{{ nfs_client_mounts }}"
