How to discern, if a directory is NFS-mounted from Ansible?

你说的曾经没有我的故事 提交于 2020-01-14 14:57:07

问题


I need to set up an application directory for a "hairy" app. Depending on the case, the directory may be local to each server participating, or shared among several servers via NFS.

So, I need to be able to detect, whether the given path is local or NFS-accessed and skip some of the tasks in the latter case.

What's the best way to detect this in an Ansible role?

I tried using the stat module, but device_type seems to be set to 0 in all cases, NFS or local (XFS).

On Linux I could invoke stat -f /path -- this would output details, including the type (the utility uses the statfs syscall). But this is a Linux-only method, and I'd rather avoid such petty OS-dependencies (same goes for the mountpoint utility).

I would write a custom library function, but there is no os.statfs in Python...

What's left?


回答1:


Another approach would make use of Ansible facts. You could filter the ansible_mounts array for your mount=your mount point and extract the filesystem type field. For an example, please see the answer I got here: https://stackoverflow.com/a/49662135/1268949 .

Another example from my production code:

- name: Determine shared-dir mount point
command: "/usr/bin/env stat -c '%m' {{ shared_dir_real_path }}"
register: shared_dir_mount_point
changed_when: False

- name: Determine the mount point's filesystem type and mount options
set_fact:
    "shared_dir_mount_{{ item }}": "{{ ansible_mounts | selectattr('mount', 'equalto', shared_dir_mount_point.stdout) | map(attribute = item) | join(',') }}"
with_items:
    - fstype
    - options



回答2:


If the GNU stat utility is available on your target platforms, then you can invoke it in a way that doesn't make use of the statfs call to detect the mount-point and then search it in the output of mount, e.g. on Linux:

$ mount | grep -F `stat -c %m /boot/grub` | cut -d' ' -f5
ext2

I verified that this invocation of stat only makes use of standard system calls (see CONFORMING TO in the man page of stat(2)):

$ strace stat -c %m /boot/grub/ |& fgrep stat
execve("/usr/bin/stat", ["stat", "-c", "%m", "/boot/grub/"], [/* 65 vars */]) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=218501, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=130224, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=456632, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=14608, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=138696, ...}) = 0
statfs("/sys/fs/selinux", 0x7ffe62882ff0) = -1 ENOENT (No such file or directory)
statfs("/selinux", 0x7ffe62882ff0)      = -1 ENOENT (No such file or directory)
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=5152256, ...}) = 0
lstat("/boot/grub/", {st_mode=S_IFDIR|0755, st_size=1024, ...}) = 0
lstat("/boot", {st_mode=S_IFDIR|0755, st_size=3072, ...}) = 0
lstat("/boot/grub", {st_mode=S_IFDIR|0755, st_size=1024, ...}) = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
stat("/boot/grub", {st_mode=S_IFDIR|0755, st_size=1024, ...}) = 0
stat("..", {st_mode=S_IFDIR|0755, st_size=3072, ...}) = 0
stat("..", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/boot", {st_mode=S_IFDIR|0755, st_size=3072, ...}) = 0
fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0


来源:https://stackoverflow.com/questions/49581325/how-to-discern-if-a-directory-is-nfs-mounted-from-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!