Neil
Neil

Reputation: 3056

How to use Ansible with_fileglob template to exclude some files?

I have one block of Ansible, using with_fileglob.

How can i exclude files with name containing useless?

- set_fact: files:{{item}}
  with_fileglob:
    - "path/to/*.tar.gz | exclude "useless"

In another word, I don't want to get the file name with such string "useless".

How could I realize it?

Upvotes: 1

Views: 6431

Answers (2)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

Here's a nice oneliner for you:

- set_fact:
    files: "{{ lookup('fileglob','path/to/*.tar.gz',wantlist=true) | reject('search','useless') | list }}"

Upvotes: 6

gile
gile

Reputation: 5996

As far as I know glob allows include patterns; it allows excluding some characters only, but not for substrings. You could try excluding files containing "useless" with two steps as below, although it gets a register and not a fact

   - name: retrieve list of files
     command: ls {{ item }}
     register: contents
     with_fileglob:
       - path/to/*.tar.gz

   - name: list excluding useless
     debug: msg="Useful file {{item.item}}"
     with_items: "{{ contents.results }}"
     when: "{{ item.stdout.find('useless')}} == -1"

Upvotes: 0

Related Questions