python-如何从Ansible识别目录是否为NFS挂载?

我需要为“多毛”应用程序设置一个应用程序目录.根据情况的不同,该目录可能位于每个参与服务器的本地目录中,或者通过NFS在多个服务器之间共享.

因此,我需要能够检测给定的路径是本地路径还是NFS访问路径,并在后一种情况下跳过某些任务.

在Ansible角色中检测到此错误的最佳方法是什么?

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

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

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

还剩什么?

解决方法:

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

我的生产代码中的另一个示例:

- 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
上一篇:我如何异步计划文件系统统计操作?


下一篇:Linux,C:access()无法捕获权限问题或其他问题