在Ansible中,可以使用with_fileglob
循环遍历文件通配符列表。下面是一个示例代码:
- name: 循环遍历文件通配符列表
hosts: all
tasks:
- name: 查找匹配的文件
find:
paths: /path/to/files
patterns: "{{ item }}"
register: file_matches
with_fileglob:
- "*.txt"
- "*.csv"
- name: 打印匹配的文件路径
debug:
var: item.path
with_items: "{{ file_matches.files }}"
在上面的示例中,我们使用find
模块来查找匹配的文件,paths
参数指定要搜索的路径,patterns
参数使用item
变量来表示循环遍历的文件通配符。register
参数用于将结果存储在变量file_matches
中。
然后,我们使用debug
模块来打印匹配的文件路径,item.path
表示循环遍历的每个文件的路径。with_items
参数用于循环遍历file_matches.files
变量中的每个文件。
你可以根据自己的实际需求修改路径、通配符列表和执行的任务。