Question:
I have the following directory and it has the following files:
1 2 3 4 |
/tmp/test/file1.txt /tmp/test/file1.txt.backup /tmp/test/mywords.csv |
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:
1 2 3 4 5 6 |
- file: path: "{{item}}" state: absent with_fileglob: - /tmp/test/file* |
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.
1 2 3 4 5 6 7 8 9 10 |
- find: paths: "/tmp/test" patterns: "file*" register: result - file: path: "{{item.path}}" #correction code state: absent with_items: result.files |