Find out specific files in sub directories using Ansible Playbook

ansibleansible-playbook

I have directory structure as below, wants to fetch the specific sub directory files using ansible rather than all.

/mnt/server1 ->
  ----> yyy.deb

  ----> /mnt/server1/All/tttsss.deb

  ----> /mnt/server1/HS-CLONE/gggg.deb

  ----> /mnt/server1/HS-TEST/kkkk.deb

I need to find only .deb files present under /mnt/server1/All/tttsss.deb and /mnt/server1/HS-CLONE/gggg.deb directories. I don't require all other files.

When i trying using below logic, the parent directory file yyy.deb is also coming as output.

- name: Ansible find files in subdirectory examples
  find:
         paths:  /mnt/server1
         file_type: file
         recurse: yes
         use_regex: yes
         patterns:
           - 'All'
           - "HS-CLONE"
           - '.*deb$'

  register: files_matched_subdirectory

With the above logic output as:

Output: (Not to use the exclude option in Playbook)

yyy.deb                              -> This file name may vary.
/mnt/server1/All/tttsss.deb
/mnt/server1/HS-CLONE/gggg.deb

Expected output should be: ( excepting to have generic pattern to solve my probelm)

/mnt/server1/All/tttsss.deb
/mnt/server1/HS-CLONE/gggg.deb

Best Answer

You should specify the paths you want explicitly. If you don't want to search the path /mnt/server1, but only specific subdirectories, list them.

- name: Ansible find files in subdirectory examples
  find:
         paths: 
           - /mnt/server1/All
           - /mnt/server1/HS-CLONE
         file_type: file
         recurse: yes
         use_regex: yes
         patterns:
           - '.*deb$'
Related Topic