微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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://stackoverflow.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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐