What you'll learn
- What is the Ansible mount Module?
- Mount an SMB/CIFS Windows Share with Ansible
- Parameters
- code
- execution
- before execution
- after execution
- Secure the credentials (avoid plaintext passwords)
- Persistent mounts and unmounting
- Conclusion
What is the Ansible mount Module?
The `ansible.posix.mount` module manages active and configured mount points on Linux systems, allowing you to mount filesystems—including NFS shares, SMB/CIFS shares from Windows, local block devices, and more—both immediately and persistently via `/etc/fstab`.
**Module name:** `ansible.posix.mount`
**Primary use cases:**
- Mount NFS (Network File System) shares
- Mount SMB/CIFS shares from Windows servers
- Mount local block devices and partitions
- Manage mount points via `/etc/fstab` to persist across reboots
- Control mount states: mount, unmount, or remove
For Windows hosts specifically, use the `community.windows.win_mapped_drive` module instead.
Mount an SMB/CIFS Windows Share with Ansible
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 SMB/CIFS filesystem.
code
- mount_cifs.yml
```yaml
---
- name: mount module Playbook
hosts: all
become: true
vars:
uri: "//windows-pc/share"
username: "example@domain"
password: "password"
mountpoint: "/share"
tasks:
- name: utility present
ansible.builtin.package:
name: cifs-utils
state: present
- name: check mountpoint exist
ansible.builtin.file:
path: "{{ mountpoint }}"
state: directory
mode: '0755'
owner: root
group: root
- name: Mount network share
ansible.posix.mount:
src: "{{ uri }}"
path: "{{ mountpoint }}"
fstype: cifs
opts: 'username={{ username }},password={{ password }}'
state: mounted
```
execution
```bash
$ ansible-playbook -i Playbook/inventory mount\ drive/cifs.yml
PLAY [mount module Playbook] *********************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [demo.example.com]
TASK [utility present] ***********************************************************************************
changed: [demo.example.com]
TASK [check mountpoint exist] ***********