Ansible 检查目录是否可访问然后执行某些操作

noz*_*ato 3 automation ansible devops

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

- 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
Run Code Online (Sandbox Code Playgroud)

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

rpm*_*192 5

您可以使用该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)
Run Code Online (Sandbox Code Playgroud)