Last active
September 27, 2024 10:20
-
-
Save lemajes/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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 }}" |
The
use:regex:
on line 7 should beuse_regex:
.
Thank you ^^ !
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
The
use:regex:
on line 7 should beuse_regex:
.