ansible - Ansible:删除文件和文件夹,同时排除一些
问题描述
在我的 Ansible Playbook 中,我想要一个从应用程序目录中删除旧文件和文件夹的任务。这个原本简单的任务的转折是需要保留一些文件或文件夹。想象一下这样的事情:
/opt/application
- /config
- *.properties
- special.yml
- /logs
- /bin
- /var
- /data
- /templates
假设我想/logs
完全保留,/var/data
并且/config
我想保留special.yml
。
(我目前无法提供确切的代码,因为我对此感到沮丧,在冷静下来后,我现在正在家里写这个问题)
我的想法是有两个排除列表,一个包含文件夹,一个包含文件。然后我使用该find
模块首先将应用程序目录中的文件夹放入一个变量中,并将其余文件相同的放入另一个变量中。file
之后,我想使用该模块删除不在排除列表中的每个文件夹和文件。
(伪 YML,因为我在 Ansible 中还不够流利,所以我可以编写一个结构合理的示例;不过它应该足够接近)
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ found_files_list.files }}"
when: well, that is the big question
我不知道如何正确构造该when
子句。甚至有可能这样吗?
解决方案
我不相信文件模块有 when 子句。但是您可能可以通过以下方式实现所需的功能:
- name: Find /opt/application all directories, exclude logs, data, and config
find:
paths: /opt/application
excludes: 'logs,data,config'
register: files_to_delete
- name: Ansible remove file glob
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
我希望这是你需要的。
推荐阅读
- android - 使用 https 使用 Android Studio +3.3 检查网络流量
- swift - 无法使用“(String)”类型的参数列表调用“isEqualToString”(来自闭包)
- vb.net - 为什么推断不适用于静态但适用于暗淡?
- java - 生成不重复的随机数数组的函数
- reactjs - 将数组映射到接口对象
- python-3.x - 尝试在 csv 文件中实现 Header 时出错
- python - 抓取完成后运行自定义代码(scrapy)
- angular - 如何访问材料垫选择列表中的选择选项
- html - 背景图片不显示 html+css
- c++ - 传递资源包装器对象