在Visual Studio Code中未显示Ansible-lint的输出

编程语言 2026-07-09

我在Visual Studio Code中尝试使用官方的Ansible扩展来对我的playbooks运行ansible-lint。

问题在于,当我在VS Code中打开一个playbook时,发现ansible-lint正在执行,并弹出错误窗口,显示以下消息。ansible-lint找到的错误不会在VS Code的编辑器窗口中被标记。通常发现的问题应当以红色或黄色下划线标出,取决于它是警告还是错误。消息:

无法解析ansible-lint的输出。请检查你的ansible-lint安装与配置。更多信息在 Ansible Server 的输出中。

当我查看Ansible Support的输出时(没有名为 Ansible Server 的选项)我可以看到:

> Path for lint: /usr/bin/ansible-lint
Validating using ansible-lint
[Error - 11:19:58] Exception while parsing ansible-lint output: Unexpected token 'y', "yaml[empty"... is not valid JSON
Tried to parse the following:
yaml[empty-lines]: Too many blank lines (1 > 0)
roles/foreman/tasks/activation_key.yml:19

在命令行中使用ansible-lint一切正常。

我已经尝试过:

  • 使用python3.14通过pip安装最新版本的ansible-lint和 ansible-core
  • 使用环境变量ANSIBLE_LINT_FORMAT=json将输出格式设置为JSON
  • 在扩展设置中指定ansible-lint二进制文件的完整路径
  • 重新安装扩展
  • 重新安装ansible-core和 ansible-lint

版本信息:

  • OS: Fedora Linux 43搭载GNOME 49
  • VS Code: 1.122.1,通过RPM包安装
  • Ansible扩展: 26.5.0
  • Ansible: ansible-lint 25使用ansible-core:2.18.12 ansible-compat:25.8.1 ruamel-yaml:0.18.16 ruamel-yaml-clib:0.2.12
  • Python: 3.14.5

ansible.cfg 文件:

[defaults]
# (pathlist) Comma separated list of Ansible inventory sources
inventory=./inventory
remote_user=root

# (path) The vault password file to use. Equivalent to --vault-password-file or --vault-id
# If executable, it will be run and the resulting stdout will be used as the password.
vault_password_file=./.vault_password

# (pathspec) Colon separated paths in which Ansible will search for Roles.
roles_path=./roles:/usr/share/ansible/roles:/etc/ansible/roles

.ansible-lint 文件:

---
exclude_paths:
  - '.git/'
  - 'files/'
  - 'vars/*pwd*'
  - 'meta/'

kinds:
  - tasks: 'tasks/*.{yml,yaml}'
  - vars: 'vars/*.{yml,yaml}'
  - vars: 'defaults/*.{yml,yaml}'
  - meta: 'meta/main.{yml,yaml}'
  - yaml: '.ansible-lint'
  - yaml: '.pre-commit-config.yaml'
  - yaml: '.yamllint'

loop_var_prefix: '^(__|{role}_)'
max_block_depth: 20
offline: true
profile: 'production'
skip_action_validation: false
skip_list: []
task_name_prefix: '{stem} | '
use_default_rules: true
var_naming_pattern: '^[a-z_][a-z0-9_]*$'
warn_list:
  - 'experimental'
  - 'package-latest'
  - 'no-handler'
write_list:
  - 'none'

解决方案

我建议在VS Code中安装 Error Lens扩展。它应该通过直接在编辑器中显示Ansible Lint的诊断信息来解决你的问题。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章