# How to find a file with stat and wildcard in the path?

**URL:** <https://forum.ansible.com/t/how-to-find-a-file-with-stat-and-wildcard-in-the-path/41620>\
**Category:** Get Help\
**Created:** [April 9, 2025, 8:48am UTC](https://forum.ansible.com/t/how-to-find-a-file-with-stat-and-wildcard-in-the-path/41620 "2025-04-09T08:48:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [April 9, 2025, 8:48am UTC](https://forum.ansible.com/t/how-to-find-a-file-with-stat-and-wildcard-in-the-path/41620/1 "2025-04-09T08:48:23Z")

</div>

I want to check whether a file exists.

It works, when I have the exact file name `30-eakte-keepalive.conf` as below.

```auto
- name: check file existence
  hosts: "{{ targets | default('postgres') }}"
  vars:
    object: /opt/db/data/postgres/data/conf.d/30-eakte-keepalive.conf # 00-ina-default.conf # 10-ssl.conf
  tasks:
    - name: check existence of "{{ object }}"
      ansible.builtin.stat:
        path: "{{ object }}" # /opt/db/postgres/nrpe
      become: true
      become_user: postgres
      register: return_object

```

however I am not certain whether the name is identical exactly so I’d prefer to apply a wildcard in my search string like `/opt/db/data/postgres/conf.d/*keepalive*.conf`

how can I achieve this in a play as the above?

I tested `/opt/db/data/postgres/conf.d/*keepalive*.conf` which returns a 'does not exist`, so the \* does not seem to work as a wildcard here.

or is `stat` not even the best module to do this?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 9, 2025, 10:20am UTC](https://forum.ansible.com/t/how-to-find-a-file-with-stat-and-wildcard-in-the-path/41620/2 "2025-04-09T10:20:59Z")

</div>

Why don’t you use the [find module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html) to file the file(s) and then stat them (in a loop if there might be more than one)?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [April 9, 2025, 11:21am UTC](https://forum.ansible.com/t/how-to-find-a-file-with-stat-and-wildcard-in-the-path/41620/3 "2025-04-09T11:21:51Z")

</div>

coz I didn’t think of it. Thanx for the pointer.
