Mount an NFS share in Linux - Ansible module mount
How to mount an NFS share in Linux. The Ansible Playbook code is going to check the required packages, create the mount-point, and set up the NFS network shared folder using NFS4 protocol on the Linux target machine. Demo and live coding included for RedHat-like and Debian-like systems.


How to mount an NFS share in Linux with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible mounts an NFS Share in Linux
- ansible.posix.mount
- Control active and configured mount points
Today we’re talking about the Ansible module mount.
The full name is ansible.posix.mount
, which means that is part of the collection of modules “ansible.posix” to interact with POSIX platforms.
The purpose of the module is to control active and configured mount points.
For Windows, use the community.windows.win_mapped_drive
module instead.
Parameters
- path string - mount point (e.g. /mnt)
- state string - mounted / unmounted / present / absent / remounted
- src string - device or network volume
- fstype string - ext4, xfs, iso9660, nfs, cifs, etc.
- opts string- mount options
This module has many parameters to perform any task.
The only required are “path” and “state”.
The parameter “path” specifies the path to the mount point (e.g. /mnt/).
The parameter “state” allows us to verify a specific state of the mount point. The options “mounted”, “unmounted” and “remounted” change the device status. The “present” and “absent” options only change the /etc/fstab
file.
The src
parameter specifies the device or network volume for NFS or SMB/CIFS.
The fstype
parameter specifies the filesystem type. For example ext4, XFS, iso9660, NFS, CIFS, etc.
The opts
parameter allows us to specify some mount options, that vary for each filesystem type.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Let’s jump in a real-life Ansible Playbook to mount an NFS share in Linux RedHat-like and Debian-like.
code
- nfs.yml
---
- name: mount module demo
hosts: all
become: true
vars:
mynfs: "192.168.0.200:/nfs/share"
mountpoint: "/share"
permission: '0777'
myopts: 'rw,sync'
tasks:
- name: utility present redhat-like
ansible.builtin.yum:
name:
- nfs-utils
- nfs4-acl-tools
state: present
when: ansible_os_family == 'RedHat'
- name: utility present debian-like
ansible.builtin.apt:
name:
- nfs-common
- nfs4-acl-tools
state: present
when: ansible_os_family == 'Debian'
- name: check mountpoint exist
ansible.builtin.file:
path: "{{ mountpoint }}"
state: directory
mode: "{{ permission }}"
owner: root
group: root
- name: mount network share
ansible.posix.mount:
src: "{{ mynfs }}"
path: "{{ mountpoint }}"
fstype: nfs
opts: "{{ myopts }}"
state: mounted
execution
$ ansible-playbook -i virtualmachines/demo/inventory mount\ drive/nfs.yml
PLAY [mount module demo] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [utility present redhat-like] ****************************************************************
changed: [demo.example.com]
TASK [utility present debian-like] ****************************************************************
skipping: [demo.example.com]
TASK [check mountpoint exist] *********************************************************************
changed: [demo.example.com]
TASK [mount network share] ************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=4 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
before execution
$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.4 (Ootpa)
[[email protected] devops]# yum list nfs-utils
Updating Subscription Management repositories.
Last metadata expiration check: 0:28:59 ago on Wed 24 Nov 2021 11:17:15 AM UTC.
Available Packages
nfs-utils.x86_64 1:2.3.3-46.el8 rhel-8-for-x86_64-baseos-rpms
[[email protected] devops]# showmount --exports 192.168.0.200
bash: showmount: command not found
[[email protected] devops]# ls -al /share
ls: cannot access '/share': No such file or directory
[[email protected] devops]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Sep 1 00:06:23 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel_rhel8-root / xfs defaults 0 0
UUID=ae6c1777-c1c9-42a1-8fcf-513077aac39b /boot xfs defaults 0 0
/dev/mapper/rhel_rhel8-swap none swap defaults 0 0
[[email protected] devops]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=395492k,nr_inodes=98873,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime,seclabel)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,rdma)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpu,cpuacct)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_cls,net_prio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime,seclabel)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/rhel_rhel8-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel,pagesize=2M)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=38,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18508)
debugfs on /sys/kernel/debug type debugfs (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
tmpfs on /run/user/1001 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=82872k,mode=700,uid=1001,gid=10)
[[email protected] devops]#
after execution
$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# yum list nfs-utils
Updating Subscription Management repositories.
Last metadata expiration check: 0:31:28 ago on Wed 24 Nov 2021 11:17:15 AM UTC.
Installed Packages
nfs-utils.x86_64 1:2.3.3-46.el8 @rhel-8-for-x86_64-baseos-rpms
[[email protected] devops]# showmount --exports 192.168.0.200
Export list for 192.168.0.200:
/nfs/share 192.168.0.200/24
[[email protected] devops]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Sep 1 00:06:23 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel_rhel8-root / xfs defaults 0 0
UUID=ae6c1777-c1c9-42a1-8fcf-513077aac39b /boot xfs defaults 0 0
/dev/mapper/rhel_rhel8-swap none swap defaults 0 0
192.168.0.200:/nfs/share /share nfs rw,sync 0 0
[[email protected] devops]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=395492k,nr_inodes=98873,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime,seclabel)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,rdma)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpu,cpuacct)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_cls,net_prio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
none on /sys/kernel/tracing type tracefs (rw,relatime,seclabel)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/rhel_rhel8-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel,pagesize=2M)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=38,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18508)
debugfs on /sys/kernel/debug type debugfs (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
tmpfs on /run/user/1001 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=82872k,mode=700,uid=1001,gid=10)
192.168.0.200:/nfs/share on /share type nfs4 (rw,relatime,sync,vers=4.2,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.190,local_lock=none,addr=192.168.0.200)
[[email protected] devops]# ls -al /share
total 0
drwxrwxrwx. 2 root root 22 Nov 24 11:34 .
dr-xr-xr-x. 18 root root 237 Nov 24 11:47 ..
-rw-r--r--. 1 nobody nobody 0 Nov 24 11:34 test.txt
[[email protected] devops]# echo "test" > /share/test2.txt
[[email protected] devops]# ls -al /share
total 4
drwxrwxrwx. 2 root root 39 Nov 24 11:50 .
dr-xr-xr-x. 18 root root 237 Nov 24 11:47 ..
-rw-r--r--. 1 nobody nobody 0 Nov 24 11:34 test.txt
-rw-r--r--. 1 nobody nobody 5 Nov 24 11:50 test2.txt
[[email protected] devops]# cat /share/test2.txt
test
[[email protected] devops]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 387M 0 387M 0% /dev
tmpfs 405M 0 405M 0% /dev/shm
tmpfs 405M 11M 395M 3% /run
tmpfs 405M 0 405M 0% /sys/fs/cgroup
/dev/mapper/rhel_rhel8-root 70G 3.2G 67G 5% /
/dev/sda1 1014M 191M 824M 19% /boot
tmpfs 81M 0 81M 0% /run/user/1001
192.168.0.200:/nfs/share 70G 2.5G 68G 4% /share
[[email protected] devops]#
Recap
Now you know how to mount an NFS share in Linux with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate