CentOS虚拟机运行ansible-lint报AttributeError错误求助
Ansible-Lint 运行报错:AttributeError: 'str' object has no attribute 'resolve'
报错信息
Run ansible-lint --parseable ./ansible Traceback (most recent call last): File "/usr/local/bin/ansible-lint", line 8, in <module> sys.exit(_run_cli_entrypoint()) File "/usr/local/lib/python3.9/site-packages/ansiblelint/__main__.py", line 344, in _run_cli_entrypoint sys.exit(main(sys.argv)) File "/usr/local/lib/python3.9/site-packages/ansiblelint/__main__.py", line 197, in main initialize_options(argv[1:]) File "/usr/local/lib/python3.9/site-packages/ansiblelint/__main__.py", line 110, in initialize_options options.cache_dir = get_cache_dir(options.project_dir) File "/usr/local/lib/python3.9/site-packages/ansible_compat/prerun.py", line 13, in get_cache_dir basename = project_dir.resolve().name.encode(encoding="utf-8") AttributeError: 'str' object has no attribute 'resolve' Error: Process completed with exit code 1.
已尝试的软件包版本
- python3: 3.9.16
- ansible: 8.0.0 或 6.6.0
- ansible-compat: 4.1.2 或 2.2.7
- ansible-core: 2.15.0 或 2.13.9
- ansible-lint: 6.17.0 或 6.10.2
已安装的依赖
- flake8
- yamllint==1.28.0
- codespell
- comment_parser
代码检查任务步骤
- name: 'Lint yaml - ansible, GH actions and GH workflows' run: yamllint -f parsable ./ if: always()
问题分析与解决办法
这个错误核心是ansible-compat模块误将字符串类型的路径当作Path对象处理,调用了仅Path对象才有的resolve()方法,导致属性错误。以下是可行的解决方向:
手动指定缓存目录运行
绕过自动生成缓存目录的逻辑,修改命令为:ansible-lint --parseable --cache-dir ~/.cache/ansible-lint ./ansible调整ansible-compat版本
安装适配Python 3.9的ansible-compat版本,比如:pip install ansible-compat==3.0.0该版本对字符串路径的处理逻辑更兼容Python 3.9环境。
验证路径有效性
确认./ansible路径存在且当前用户有访问权限,避免因路径异常导致参数错误。检查运行环境
确保系统PYTHONPATH环境变量未被篡改,防止导入错误的模块版本干扰运行逻辑。
内容的提问来源于stack exchange,提问作者Annemie
相关产品推荐
相关产品推荐

