# how to parse variable in patterns by find module

**URL:** <https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759>\
**Category:** Ansible Project\
**Created:** [March 13, 2023, 4:39pm UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759 "2023-03-13T16:39:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 13, 2023, 4:39pm UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759/1 "2023-03-13T16:39:36Z")

</div>

Hi everyone,

I’ve tried to using a variable in find module by pattern by it doesn’t seems to work. Here’s below a example. I also looked at another solution with set\_facts but I doesn’t run too.  
I want to list all the backup files in a directory. The files have a timestamp and it’s filename will be defined by variable.  
Does anyone have an idea how should I proceed? or in which way?

Thanks in advance for your help

Here’s the variables:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 13, 2023, 5:11pm UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759/2 "2023-03-13T17:11:33Z")

</div>

> Hi everyone,
> 
> I've tried to using a variable in find module by pattern by it doesn't seems to work. Here's below a example. I also looked at another solution with set\_facts but I doesn't run too.  
> I want to list all the backup files in a directory. The files have a timestamp and it's filename will be defined by variable.  
> Does anyone have an idea how should I proceed? or in which way?
> 
> Thanks in advance for your help
> 
> Here's the variables:  
> ---  
> file:  
> - filename: backup  
> state: present  
> [...]
> 
> Here's the task:  
> ---  
> [...]  
> find:  
> paths: /tmp  
> recurse: false  
> file\_type\_ file  
> patterns: '^{{ item.filename }}\.[0-9]{6}.\*$'  
> use\_regex: yes  
> loop: {{ file }}

Hello, you need to quote the argument for the loop:

&nbsp;&nbsp;&nbsp;loop: '{{ file }}'

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 13, 2023, 5:11pm UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759/3 "2023-03-13T17:11:51Z")

</div>

You are not registering the product of the task.

> find:  
> paths: /tmp  
> recurse: false  
> file\_type\_ file  
> patterns: ‘^{{ item.filename }}.[0-9]{6}.\*$’  
> use\_regex: yes  
> loop: {{ file }}

> register: **found**

You then need to look at **found.files** (a list) to see what is found. This is documented in the ansible find module.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 13, 2023, 5:22pm UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759/4 "2023-03-13T17:22:50Z")

</div>

- name: look for backup files

find:

paths: /tmp

recurse: false

file\_type: ‘file’

patterns: ‘^{{ item.filename }}.[0-9]{6}.\*$’

use\_regex: true

loop: [‘backup’]

**register: found**

- name: get list of files

set\_fact:

files: “{{ **found.results** | json\_query(‘**[_].files[_][]**’) }}”

- name: show list of files

debug: var= **item.path**

loop: “{{ **files** }}”

Walter

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 14, 2023, 7:00am UTC](https://forum.ansible.com/t/how-to-parse-variable-in-patterns-by-find-module/37759/5 "2023-03-14T07:00:51Z")

</div>

Thank you for your answer and the tip to get list of files (set\_fact), now it’s working.  
Best regards, J.
