---
- name: Check if nfs is installed
  ignore_errors: true
  command: systemctl status nfs-kernel-server
  register: nfs_server_install

- name: Install nfs
  command: apt update && apt install -y nfs-kernel-server
  when: .nfs_server_install.stderr | ne ""

- name: Create nfs share directory
  command: |
    if [ ! -d {{ .item }} ]; then
      mkdir -p {{ .item }}
      chmod -R 0755 {{ .item }}
      chown nobody:nogroup {{ .item }}
    fi
  loop: "{{ .nfs.share_dir | toJson }}"

- name: Generate nfs config
  template:
    src: exports
    dest: /etc/exports

- name: Export share directory and start nfs server
  command: |
    exportfs -a
    systemctl enable nfs-kernel-server && systemctl restart nfs-kernel-server 
