π¦ Contributor Guide: Mounting Disks Using fstab Using Ansible
The fstab role provides a declarative, idempotent, and state-aware way to manage filesystem mounts via /etc/fstab. Unlike traditional approaches that only append entries, this role ensures the systemβs mount configuration matches your desired state exactlyβadding new mounts and removing those no longer defined.
π Source repository: π https://github.com/t3knoid/ansible/tree/main/roles/fstab
Unlike autofs, which performs on-demand NFS automounting, this role uses static /etc/fstab entries, making it suitable for both NFS and local disk mounts.
β¨ Key Features
- Declarative β Define desired mounts; the role enforces that state
- Idempotent β Safe to run repeatedly; only changes when needed
- State-aware removal β Removes only mounts previously managed by this role
- Mount verification β Confirms a mount is active before unmounting
- Supports multiple filesystem types β NFS, ext4, xfs, vfat, and more
- Persistent mounts β Ensures mounts survive reboots via
/etc/fstab
π§° Prerequisites
- Target system must be Linux-based (Debian/Ubuntu recommended)
-
Install the required collection:
ansible-galaxy collection install ansible.posix
βοΈ Variables
π΄ Required
fstab_mounts (list): Array of mount configurations. Each entry must include:
-
deviceβ Device path or NFS export- Local:
/dev/sdb1,/dev/vdc1 - NFS:
192.168.2.240:/volume1/Photos,nfs-server:/export/share
- Local:
-
mount_pointβ Local mount path (e.g.,/data,/nfs/music)
π Optional
| Parameter | Type | Default | Description |
|---|---|---|---|
fstype |
string | ext4 |
Filesystem type (nfs, ext4, xfs, etc.) |
opts |
string | defaults |
Mount options (comma-separated) |
boot |
boolean | true |
Mount at system startup |
π§ How It Works
The role maintains declarative state by tracking which mounts it manages.
π Role Execution Flow
- Initialize β Load defaults and prepare variables
- Calculate changes β Compare defined mounts vs. previously managed ones
-
Remove old mounts
- Check if mounted
- Unmount
- Remove
/etc/fstabentry - Delete mount directory
-
Add/update mounts
- Create mount directory
- Add/update
/etc/fstabentry - Mount the filesystem
π§ State Tracking
The role uses the fact fstab_mounts_managed to ensure:
- Only mounts created by this role are removed
- Manually managed mounts remain untouched
- Behavior is consistent across runs
π Usage Examples
π’ Basic NFS Mounts
- hosts: media_servers
become: true
roles:
- fstab
vars:
fstab_mounts:
- device: "192.168.2.240:/volume1/Photos"
mount_point: "/nfs/photos"
fstype: "nfs"
opts: "rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys"
- device: "192.168.2.250:/mnt/Data/music"
mount_point: "/nfs/music"
fstype: "nfs"
opts: "rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys"
π Local Disk Mounts
fstab_mounts:
- device: "/dev/sdb1"
mount_point: "/data"
fstype: "ext4"
opts: "defaults,nofail"
- device: "/dev/sdc1"
mount_point: "/backup"
fstype: "ext4"
opts: "defaults,noatime"
π Mixed Mount Types
fstab_mounts:
- device: "/dev/sdb1"
mount_point: "/local-storage"
fstype: "ext4"
- device: "nfs-server:/export"
mount_point: "/remote-storage"
fstype: "nfs"
opts: "rw,sync,hard,intr"
- device: "/dev/sdc1"
mount_point: "/backup"
fstype: "xfs"
opts: "defaults"
π Idempotent Changes Example
- First run β mounts created and mounted
- Second run β removed mount is unmounted and deleted
- Third run β no changes; system already converged
π Configuration Files
/etc/fstab
Managed declaratively via ansible.posix.mount.
π§© Task Breakdown
1οΈβ£ Initialize & Calculate
- Apply defaults
- Determine removed mounts
- Update managed list
2οΈβ£ Remove Old Mounts
- Verify mount status
- Unmount if needed
- Remove fstab entry
- Delete directory
3οΈβ£ Add/Update Mounts
- Create mount directories
- Add/update fstab entries
- Mount filesystems
π Ansible Module References
ansible.builtin.fileansible.posix.mountansible.builtin.commandansible.builtin.set_fact