以下是一个使用Ansible剧本根据日期条件删除多个/单个文件的示例:
---
- name: Delete files based on date condition
hosts: all
gather_facts: false
vars:
# 指定要删除的文件路径
file_path: /path/to/files/
tasks:
- name: Get current date
set_fact:
current_date: "{{ ansible_date_time.date }}"
- name: Find files older than a specific date
find:
paths: "{{ file_path }}"
age: "7d" # 指定文件的最后修改日期在7天前
file_type: file
register: files_to_delete
- name: Delete files older than a specific date
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
在上面的示例中,我们使用Ansible的find
模块找到指定路径下在7天前修改的文件。然后,我们使用file
模块删除这些文件。
请注意,在此示例中,我们使用了ansible_date_time.date
来获取当前日期,并使用age
参数将文件的最后修改日期与当前日期进行比较。您可以根据需要调整这些值。
此外,您还可以更改file_path
变量以指定要删除文件的路径。
希望这个示例能帮助您解决问题!
上一篇:Ansible剧本返回结果
下一篇:Ansible剧本和角色