如何辨别目录是否从 Ansible 挂载 NFS? [英] How to discern, if a directory is NFS-mounted from Ansible?

查看:45
本文介绍了如何辨别目录是否从 Ansible 挂载 NFS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个毛茸茸的"应用程序设置一个应用程序目录.根据情况,目录可能是参与的每个服务器的本地目录,或者通过 NFS 在多个服务器之间共享.

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.

所以,我需要能够检测给定的路径是本地的还是 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.

在 Ansible 角色中检测此问题的最佳方法是什么?

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

我尝试使用 stat 模块,但是 device_type 似乎在所有情况下设置为 0,NFS 或本地 (XFS).

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

在 Linux 上,我可以调用 stat -f/path -- 这将输出详细信息,包括类型(实用程序使用 statfs 系统调用).但这是一种仅限 Linux 的方法,我宁愿避免这种微小的操作系统依赖性(mountpoint 实用程序也是如此).

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).

我会写一个自定义库函数,但是 Python 中没有 os.statfs...

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

还剩下什么?

推荐答案

另一种方法是利用 Ansible 事实.您可以过滤 ansible_mounts 数组作为您的 mount=您的挂载点 并提取文件系统类型字段.例如,请参阅我在这里得到的答案:https://stackoverflow.com/a/49662135/1268949 .

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

这篇关于如何辨别目录是否从 Ansible 挂载 NFS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆