Categories

Add to requirements.yml

- src: https://github.com/geerlingguy/ansible-role-nfs
  name: geerlingguy.nfs
  version: master

Install Ansible role

ansible-galaxy install -r requirements.yml -p roles

Setup NFS Server Using Ansible

- hosts: nfs-server
  roles:
    - role: geerlingguy.nfs

Config nfs_exports

Allow only clients from IP matching nfs_network to access to NFS server. Allow nfs client connects to nfs server within the same machine using default IP of the inventory host.

---
nfs_network: "192.168.0.0/255.255.255.0"
nfs_exports:
  - /data/nfs {{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}(rw,sync,no_root_squash)
  - /data/nfs {{ nfs_network }}(rw,sync,no_root_squash)

Mount NFS from Other Linux Machines

Note: For ansible prior to Ansible 2.3, use name in mount module.

- hosts: nfs-client
  tasks:
    - name: Mount remote NFS folder
      mount:
        fstype: nfs
        name: /data/nfs_mounted
        src: "{{ hostvars[groups['nfs'][0]]['ansible_default_ipv4']['address'] }}:/data/nfs"
        state: mounted

Maintenance

Restart NFS in CentOS

service nfs restart
# or 
systemctl status nfs

Reference