1

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?

1

1 Answer 1

1

This is how I made this work (with a lot of help from the Ansible mailing list (including @Vladimir Botka)) in 3 steps

  1. finding all relevant files (find module)
  2. creating a list of these findings (set_fact)
  3. fetching he files based on that list (fetch module)
- name: finding all .pg-*.env files on the server
  ansible.builtin.find:
    paths: "/opt/db/postgres/bin"
    hidden: true
    recurse: true
    file_type: any
    patterns: '.pg*env'
  register: found_files
  become: true

- name: creating a list with the filenames
  set_fact:
    env_files: "{{ found_files.files | map(attribute='path') }}"

- name: fetch all .env files to fetched, based on the list created above
  ansible.builtin.fetch:
    src: "/opt/db/postgres/bin/{{ item }}"
    dest: fetched/
    flat: true
  become: yes
  loop: "{{ found_files.files | map(attribute='path') | map('basename') | list }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.