Skip to content

Instantly share code, notes, and snippets.

@vpnwall-services
Last active January 22, 2022 20:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpnwall-services/5e5cc8c11384d6e42e1760b73471c620 to your computer and use it in GitHub Desktop.
Save vpnwall-services/5e5cc8c11384d6e42e1760b73471c620 to your computer and use it in GitHub Desktop.
[Ansible Delete Folders And Files Wildcard] Delete folders and files wildcard method with Ansible #script #yml #ansible #delete #wildcard
- hosts: all
tasks:
- name: Ansible delete file wildcard
find:
paths: /etc/wild_card/example
patterns: "^he.*.txt"
use_regex: true
register: wildcard_files_to_delete
- name: Ansible remove file wildcard
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ wildcard_files_to_delete.files }}"
@dglinder
Copy link

The use:regex: on line 7 should be use_regex:.

@vpnwall-services
Copy link
Author

The use:regex: on line 7 should be use_regex:.

Thank you ^^ !

@sysndevops
Copy link

sysndevops commented Dec 21, 2019

Use the below Ansible task to delete the wildcard folders:

 - hosts: prodweb
  tasks:
    - name: Analyzing the directories to delete...
      find:
        paths: /opt/
        patterns: "2019-.*"
        use_regex: true
        file_type: directory
      register: folders_to_delete

    - name: Displaying the result...
      debug:
        var: folders_to_delete

    - name: Deleting the directories...
      file:
        path: "{{ item.path }}"
        state: absent
      with_items: "{{ folders_to_delete.files }}"
      become: yes

The above example will find all the directories starting with '2019-' name under the '/opt/' location and delete them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment