首页 > 解决方案 > 在变量等号中使用时出现 Ansible 错误

问题描述

我有一个可靠的角色。当启动 ansibple-playbook main.yml -C 得到这个错误:

ERROR! Syntax Error while loading YAML.
  found unknown escape character

The error appears to be in '/main': line 41, column 93, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

service          : "-XX:+UseG1GC -Xmx3584m -Xms3584m -Xmn896m -XX:MaxPermSize=512M -XX:SurvivorRatio=5 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -XX:MaxTenuringThreshold=10 -XX:InitialTenuringThreshold=10 -Xloggc:gclogs/gc.log -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2000k"
                                                                                            ^ here

There appears to be both 'k=v' shorthand syntax and YAML in this task. Only one syntax may be used.
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"

在我们的 group_vars 文件夹中,我们使用如下变量:

service          : "-XX:+UseG1GC -Xmx3584m -Xms3584m -Xmn896m -XX:MaxPermSize=512M -XX:SurvivorRatio=5 -Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -XX:MaxTenuringThreshold=10 -XX:
InitialTenuringThreshold=10 -Xloggc:gclogs/gc.log -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCDate
Stamps -XX:+PrintTenuringDistribution -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2000k"

这是作为 systemd 单元启动的命令。我哪里错了?我认为等号错误,但我无法替换它。

标签: ansible

解决方案


这一定是格式或缩进的问题。下面的剧本按预期工作。

- hosts: localhost
  vars:
    svc: "-XX:+UseG1GC -Xmx3584m -Xms3584m -Xmn896m -XX:MaxPermSize=512M -XX:SurvivorRatio=5
          -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9090
          -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false
          -XX:MaxTenuringThreshold=10 -XX:InitialTenuringThreshold=10 -Xloggc:gclogs/gc.log
          -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime
          -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+UseGCLogFileRotation
          -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2000k"
  tasks:
    - debug:
        var: svc

推荐阅读