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

查看:84
本文介绍了如何从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?

我尝试使用统计模块,但是所有情况下,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 syscall).但这是仅Linux的方法,我宁愿避免这种琐碎的OS依赖性( 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事实.您可以为mount = 您的挂载点过滤 ansible_mounts 数组,然后提取文件系统类型字段.例如,请参阅我在这里得到的答案: 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天全站免登陆