This article will learn how to use the Ansible mount module to manage mount points on remote systems.
Install Ansible Mount (POSIX) module
The ansible mount module is part of Ansible.posix.collections and hence may not be available in all Ansible installations by default.
To use it, we need to install it from the Ansbile Galaxy. Open the terminal and enter the command:
The command above will install the Posix collections.
We will use example playbooks to understand better how to use the mount module in Ansible.
Example 1 – Mount a device
We call the mount module and provide the required parameters to mount a device using the Ansible playbook. The most important ones are path and src.
The path parameter defines the path to the mount point, while the src parameter defines the device or volume to be mounted to the specified path.
The example playbook below shows you how to mount a device using its label.
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Mount a device by label
mount:
path: /mounts/backups
src: LABEL=Backup
fstype: ext4
state: present
The playbook above will mount the device with the label “Backup” to /mounts/backups on the remote host.
We also define the filesystem in the fstype parameter. Finally, we describe the state of the mount point.
The mount module supports the following states:
- Mounted – When the state is set to mounted, the device will be mounted and configured in the fstab. Ansible will automatically create the mount point if it does not exist.
- Unmounted – if set to unmounted, the specified device will be mounted with no changes to the fstab.
- Present – If set to present, the device is configured in fstab, with no need for a mount point.
- Absent – if absent, Ansible will remove the device’s mount entry from fstab and remove its mount point.
- Remounted – used when you want to remount a device. Typically used to refresh the mount point.
Example 2 – Mount on NTFS filesystem.
To mount a device in the NTFS filesystem, change the fstype as shown in the example playbook below.
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Mount a device by label
mount:
path: /mounts/backups
src: LABEL=Backup
fstype: ntfs
state: present
Example 3 – Mount and Bind Volume
To mount and bind a volume using the mount module, use an example playbook as shown:
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Mount and bind volume
mount:
path: /mnt/dev10
src: /dev/mountMe
opts: bind
fstype: none
state: mounted
In the example above, we use the mount option, bind to bind the volume. Check fstab mount options to see the available options.
Example 4 – Mount a device by UUID
Instead of a label, you can use the UUID to mount a specific device. Consider the example playbook below:
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Mount device via UUID
mount:
path: /mnt/dev10
src: UUID=39717898-48ea-11ec-81d3-0242ac130003
opts: defaults
fstype: ext4
state: present
Example 5 – Unmounting a Volume
To unmount a mounted volume using the Ansible mount module, set the state to unmounted as shown in the playbook below:
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Unmount a volume
mount:
path: /mnt/dev10
state: unmounted
The unmount option does not edit the fstab. For that, you can use the state: absent as shown in the playbook below:
Example 6 – Unmounting Volume and edit fstab
- name: Ansible Mount Module
hosts: all
gather_facts: false
become: true
tasks:
- name: Unmount a volume
mount:
path: /mnt/dev10
state: absent
The example above will unmount and edit the fstab to remove the mount point.
Closing
In this guide, we discussed how to work with the Ansible mount module and looked at various use-cases of the module.
Thank you for reading.