Ansible Setup NFS server and client

#Introduction

In this tutorial, How to Setup NFS server and client using ansible. I use vagrant. My example Ansible create multiple server here.

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 run command for NFS client

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

The output terminal as picture below

Conclusion

You have 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 →

3 thoughts on “Ansible Setup NFS server and client

    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!

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

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