2

I just want to know about how to check the file exists or not any directory via ansible code. I have tried with stat module with path "*.test" already but it did not working.

Below is my trying:

- name: Check license file exist or not
  stat:
    path: /var/www/vb_backup/vb/*.test
  register: license

becuase path in stat module needs specific file or directory. I want everyone to help me on this.

Thank you in advance.

Ansible version: ansible 2.8.7

Houy Narun
  • 1,557
  • 5
  • 37
  • 86
Kimly Vith
  • 53
  • 1
  • 6

4 Answers4

3

Use module find. For example

  - find:
      paths: /var/www/vb_backup/vb
      patterns: "*.test"
    register: result

  - debug:
      msg: "No files found."
    when: result.matched == 0

  - debug:
      msg: "File found: {{ item.path }}"
    loop: "{{ result.files }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

You can use find module and assert module.

- name: Find files
  find:
    paths: /var/www/vb_backup/vb
    patterns: *.test'
  register: output
- name: message
  assert:
    that:
    - output.matched
    fail_msg: "File not found"
    success_msg: "File found"

Smily
  • 2,308
  • 2
  • 17
  • 41
0

This should do the work.

path: "{{/var/www/vb_backup/vb/*.test}}"
moonmoon
  • 110
  • 10
0

you can use find module like this

find:
  paths: /var/www/vb_backup/vb/
  patterns: *.test

then you can invoke matched files