remove all files containing a certain name within a directory

Question:

I have the following directory and it has the following files:

How do I use the file module to just remove file1* files?

Answer:

edit: Ansible 2.0 has been released now, so the previous answer should work, and you can also now loop over fileglobs. Note this only works if you are running Ansible locally:

original answer:

I can’t find a way to do this currently without dropping to the shell, however if you can drop to the shell to gather a list of files that match the pattern and save that as a variable, then you can loop over the file module with with_items

Ansible 2.0 will include a “find” module that would get the list for you: http://docs.ansible.com/ansible/find_module.html

Here’s a couple of tasks that should do it in ansible 2.0, but I don’t have it installed so I haven’t tested it and may be accessing the results of the find module incorrectly.

Leave a Reply