You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible find模块意外匹配隐藏目录文件,求助解决方法

解决Ansible Find任务匹配隐藏目录内文件的问题

问题原因

Ansible find 模块默认的 hidden: no 参数仅排除隐藏文件(文件名以.开头的文件),但不会阻止递归遍历隐藏目录。当开启recurse: yes时,模块会进入所有子目录(包括如.ansible这类隐藏目录),并在其中查找符合文件名模式的非隐藏文件,这就导致你意外匹配到了隐藏目录内的文件。

解决方案

方案1:在Find任务中直接排除隐藏目录

修改find任务,通过正则匹配排除所有包含隐藏目录的路径:

- name: Find all script files
  find:
    recurse: yes
    paths: /var/bbb
    patterns:
      - "*.sh"
      - "*.py"
      - "*.env"
    exclude_paths: '.*\/\.[^\/]+'
    use_regex: yes
  register: script_files
  • exclude_paths: '.*\/\.[^\/]+':正则表达式匹配任何包含隐藏目录的路径(例如/.ansible/)
  • use_regex: yes:告知模块将exclude_paths作为正则表达式解析

方案2:在后续任务中过滤路径

如果不需要修改find任务,可以在修改权限的任务中添加条件判断,跳过来自隐藏目录的文件:

- name: Make all scripts executable
  file:
    dest: "{{ item.path }}"
    mode: "a+rx"
  with_items: "{{ script_files.files }}"
  when: item.path is not regex('\/\.[^\/]+\/')

该条件会检查文件路径中是否存在/.xxx/格式的片段(即隐藏目录),若存在则跳过当前文件。

内容的提问来源于stack exchange,提问作者Isaac Hartung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:15:27