π¦ Contributor Guide: Creating Autofs Mounts Using Ansible
The autofs role installs and configures the autofs automounter on Debian/Ubuntu systems. Autofs provides transparent, on-demand NFS mountsβfilesystems are mounted automatically when accessed and unmounted after a period of inactivity.
This makes it ideal for:
- Large numbers of NFS shares
- Environments where boot-time mounts cause delays
- Media servers and shared resources
π Source repository: π https://github.com/t3knoid/ansible/tree/main/roles/autofs
β¨ Key Features
- Automatic NFS mounting β Mounts appear instantly on access
- Automatic unmounting β Frees resources after idle timeout
- Transparent to users β No manual mount/unmount required
- Scalable β Efficiently handles dozens or hundreds of NFS shares
- Service-based β Managed via the autofs daemon
- Dynamic configuration β Add or remove mounts without reboots
π§° Prerequisites
- Target OS: Debian or Ubuntu
- Minimum Ansible version: 2.9
- Network access to NFS servers
- Root or
becomeaccess on target hosts
βοΈ Variables
π΄ Required
-
autofs_nfs_mounts(list): Array of NFS mount definitions. Each entry must include:mount_name(string): Short name used in the mount pathserver(string): NFS export inserver:/pathformatmount_options(string): NFS mount options (leading-required)
π Mount Structure
Each mount follows this structure:
- mount_name: music
server: 192.168.2.250:/mnt/Data/music
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
π§ How It Works
π Autofs Architecture
Autofs relies on a hierarchical configuration model:
- Master map (
/etc/auto.master) Defines base mount points and associated automount maps - Automount map (
/etc/auto.nfs) Lists individual NFS mounts and their options - Autofs daemon Monitors access and mounts/unmounts filesystems dynamically
π Role Execution Flow
- Install the
autofspackage - Configure
/etc/auto.masterwith a/nfsbase path - Populate
/etc/auto.nfswith defined NFS mounts - Enable and start the autofs service
π Mount Path Generation
Mount paths are generated automatically:
- Base path:
/nfs - Mount name:
music - Resulting path:
/nfs/music
π Usage Examples
π’ Basic NFS Mounts
- hosts: media_servers
become: true
roles:
- autofs
vars:
autofs_nfs_mounts:
- mount_name: photos
server: 192.168.2.240:/volume1/Photos
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
- mount_name: music
server: 192.168.2.250:/mnt/Data/music
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
π With Group Variables
# inventory/media/group_vars/all/main.yml
autofs_nfs_mounts:
- mount_name: photos
server: 192.168.2.240:/volume1/Photos
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
- mount_name: music
server: 192.168.2.250:/mnt/Data/music
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
- mount_name: books
server: 192.168.2.250:/mnt/Data/books
mount_options: -rw,relatime,hard,rsize=1048576,wsize=1048576,proto=tcp,timeo=600,retrans=2,sec=sys
Then reference the role:
- hosts: media_servers
become: true
roles:
- autofs
π Configuration Files
/etc/auto.master
/nfs /etc/auto.nfs
Defines:
/nfsas the base mount directory/etc/auto.nfsas the automount map
/etc/auto.nfs
# {mark} ANSIBLE MANAGED BLOCK
photos -rw,relatime,hard,... 192.168.2.240:/volume1/Photos
music -rw,relatime,hard,... 192.168.2.250:/mnt/Data/music
books -rw,relatime,hard,... 192.168.2.250:/mnt/Data/books
# {mark} ANSIBLE MANAGED BLOCK
Uses blockinfile markers to ensure idempotent updates.
π§© Task Breakdown
1οΈβ£ Install autofs
- Installs the package
- Updates the package cache
2οΈβ£ Configure auto.master
- Deploys template
- Sets ownership and permissions
- Triggers restart on change
3οΈβ£ Manage auto.nfs
- Uses
blockinfile - Preserves unmanaged entries
- Triggers restart on change
π Restart Handler
- Restarts autofs only when configuration changes occur
π§Ή Removal Task
Optional uninstall via remove.yml:
- name: Remove autofs
ansible.builtin.include_tasks: "roles/autofs/tasks/remove.yml"
Actions:
- Stops and disables autofs
- Removes the package
- Cleans up configuration files
π Ansible Module References
ansible.builtin.aptansible.builtin.templateansible.builtin.blockinfileansible.builtin.service
π Examples in Repository
inventory/semaphore/group_vars/all/main.ymlplaybooks/grafana/create_db.yml