# Ansible playbook for updated file listing

**URL:** <https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072>\
**Category:** Get Help\
**Tags:** gcp, ansible\
**Created:** [November 22, 2024, 6:02pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072 "2024-11-22T18:02:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [November 22, 2024, 6:02pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/1 "2024-11-22T18:02:03Z")

</div>

I am trying to develop an ansible module which would traverse subfolders under a main folder and list all .SQL files inside the sub-folders which were modified in the last 30 minutes. Below is my code:

- name: Install SQL scripts on Private PostgreSQL Instance  
hosts: localhost  
gather\_facts: no

# become: yes

tasks:  
- name: install pip3  
apt: name=python3-pip state=present

```
- name: Install required apt packages
  apt:
    name: 
      - libpq-dev
      
- name: Install required python packages
  pip:
    name: 
      - psycopg2

- name: Download cloudSQL proxy
  ansible.builtin.get_url:
    url: https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.13.0/cloud-sql-proxy.linux.amd64
    dest: ./cloud-sql-proxy
    mode: "0755"

- name: Set GCP project
  command:
    gcloud config set project '{{ gcp_project }}'

- name: Print gcloud config
  command:
    gcloud config list
    
- name: Start cloudSQL proxy
  command: ./cloud-sql-proxy --private-ip "{{ gcp_project }}:{{ db_instance_location }}:{{ db_instance_name }}"
  async: 3600
  poll: 0

- name: List subfolders
  find: 
    path: DDL/
    recurse: no
    file_type: directory
  register: subfolders

- name: Find recently modified files in each subfolder
  find:
    path: "{{ item.path }}"
    recurse: yes
    patterns: "*.sql"
    age: "-30m"
    age_stamp: "mtime"
    use_regex: yes
  loop: "{{ subfolders.files }}"
  register: recent_files_per_subfolder
  loop_control:
    label: "{{ item.path }}"

- name: Display files modified within the last 5 minutes for each subfolder
  debug:
    msg: "Files modified in {{ item.item.path }}: {{ item.files | map(attribute='path') | list }}"
  loop: "{{ recent_files_per_subfolder.results }}"
  when: subfolders.files | length > 0

```

However the playbook output not listing anything. I have tried changing the age parameter, but the output is same.

Any help would be really appreciated.

---

<div class="post-metadata">

**Author:** ![tonk](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tonk/32/214_2.png) [@tonk](https://forum.ansible.com/u/tonk)\
**Post date:** [November 22, 2024, 6:08pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/2 "2024-11-22T18:08:30Z")

</div>

I’m not completely sure, but `patterns: "*.sql"` and `use_regex: yes` doesn’t match, as `*.sql` is a filename-expansion pattern and not a regular expression.  
For it to be a regexp it would need to be something like `^.*\.sql$`

HTH

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [November 25, 2024, 3:30pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/3 "2024-11-25T15:30:11Z")

</div>

This solution doesn’t seem to work.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 25, 2024, 5:02pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/4 "2024-11-25T17:02:21Z")

</div>

What does `subfolders` look like, try adding a `debug` for this and post the result here?

```yaml
- name: Debug subfolders
  ansible.builtin.debug:
    var: subfolders

```

Also try setting `use_regex` to `false` as `*.sql` is a shell pattern?

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [November 25, 2024, 5:40pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/5 "2024-11-25T17:40:18Z")

</div>

Hi Chris,  
Here is what the folder structure looks like:

DDL(main folder) → Multiple sub-folders → Each sub-folder contains multiple .sql files

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 25, 2024, 5:45pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/6 "2024-11-25T17:45:10Z")

</div>

Right, but does the `subfolders` dictionary contain the list of sub-directories? I’m asking as `DDL/` is a relative path and I’m wondering if a absolute path might be needed here? 🤷

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [November 25, 2024, 6:57pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/7 "2024-11-25T18:57:08Z")

</div>

yes it does:

```
- name: List subfolders
  find: 
    path: DDL/
    recurse: yes
    file_type: directory
  register: subfolders

- name: Find recently modified files in each subfolder
  find:
    path: "{{ item.path }}"
    recurse: yes
    patterns: "*.sql"
    age: "-10m"

```

# age\_stamp: “mtime”

# use\_regex: yes

```
  loop: "{{ subfolders.files }}"
  register: recent_files_per_subfolder
  loop_control:
    label: "{{ item.path }}"

- name: Display files modified within the last 5 minutes for each subfolder
  debug:
    msg: "Files modified in {{ item.item.path }}: {{ item.files | map(attribute='path') | list }}"
  loop: "{{ recent_files_per_subfolder.results }}"
  when: subfolders.files | length > 0

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 25, 2024, 7:01pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/8 "2024-11-25T19:01:56Z")

</div>

> [@Chandra\_Mukherjee](#):
>
> use\_regex: yes

Did you try `use_regex: false` as I [suggested above](http://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/4)?

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [December 2, 2024, 4:02pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/9 "2024-12-02T16:02:23Z")

</div>

i did, but unfortunately, no luck. ☹

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 2, 2024, 4:14pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/10 "2024-12-02T16:14:46Z")

</div>

If you want to use Ansible over SSH using public keys then you first need to get SSH working with public keys, perhaps there are logs on your switch which might indicate why it isn’t working?

Or run `ssh -vvvv` to get verbose output in case this explains why it is not working?

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [December 2, 2024, 4:35pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/11 "2024-12-02T16:35:16Z")

</div>

Ansible is running on a github runner where the code is checked out. It is not doing ssh to anywhere.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 2, 2024, 5:34pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/12 "2024-12-02T17:34:49Z")

</div>

Aargh, I was replying to the wrong thread, sorry! 🙄

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 2, 2024, 6:53pm UTC](https://forum.ansible.com/t/ansible-playbook-for-updated-file-listing/11072/13 "2024-12-02T18:53:06Z")

</div>

> [@Chandra\_Mukherjee](#):
>
> i did, but unfortunately, no luck.

But you didn’t show us what happened! Saying you tried it but it didn’t work gives us nothing to help you with. When Chris asked about the folder structure and gave the `ansible.builtin.debug` task to answer the question, you instead told us what you think the folder structure is, which is only somewhat interesting. But we aren’t trying to debug you; we’re trying to debug your tasks. What matters is what they see, not what you think is there.

I’ve seen nothing so far that convinces me your first “`find`” task is finding anything. What’s the output from this:

```yaml
- name: List subfolders
  ansible.builtin.find:
    path: DDL # <- highly questionable relative path
    recurse: no
    file_type: directory
  register: subfolders

- name: Debug subfolders
  ansible.builtin.debug:
    var: subfolders

```
