Check if directory is a mount point?

nazerb picture nazerb · Jan 11, 2016 · Viewed 16.1k times · Source

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.

Answer

Bernardo Vale picture Bernardo Vale · Jan 11, 2016

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.