Ansible使用通配符/regex/glob删除文件,但有例外
我想删除基于通配符的文件,但也要向规则中添加例外Ansible使用通配符/regex/glob删除文件,但有例外,ansible,Ansible,我想删除基于通配符的文件,但也要向规则中添加例外 - 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 remo
- 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”的文件。我该怎么做呢?只要在删除文件的任务中添加一个
条件即可。例如,类似于:
- 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
如果您想基于某种模式保留文件,可以使用ansible的匹配
或搜索
测试:
- 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 }}"