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

I want to check whether a file exists.

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

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

Why don’t you use the find module to file the file(s) and then stat them (in a loop if there might be more than one)?

1 Like

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

2 Likes