AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Mount Module: Mount NFS, CIFS & ansible.posix.mount Guide — Video Tutorial
How to mount NFS and CIFS shares in Linux with Ansible mount module (ansible.posix.mount). Configure /etc/fstab entries, set mount options, and manage.
What You'll Learn
- How to mount an NFS share in Linux with Ansible?
- Ansible mounts an NFS Share in Linux
- Parameters
- code
- execution
- before execution
- after execution
- Conclusion
- Related Articles
Full Tutorial Content
How to mount an NFS share in Linux with Ansible?
I'm going to show you a live Playbook 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.
## Playbook
Let's jump in a real-life Ansible Playbook to mount an NFS share in Linux RedHat-like and Debian-like.
code
- nfs.yml
```yaml
---
- name: mount module Playbook
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
```bash
$ ansible-playbook -i virtualmachines/demo/inventory mount\ drive/nfs.yml
PLAY [mount module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: installation
Read the full written article: Ansible Mount Module: Mount NFS, CIFS & ansible.posix.mount Guide
Related Video Tutorials
- Efficient NFS Server Setup with Ansible on Red Hat — Learn to automate the installation and configuration of an NFS server on Red Hat using an Ansible playbook. Enhance your system's efficiency now.
- Ansible mount Module: Mount Filesystems, NFS, SMB/CIFS Shares (Guide) — How to mount filesystems with Ansible mount module (ansible.posix.mount). Mount NFS, SMB/CIFS, Windows shares, configure fstab.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- How to Add a Disk to a VMware VM Using Ansible Playbook — Learn how to add a disk to a VMware VM using an Ansible playbook. This guide includes the YAML configuration, variables, and execution steps for easy.
- Add Secondary Groups to Linux Users with Ansible Playbook — Learn how to add secondary groups to Linux users with an Ansible playbook. This step-by-step guide includes YAML configuration and execution details.
- ansible.builtin.command Module: Run Ad-Hoc Commands & Tasks (Guide) — How to run commands with Ansible command module (ansible.builtin.command). Execute ad-hoc tasks, manage remote systems, capture output with register.