ansible - Ansible删除带有通配符/正则表达式/ glob的文件,但有异常
问题描述
我想根据通配符删除文件,但还要向规则添加例外。
- hosts: all
tasks:
- name: Ansible delete file wildcard
find:
paths: /etc/wild_card/example
patterns: "*.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 }}"
例如,我想排除一个名为“important.txt”的文件。我怎样才能做到这一点?
解决方案
只需在when
删除文件的任务中添加条件即可。例如,类似:
- name: Ansible remove file wildcard
file:
path: "{{ item.path }}"
state: absent
when: item.path != '/etc/wild_card/example/important.txt'
with_items: "{{ wildcard_files_to_delete.files }}"
这将跳过特定文件。如果您有要跳过的文件列表,您可以这样做:
- name: Ansible remove file wildcard
file:
path: "{{ item.path }}"
state: absent
when: item.path not in files_to_skip
with_items: "{{ wildcard_files_to_delete.files }}"
vars:
files_to_skip:
- /etc/wild_card/example/important.txt
- /etc/wild_card/example/saveme.txt
如果您想根据某种模式保留文件,您可以使用 ansiblematch
或search
测试:
- name: Ansible remove file wildcard
file:
path: "{{ item.path }}"
state: absent
when: item.path is not search('important.txt')
with_items: "{{ wildcard_files_to_delete.files }}"
推荐阅读
- ajax - jQuery [attribute!=value] 选择器
- java - ThreadMXBean.getThreadCpuTime 返回 0
- c++ - 为表示一个或多个操作的 C++ 函数查找 int 参数的类型安全替换
- git - 在drupal 8中安装模块时作曲家抛出git错误
- typescript - 将类型分配给可能不需要解析的 JSON 类型
- java - 活动中的 Android 工作室名称选项卡
- sql - 我可以合并或加入这些表吗?
- c# - 如何在 ASP.NET 中使用视图模型?
- php - 即使 WP_DEBUG=true,Wordpress 也不会写入 debug.log
- quill - Quill:如何获取包含样式的 html 数据?