---
- name: Find all subfolders
  ansible.builtin.find:
    file_type: directory
    paths: "{{ dir }}"
    recurse: true
  register: folders

- name: Check for upload.sh in each subfolder
  ansible.builtin.find:
    paths: "{{ item.path }}"
    patterns: "upload.sh"
    recurse: false
  register: upload_scripts
  with_items: "{{ folders.files }}"

- name: Execute upload.sh if present
  ansible.builtin.command: "bash {{ item.0.path }}/upload.sh"
  changed_when: item.1.matched > 0
  delay: 5
  retries: 3
  when: item.1.matched > 0
  with_together:
    - "{{ folders.files }}"
    - "{{ upload_scripts.results }}"
