Mounting and un-mounting a volume in ansible:
The mount module in Ansible provides ability to mount and un-mount filesystems and devices. In this post, we will focus on how to bind mount a volume and un-mount using Ansible.
Bind mounting a volume
An interesting stackoverflow post explains bind mount in layman terms.
A bind mount is an alternate view of a directory tree. Classically, mounting creates a view of a storage device as a directory tree. A bind mount instead takes an existing directory tree and replicates it under a different point. The directories and files in the bind mount are the same as the original. Any modification on one side is immediately reflected on the other side, since the two views show the same data.
One can mount a volume using ansible task as shown in the code snippet below. If state: mounted, the device will be actively mounted and appropriately configured in /etc/fstab. For bind mount, fstype can be none.
- name: Mount
mount:
path: /tmp/rohan
src: /etc
opts: bind
fstype: none
state: mounted
If state: present, /etc/fstab will be configured but device mount status will not change. If it was not mounted earlier, then it won’t be mounted. This is true for non-bind mounts too.
- name: Mount
mount:
path: /tmp/rohan
src: /etc
opts: bind
fstype: none
state: present
Content of /etc/fstab after bind mounting
[root@localhost ansible]# tail -1 /etc/fstab
/etc /tmp/rohan none bind 0 0
Content of /etc/fstab after mounting (non bind)
[root@localhost ansible]# tail -1 /etc/fstab
/etc /tmp/rohan none defaults 0 0
Un-mounting a volume
If state:unmounted, the device will be unmounted without changing /etc/fstab.
- name: Unmount
mount:
path: /tmp/rohan
state: unmounted
If state: absent, the device mount’s entry will be removed from /etc/fstab and will also unmount the device and remove the mount point.
- name: Unmount
mount:
path: /tmp/rohan
state: absent
A sample playbook is available here.