πŸ“¦ 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:

πŸ”— Source repository: πŸ‘‰ https://github.com/t3knoid/ansible/tree/main/roles/autofs


✨ Key Features


🧰 Prerequisites


βš™οΈ Variables

πŸ”΄ 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:

  1. Master map (/etc/auto.master) Defines base mount points and associated automount maps
  2. Automount map (/etc/auto.nfs) Lists individual NFS mounts and their options
  3. Autofs daemon Monitors access and mounts/unmounts filesystems dynamically

πŸ” Role Execution Flow

  1. Install the autofs package
  2. Configure /etc/auto.master with a /nfs base path
  3. Populate /etc/auto.nfs with defined NFS mounts
  4. Enable and start the autofs service

πŸ—‚ Mount Path Generation

Mount paths are generated automatically:


πŸš€ 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:

/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

2️⃣ Configure auto.master

3️⃣ Manage auto.nfs

πŸ”„ Restart Handler


🧹 Removal Task

Optional uninstall via remove.yml:

- name: Remove autofs
  ansible.builtin.include_tasks: "roles/autofs/tasks/remove.yml"

Actions:


πŸ“š Ansible Module References


πŸ“Œ Examples in Repository