Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete *.web files only if they exist

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
like image 430
miki Avatar asked Jan 22 '16 14:01

miki


1 Answers

@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'] }}"
like image 125
Josh Smift Avatar answered Oct 01 '22 06:10

Josh Smift