Ansible Setup NFS server and client

#Introduction

In this tutorial, How to set up an NFS server and client using Ansible. I use Vagrant. In my example, Ansible creates multiple servers here. Now, let’s go to the Ansible Setup NFS server and client.

Ansible file and folder

[vagrant@ansible_controller ~]$ tree .
.
├── ansible
│   ├── ansible.cfg
│   ├── exports.j2
│   └── hosts
├── nfs-client.yml
└── nfs-server.yml

1 directory, 5 files

Ansible script

nfs-server.yml file for NFS Server as below

---
- hosts: nfs-server
  become: yes
  tasks:
    - name: install nfs-utils
      yum: name=nfs-utils state=latest

    - name: Create a mountable directory if it does not exist
      file:
        path: /home/vagrant/nfs_test
        state: directory
        owner: vagrant
        group: vagrant
        mode: '0775'
    - name: enable rpcbind nfslock nfs
      service:
        name: "{{ item }}"
        enabled: yes
      with_items:
        - rpcbind
        - nfslock
        - nfs
    - name: Copy exports file.
      template:
        src: ./ansible/exports.j2
        dest: /etc/exports
        owner: root
        group: root
        mode: 0644
    - name: NFS apply change configrue
      shell: systemctl reload nfs;exportfs -a

nfs-client.yml file for nfs clients as below

---
- hosts: nfs-clients
  become: yes
  tasks:
    - name: install nfs-utils
      yum: name=nfs-utils state=latest

    - name: Create a mountable directory if it does not exist
      file:
        path: /mnt/web_storage
        state: directory
        owner: vagrant
        group: vagrant
        mode: '0775'
    - name: Mount volumn
      shell: sudo mount 192.168.3.9:/home/vagrant/nfs_test /mnt/web_storage

exports.j2 file with content as below

# /etc/exports: the access control list for filesystems which may be exported
#   to NFS clients.  See exports(5).
/mnt/nfs_test            *(rw,sync,no_root_squash,no_subtree_check)

My example hosts file as below

[nfs-server]
servernfs

[nfs-clients]
server1

File ansible/ansible.cfg example as below

[defaults]
inventory = ./hosts
forks = 15
log_path=$HOME/ansible/ansible.log
host_key_chcking = False
gathering = smart

Ansible run command for NFS server

[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts nfs-server.yml

The output terminal as picture below

Ansible Setup NFS server and client

Ansible run command for NFS client

[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts nfs-client.yml

The output terminal as picture below

Ansible Setup NFS server and client

Conclusion

You have to use ansible setup NFS server and NFS client. I hope will this your helpful. Thank you for reading the DevopsRoles page!

, ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

5 thoughts on “Ansible Setup NFS server and client

  1. it does not mount when i try
    shell: sudo mount 192.168.3.9:/home/vagrant/nfs_test /mnt/web_storage

    1. My example /etc/hosts as below:

      [vagrant@ansible_controller ~]$ cat /etc/hosts
      127.0.0.1 localhost
      127.0.1.1 ansible_controller
      192.168.3.10 ansible_controller
      192.168.3.11 server1
      192.168.3.9 servernfs

      Thanks, have a nice day!

  2. Hi @jack,
    you can check connection from nfs client to nfs server. telnet 192.168.3.9 111
    Check /etc/hosts ?
    thanks

  3. There might be a mistake here – you create a “/home/vagrant/nfs_test” but then you add “/mnt/nfs_test” to your exports file. These paths should be the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.