I need to create an Ansible playbook to delete the *.web
files in a specific directory only if the files exists.
OS : cent OS, Redhat 5x, 6x.
I have tried the following with no success:
- stat: path=/opt/app/jboss/configuration/*.web
register: web
- shell: rm -rf /opt/app/jboss/configuration/*.web
when: web.stat.exists
@bruce-p's answer gives a deprecation warning with Ansible 2.0+, but the new with_fileglob gives another option:
- file: path={{ item }} state=absent
with_fileglob: /opt/app/jboss/configuration/*.web
(Similar question remove all files containing a certain name within a directory has a similar answer.)
EDIT: As noted below, that won't work; here's an example of "the fancy way":
- find:
paths: /opt/app/jboss/configuration
patterns: "*.web"
register: find_results
- file:
path: "{{ item['path'] }}"
state: absent
with_items: "{{ find_results['files'] }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With