how to parse variable in patterns by find module

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:

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:

   loop: '{{ file }}'

Regards
           Racke

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

  • 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

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