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

使用Ansible Galaxy的jdauphant.ssl-certs角色遇YAML语法错误

解决Ansible Playbook YAML语法错误(使用jdauphant.ssl-certs角色时)

我懂你这种照搬示例却踩语法坑的郁闷,咱们直接来搞定这个问题。先看你遇到的错误提示:

ERROR! Syntax Error while loading YAML. mapping values are not allowed in this context
The error appears to have been in '/home/blah/example/provisioning/development.yml': line 11, column 34, but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:

  • jdauphant.ssl-certs ssl_certs_generate_dh_param: true
    ^ here

问题根源

YAML对结构和缩进的要求非常严格,你在roles列表里直接把角色名和变量写在同一行的写法,不符合YAML的语法规范。当需要给角色传递自定义变量时,必须用结构化的键值对形式,把变量缩进嵌套在对应的角色条目下。

修正后的Playbook代码

我帮你调整了所有语法问题,同时优化了Nginx配置的格式,确保完全符合YAML规范:

---
- name: Web01
  hosts: all
  become: yes
  gather_facts: true
  roles:
    - common
    - role: jdauphant.ssl-certs
      ssl_certs_generate_dh_param: true
    - role: jdauphant.nginx
      nginx_configs:
        ssl:
          - "ssl_certificate_key {{ssl_certs_privkey_path}}"
          - "ssl_certificate {{ssl_certs_cert_path}}"
          - "ssl_dhparam {{ssl_certs_dhparam_path}}"
      nginx_sites:
        default:
          - "listen 443 ssl"
          - "server_name _"
          - "root \"/usr/share/nginx/html\""
          - "index index.html"

关键修正说明

  1. 角色传参格式修正:把原来一行式的角色+变量写法,改成了结构化的嵌套形式:

    - role: jdauphant.ssl-certs
      ssl_certs_generate_dh_param: true
    

    这样YAML能清晰识别这是一个角色条目,缩进后的内容是该角色的自定义变量。

  2. 配置项添加引号:给Nginx的每一行配置加上双引号,避免YAML把模板变量(比如{{ssl_certs_privkey_path}})或带空格的路径解析出错,确保语法安全。

  3. 统一缩进规范:所有层级的缩进统一使用2个空格(Ansible官方推荐标准),彻底避免因缩进混乱导致的语法错误。

把这个修正后的代码替换原文件,再运行Playbook应该就能正常执行了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:31:56