fetching multiple files from a host based on a naming pattern with Ansible

Issue

This Content is from SuperUser. Question asked by vrms

I have a folder on a remote that looks like this.

> ls -la
-rw-rw-r--. 1 postgres postgres  469 15. Aug 12:37 220815.sql
-rwxr-xr-x. 1 postgres postgres   82 30. Dez 2015  client-postgres
-rw-------. 1 postgres postgres 2327 16. Sep 14:26 logfile
-rw-rw-r--. 1 postgres postgres    0 10. Aug 08:46 new_bindir
-rwxr-xr-x. 1 postgres postgres  497  2. Mär 2021  .pg.env
-rw-------. 1 postgres postgres  680 16. Sep 13:07 .pg-service10.env
-rw-------. 1 postgres postgres  680 16. Sep 13:08 .pg-service11.env
-rw-------. 1 postgres postgres  680 16. Sep 16:25 .pg-service15.env
-rw-------. 1 postgres postgres  708 16. Sep 11:27 .pg-service_name.env
-rw-------. 1 postgres postgres  692 16. Sep 12:36 .pg-servicename.env
-rw-------. 1 postgres postgres 1050 16. Sep 11:27 .pg-service_name.env.bak
-rw-r--r--. 1 postgres postgres  797 18. Okt 2021  postgresql_rpm.service
-rw-r--r--. 1 postgres postgres  855 28. Okt 2021  postgresql.service

now I need to fetch all files from that folder matching the .pg-*.service pattern (resulting into fetching 6 files as per this example). The playbook is runnning against a single host.

however it looks as the logig as i.e. a locale cat (cat pg*.env) is not interpreted right by ansible. and the below does not work because a file named /opt/db/postgres/bin/.pg*env does not exist.

- name: fetch all .env files to fetched
  ansible.builtin.fetch:
    src: /opt/db/postgres/bin/.pg*env
    dest: fetched/
    flat: true
  become: yes

I can not use a loop because i can not predict the explicit file names. I only know their names follow that pattern mentioned above.

What is the trick here?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from SuperUser , is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?