how to parse variable in patterns by find module

64 views
Skip to first unread message

Hiero-nymo

unread,
Mar 13, 2023, 12:39:37 PM3/13/23
to Ansible Project
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 }}

Stefan Hornburg (Racke)

unread,
Mar 13, 2023, 1:11:57 PM3/13/23
to ansible...@googlegroups.com
Hello, you need to quote the argument for the loop:

loop: '{{ file }}'

Regards
Racke

> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3419395b-36b0-4195-9278-de959fa763can%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/3419395b-36b0-4195-9278-de959fa763can%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper


Rowe, Walter P. (Fed)

unread,
Mar 13, 2023, 1:12:14 PM3/13/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3419395b-36b0-4195-9278-de959fa763can%40googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Mar 13, 2023, 1:23:13 PM3/13/23
to ansible...@googlegroups.com

    - 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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Hiero-nymo

unread,
Mar 14, 2023, 3:00:51 AM3/14/23
to Ansible Project
Thank you for your answer and the tip to get list of files (set_fact), now it's working.
Best regards, J.

Reply all
Reply to author
Forward
0 new messages