Ansible 检查目录是否可访问然后做一些事情

Ansible check if directory acessible then do something

我想在我的角色中包含这个逻辑

- name: i want to check that NFS share is accessible and R/W
  module_to_check_nfs_share: touch {{ nfs_share_path }} ( for example , or cd to it)

- name: add NFS share path to elasticsearch config
  template: ...
  when: nfs_module == success

如何使用 ansible 做这样的事情?所以我的方案是,尝试写入一些东西或 cd 到 NFS 共享并确保 NFS 文件系统可访问并且正常

您可以使用stat 模块来查看目录是否存在以及是否可写。 stat.writable 包含一个布尔值,当目录存在时 true 并且 是可写的。

- name: Get directory status
  stat:
    path: /path/to/your/dir
  register: dirstatus

- name: Conditional task that only runs when dir exists and is writable
  template: ...
  when: dirstatus.stat.writeable | default(false)