I need to check if a directory is a mount point before doing some other tasks.
I have been looking around the documentation and it only seems that you can create/destroy mount points but not just check if one exists. From the link below.
http://docs.ansible.com/ansible/mount_module.html
I am wondering if there is any way to check it exists with ansible, or will it have to be some other language called from ansible.
Using the mount Command One way we can determine if a directory is mounted is by running the mount command and filtering the output. The above line will exit with 0 (success) if /mnt/backup is a mount point. Otherwise, it'll return -1 (error).
To find out what drives are mounted you can check /etc/mtab , which is a list of all devices mounted on the system. It can sometimes have various tmpfs and other things you aren't looking for mounted too, so I reccomend cat /etc/mtab | grep /dev/sd to get only physical devices.
Viewing mount options of filesystem To see what options a mounted filesystem is utilizing run the mount command can be ran without any arguments. You can also grep for a particular mount point as sometimes (specially if you are using RHEL/CentOS 7) you might get a huge list of system mount points.
You can check this using the ansible_mounts
fact, without running an extra command, although it's somewhat convoluted.
- debug:
msg: "{{ mount_point }} is mounted"
when: ansible_mounts | selectattr('mount', 'equalto', mount_point) | list | length > 0
- debug:
msg: "{{ mount_point }} is not mounted"
when: ansible_mounts | selectattr('mount', 'equalto', mount_point) | list | length == 0
where mount_point
is the directory path to check.
Note that Ansible facts don't automatically get updated, so if you mount or unmount the directory during the play the results will be out of date unless you update them by re-running the setup module:
- name: Gather facts again to update mount points
setup:
I've tried with both mount
and stat
module. Both didn't met your requirements.
I've manage to work only using a OS command. I've tested on Redhat, Debian and SLES families.
vars:
- myvolume: /backup
tasks:
- command: mountpoint -q {{myvolume}}
register: volume_stat
failed_when: False
changed_when: False
- debug:
msg: "This is a mountpoint!"
when: volume_stat.rc == 0
The problem is, mountpoint
command generates stderr if the path isn't a mount point so you have to use ignore_errors
, witch is not a good solution.
EDIT 1: Is mentioned by @udondan, failed_when
is a better approach then ignore_errors
since it doesn't output errors.
It may be what you want if you need to stop the playbook if the path isn't a mount point.
I hope someone find a better solution than this.
NOTE: There is some platforms that doesn't have mountpoint
command, as far as I know Darwin (Mac OSX) and SunOS (Oracle Solaris), if you need this to work on those systems, you'll need to find another workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With