---

# ---
# NFS Server
# ---

- name: (nfs.yml) Ensure NFS utilities (server) are installed.
  apt:
    name:
      - nfs-common
      - nfs-kernel-server
    state: present
  when:
    - ansible_os_family == "Debian"
    - "groups['nfs_server']|string is search(inventory_hostname)"
  tags:
    - nfs-server

- name: (nfs.yml) Ensure directories to export exist
  file:
    path: '{{ item.src.split(":")[1] }}'
    owner: root
    group: root
    mode: '0755'
    state: directory
  with_items: "{{ nfs_exports }}"
  loop_control:
    label: '{{ item.path }}'
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
  tags:
    - nfs-server

- name: (nfs.yml) Copy exports file.
  template:
    src: etc/exports.j2
    dest: /etc/exports
    owner: root
    group: root
    mode: 0644
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
  notify: Reload nfs
  tags:
    - nfs-server

- name: Enable service rpc-statd and ensure it is not masked
  systemd:
    name: rpc-statd
    enabled: yes
    masked: no
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"

- name: Make sure service rpc-statd is running
  systemd:
    state: started
    name: rpc-statd
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
  tags:
    - nfs-server

# ---
# /etc/default/nfs-kernel-server (Server)
# ---

- name: (nfs.yml) Check if file '/etc/default/nfs-kernel-server.ORIG' exists
  stat:
    path: /etc/default/nfs-kernel-server
  register: default_nfs_kernel_server_exists
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
  tags:
    - nfs-server

- name: (nfs.yml) Backup existing file /etc/default/nfs-kernel-server
  command: cp -a /etc/default/nfs-kernel-server /etc/default/nfs-kernel-server.ORIG
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
    - default_nfs_kernel_server_exists.stat.exists == False
  tags:
    - nfs-server

- name: (nfs.yml) Adjust file /etc/default/nfs-kernel-server - set 'RPCNFSDCOUNT' (server)
  replace:
    path: /etc/default/nfs-kernel-server
    regexp: '^RPCNFSDCOUNT=.*'
    replace: "RPCNFSDCOUNT={{ nfs_start_servers | default('16') }}"
  when:
    - "groups['nfs_server']|string is search(inventory_hostname)"
  tags:
    - nfs-server

# ---
# Configuration Server
# ---

- name: (nfs.yml) Ensure directory '/etc/nfs.conf.d' exists
  file:
    path: /etc/nfs.conf.d
    state: directory
    mode: 0755
    group: root
    owner: root
  when:
    - inventory_hostname in groups['nfs_server']
  tags:
    - nfs-server


- name: (nfs.yml) Create/Update file '/etc/nfs.conf.d/20-start_servers.conf' from template '20-start_servers.conf.j2'
  template:
    src: etc/nfs.conf.d/20-start_servers.conf.j2
    dest: /etc/nfs.conf.d/20-start_servers.conf
    owner: root
    group: root
    mode: 0644
  notify: "Restart nfs-kernel-server"
  when:
    - inventory_hostname in groups['nfs_server']
  tags:
    - nfs-server


# ---
# NFS clients
# ---

- name: (nfs.yml) Ensure NFS utilities (clients) are installed.
  apt:
    pkg: nfs-common
    state: present
  when: 
    - ansible_os_family == "Debian"
    - "groups['nfs_client']|string is search(inventory_hostname)"
  tags:
    - nfs-client

- name: (nfs.yml) NFS Mount exports from nfs server
  mount:
    path: "{{ item.path }}"
    src: "{{ item.src }}"
    fstype: nfs
    opts: "{{ item.mount_opts }}"
    dump: "{{ item.dump | default(omit) }}"
    passno: "{{ item.passno | default(omit) }}"
    state: mounted
  loop: "{{ nfs_exports }}"
  loop_control:
    label: '{{ item.src }}'
  when:
    - "groups['nfs_client']|string is search(inventory_hostname)"
  tags:
    - nfs-client



