NFS, or Network File Share is a service that allows client to store and share files and folders that will be stored in a centralized server.
Installing NFS server on RHEL 9 node is straight forwards. Here we are going to configure the Ansible server itself to host the NFS server too. The procedure is as follows:
[root@centos9vm ~]# mkdir /centos9vm_share
[root@centos9vm ~]# touch /centos9vm_share/file_1
[root@centos9vm ~]# dnf install nfs-utils -y
[root@centos9vm ~]# echo “/centos9vm_share *(ro)” >> /etc/exports
======
[root@centos9vm ~]# systemctl start nfs-server rpcbind
[root@centos9vm ~]# systemctl enable nfs-server rpcbind
======
[root@centos9vm ~]# showmount -e localhost
Export list for localhost:
/centos9vm_share *
======
Now let us see if we can attach this mountpoint on a client node 192.168.48.129. Let us create a playbook.
[root@centos9vm ~]# cat mount_nfs.yml
==== ==
– – –
– name: Playbook to mount NFS shared storage
hosts: 192.168.48.129
tasks:
– name: Task to mount NFS share
ansible.posix.mount:
src: 192.168.48.128:/centos9vm_share
path: /mnt
state: mounted
fstype: nfs
==== ===
[root@centosMYOBvm ~]# df -h | grep mnt
There will none when running the above command as we have unmounted the mount during the first half of this exercise. Now let us run the playbook on the controller node.
[root@centos9vm ~]# ansible-navigator run -m stdout mount_nfs.yml
=== === =
PLAY [Playbook to mount NFS shared storage] ************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]
TASK [Task to mount NFS share] *************************************************
changed: [192.168.48.129]
PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
==== ===
Let us check back on the managed node again. This time the NFS share should be added.
[root@centosMYOBvm ~]# df -h | grep mnt
192.168.48.128:/centos9vm_share 4.4G 3.0G 1.5G 68% /mnt
==== ===
Now let us see if we can unmount the share using a small tweak in the same playbook, but with the “state” parameter replacing the existing one.
[root@centos9vm ~]# cat mount_nfs.yml
===== ===
– – –
– name: Playbook to mount NFS shared storage
hosts: 192.168.48.129
tasks:
– name: Task to mount NFS share
ansible.posix.mount:
src: 192.168.48.128:/centos9vm_share
path: /mnt
state: unmounted
fstype: nfs
==== ====
[root@centos9vm ~]# ansible-navigator run -m stdout mount_nfs.yml
PLAY [Playbook to mount NFS shared storage] ************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]
TASK [Task to mount NFS share] *************************************************
changed: [192.168.48.129]
PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
=== ===
Let us check back on the managed node now.
[root@centosMYOBvm ~]# df -h | grep mnt
We should not see any output now.