- name: Ansible delete file example
file:
path: /etc/delete.conf
state: absent
注意:当你知道一个文件名的时候,可以这样删除这个文件。
- name: Ansible delete multiple file example
file:
path: "{{ item }}"
state: absent
with_items:
- hello1.txt
- hello2.txt
- hello3.txt
注意:当你知道多个文件名的时候,可以这样删除这些文件。
- name: Ansible delete directory example
file:
path: removed_files
state: absent
上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
- name: Ansible delete file wildcard example
shell: rm -rf hello*.txt
上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
- hosts: all
tasks:
- name: Ansible delete file glob
find:
paths: /etc/Ansible
patterns: *.txt
register: files_to_delete
- name: Ansible remove file glob
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
- 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 }}"
- hosts: all
tasks:
- name: Ansible delete files older than 5 days example
find:
paths: /Users/dnpmacpro/Documents/Ansible
age: 5d
register: files_to_delete
- name: Ansible remove files older than a date example
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"