noze_potato
noze_potato

Reputation: 187

Ansible check if directory acessible then do something

i want to include this logic in my role

- 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

how to do something like this using the ansible? so my scenario is , to try write something or cd to NFS share and make sure that NFS filesystem is accessible and is ok

Upvotes: 3

Views: 1542

Answers (1)

rpm192
rpm192

Reputation: 2464

You can use the stat module to see whether a directory exists and if it is writeable. stat.writable contains a boolean that is true when the directory exists and is writable.

- 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)

Upvotes: 6

Related Questions