Google 部署管理器:MANIFEST_EXPANSION_USER_ERROR 多行变量

Nik*_*s B 6 google-compute-engine

尝试使用带有多行变量的 YAML 和 Jinja 的 Google 部署管理器,例如:

startup_script_passed_as_variable: |
  line 1
  line 2
  line 3
Run Code Online (Sandbox Code Playgroud)

然后:

{% if 'startup_script_passed_as_variable' in properties %}
    - key: startup-script
      value: {{properties['startup_script_passed_as_variable'] }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

给出MANIFEST_EXPANSION_USER_ERROR

错误:(gcloud.deployment-manager.deployments.create) 操作错误 operation-1432566282260-52e8eed22aa20-e6892512-baf7134:

MANIFEST_EXPANSION_USER_ERROR
清单扩展遇到以下错误:扫描“”中的简单键时,在“”中找不到预期的“:”

尝试(但失败):

{% if 'startup_script' in properties %}
        - key: startup-script
          value: {{ startup_script_passed_as_variable }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

{% if 'startup_script' in properties %}
        - key: startup-script
          value: | 
            {{ startup_script_passed_as_variable }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

{% if 'startup_script' in properties %}
        - key: startup-script
          value: | 
            {{ startup_script_passed_as_variable|indent(12) }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

Nik*_*s B 6

问题在于 YAML 和 Jinja 的组合。Jinja 对变量进行了转义,但未能按照 YAML 在作为变量传递时的要求进行缩进。

相关:https : //github.com/saltstack/salt/issues/5480

解决方案:将多行变量作为数组传递

startup_script_passed_as_variable: 
    - "line 1"
    - "line 2"
    - "line 3"
Run Code Online (Sandbox Code Playgroud)

如果您的值以 # 开头(GCE 上的启动脚本就是这样,即 #!/bin/bash),则引用很重要,否则它将被视为注释。

{% if 'startup_script' in properties %}
        - key: startup-script
          value: 
{% for line in properties['startup_script'] %}
            {{line}}
{% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

放在这里是因为 Google 部署经理的问答材料不多。