---
- name: Update APT cache
  apt:
    update_cache: yes
  failed_when: false

- name: Install NFS Kernel
  apt:
    name: nfs-kernel-server
    state: present

- name: Enable and start NFS Server
  service:
    name: nfs-server
    enabled: yes
    state: started

- name: Backup existing exports if not already moved
  command: mv /etc/exports /home/jingrow/exports
  args:
    creates: /home/jingrow/exports

- name: Remove default exports
  file:
    path: /etc/exports
    state: absent

- name: Create export symlink
  file:
    src: /home/jingrow/exports
    dest: /etc/exports
    state: link
    owner: root
    group: root

- name: Ensure ownership of exports
  file:
    path: /home/jingrow/exports
    owner: jingrow
    group: jingrow
    mode: '0644'

- name: Allow jingrow to run exportfs
  lineinfile:
    path: /etc/sudoers.d/jingrow
    line: 'jingrow ALL=(root) NOPASSWD: /usr/sbin/exportfs'
    create: yes
    mode: '0440'
    owner: root
    group: root
    validate: 'visudo -cf %s'
