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 Filesystems, NFS, SMB/CIFS Shares (Guide)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to mount filesystems with Ansible mount module (ansible.posix.mount). Mount NFS, SMB/CIFS, Windows shares, configure fstab.

Ansible mount Module: Mount Filesystems, NFS, SMB/CIFS Shares (Guide)

How to mount a Windows share in Linux SMB/CIFS with Ansible?

Ansible mount an SMB/CIFS filesystem

  • 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.

See also: Ansible Mount Module: Mount NFS, CIFS & ansible.posix.mount Guide

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
---
- 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

$ 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] ****************************************************************************
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=0    rescued=0    ignored=0

before execution

$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su 
[root@demo 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/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,rdma)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
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/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
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/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
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)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpu,cpuacct)
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)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18292)
debugfs on /sys/kernel/debug type debugfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel,pagesize=2M)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/1001 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=82872k,mode=700,uid=1001,gid=10)
[root@demo devops]# mount | grep share
[root@demo 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
[root@demo devops]#

after execution

$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo 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/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,rdma)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
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/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
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/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
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)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpu,cpuacct)
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)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18292)
debugfs on /sys/kernel/debug type debugfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel,pagesize=2M)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
tmpfs on /run/user/1001 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=82872k,mode=700,uid=1001,gid=10)
//windows-pc/share on /share type cifs (rw,relatime,vers=default,cache=strict,username=username,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.43.5,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=4194304,wsize=4194304,bsize=1048576,echo_interval=60,actimeo=1)
[root@demo devops]# touch /share/test
[root@demo devops]# ls -al /share/test 
-rwxr-xr-x. 1 root root 0 Nov 11 17:32 /share/test
[root@demo devops]# ls -al /share/
total 0
drwxr-xr-x.  2 root root   0 Nov 11 17:32 .
dr-xr-xr-x. 18 root root 237 Nov 11 17:31 ..
-rwxr-xr-x.  1 root root   0 Nov 11 17:32 test
[root@demo 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
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
#VAGRANT-END
//windows-pc/share /share cifs username=username,password=passowd 0 0
[root@demo devops]#

code with ❤️ in GitHub

Secure the credentials (avoid plaintext passwords)

The example above passes the password directly in opts, which exposes it in the playbook, in ps output, and in /etc/fstab. For anything beyond a quick test, store the credentials in a root-only file and reference it with the credentials mount option, and keep the file itself in Ansible Vault.

    - name: Deploy CIFS credentials file
      ansible.builtin.copy:
        dest: /etc/cifs-credentials
        owner: root
        group: root
        mode: '0600'
        content: |
          username={{ username }}
          password={{ password }}
      no_log: true

    - name: Mount network share with a credentials file
      ansible.posix.mount:
        src: "{{ uri }}"
        path: "{{ mountpoint }}"
        fstype: cifs
        opts: 'credentials=/etc/cifs-credentials,uid=1000,gid=1000,file_mode=0644,dir_mode=0755'
        state: mounted

no_log: true keeps the secret out of the Ansible log, mode: '0600' makes the credentials file readable only by root, and uid/gid/file_mode/dir_mode control ownership of the mounted files.

See also: Ansible for Windows: Complete Guide to Windows Automation (2026)

Persistent mounts and unmounting

The state parameter controls both the live mount and the /etc/fstab entry:

  • mounted — mount now and add it to /etc/fstab so it survives a reboot.
  • present — only write the /etc/fstab entry (mounts on next boot).
  • unmounted — unmount now but keep the /etc/fstab entry.
  • absent — unmount and remove the /etc/fstab entry.
    - name: Unmount the share and remove it from fstab
      ansible.posix.mount:
        path: "{{ mountpoint }}"
        state: absent

Conclusion

You now know how to mount an SMB/CIFS Windows share in Linux with Ansible using the ansible.posix.mount module — including how to keep the mount persistent across reboots and how to protect the share credentials with a root-only credentials file and Ansible Vault.

See also: ansible.posix.mount Module: Mount NFS, ext4, XFS Filesystems (Complete Guide)

Category: troubleshooting

Watch the video: Ansible mount Module: Mount Filesystems, NFS, SMB/CIFS Shares (Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home