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

在CI流水线中使用Ansible-lint检测多子目录角色的问题

解决Ansible角色批量检测的路径显示问题

项目结构

/projects
/plugins
/roles/sampleRole1/...
/roles/sampleRole2/..
galaxy.yml

遇到的问题

  1. 直接检测单个角色报错
    执行ansible-lint ./roles/sampleRole/时触发以下错误:
Traceback (most recent call last):
  File "/opt/homebrew/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/__main__.py", line 252, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/__main__.py", line 165, in main
    app = get_app(offline=options.offline)
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/app.py", line 252, in get_app
    _perform_mockings()
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/_mockings.py", line 88, in _perform_mockings
    while os.readlink(link_path) != target:
OSError: [Errno 22] Invalid argument: '/Users/mjones2/.cache/ansible-compat/121e36/collections/ansible_collections/something/something
  1. 批量检测脚本无完整路径输出
    编写循环脚本逐个检测角色:
for dir in ./roles/*/; do (cd "$dir" && ansible-lint -v); done 

但输出仅显示相对路径(如tasks/main.yml:19: yaml: line too long (173 > 160 characters) (yaml[line-length])),CI流水线中无法快速定位问题文件。

解决方法

方法1:修改脚本生成完整路径输出

无需进入角色目录,直接在当前路径执行检测,并通过sed为输出添加上角色的完整路径前缀:

for dir in ./roles/*/; do
  ansible-lint -v "$dir" | sed "s|^|$dir|"
done

执行后输出会变成类似./roles/sampleRole1/tasks/main.yml:19: yaml: line too long...,清晰展示问题文件的完整位置。

方法2:修复缓存问题,直接批量检测

报错原因大概率是ansible-compat缓存目录的符号链接损坏,清理缓存后可直接批量检测所有角色:

rm -rf ~/.cache/ansible-compat/
ansible-lint ./roles/

此命令会自动遍历./roles/下的所有角色,输出天然包含完整文件路径,无需额外脚本处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 05:10:53