Check if the directory is a mount point?

I need to check if the directory is a mount point before doing some other tasks.

I was looking through the documentation, and it seems that you can create / destroy mount points, but not just check if it exists. Click on the link below.

http://docs.ansible.com/ansible/mount_module.html

I am wondering if there is a way to check if this exists with the impossible, or it should be some other language called from the impossible.

+5
source share
4 answers

I tried using both mount and stat modules. Both did not meet your requirements.

I only managed to work with the OS command. I tested the 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 that the mountpoint command generates stderr if the path is not a mount point, so you should use ignore_errors , because this is not a good solution.

EDIT 1 : @udondan is mentioned, failed_when is the best approach, then ignore_errors , since it does not output errors.

This may be what you want if you need to stop the game if the path is not a mount point.

I hope someone finds a better solution than this.

NOTE There are some platforms that do not have the mountpoint command, as far as I know Darwin (Mac OSX) and SunOS (Oracle Solaris), if you need these systems to work, you need to find another workaround.

+11
source

After a while I came up with this.

 vars: - myvolume: /backup tasks: - debug: msg="The dir is a mount point" with_items: ansible_mounts when: item.mount == myvolume 

I'm not sure how this applies to all systems and / or if ansible_mounts contains all the mount points of the OS or only those that are created with the impossible.

+2
source

You can use Ansible "command" module "stdout" to determine the mount status for this directory, here is an example code:

 - name: "check mount point {{ mount_dir }}" command: mountpoint {{ mount_dir }} register: mount_stat failed_when: False changed_when: False - name: "debug" when: mount_stat.stdout == "{{ mount_dir }} is a mountpoint" debug: msg: "{{ mount_dir }} is a mountpoint" - name: "debug" when: mount_stat.stdout == "{{ mount_dir }} is not a mountpoint" debug: msg: "{{ mount_dir }} is not a mountpoint" 
+1
source

Having similar difficulties, but try something like this -

 - name: testing for required mount points fail: msg: "{{ item }} must be a mount point" when: not item|is_mount with_items: - /path/to/test 

Now, if I can just figure out how to reliably get the mount point size, lol

By the way, it’s worth looking at the facts by default, for example {{ ansible_mounts }} , although my / dev / shm does not show yet - I don’t know why. Caveat scriptor, ymmv.

+1
source

Source: https://habr.com/ru/post/1240342/


All Articles