Google compute engine Google部署管理器:清单\扩展\用户\错误多行变量

Google compute engine Google部署管理器:清单\扩展\用户\错误多行变量,google-compute-engine,Google Compute Engine,正在尝试将Google Deployment Manager与YAML和Jinja一起使用多行变量,例如: startup_script_passed_as_variable: | line 1 line 2 line 3 后来: {% if 'startup_script_passed_as_variable' in properties %} - key: startup-script value: {{properties['startup_script_

正在尝试将Google Deployment Manager与YAML和Jinja一起使用多行变量,例如:

startup_script_passed_as_variable: |
  line 1
  line 2
  line 3
后来:

{% if 'startup_script_passed_as_variable' in properties %}
    - key: startup-script
      value: {{properties['startup_script_passed_as_variable'] }}
{% endif %}
给出
清单\u扩展\u用户\u错误

错误:(gcloud.deployment manager.deployments.create)中的错误 操作操作-1432566282260-52e8eed22aa20-E689512-baf7134:

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

尝试(失败):

{% if 'startup_script' in properties %}
        - key: startup-script
          value: {{ startup_script_passed_as_variable }}
{% endif %}


问题在于YAML和Jinja的结合。Jinja会转义变量,但无法按照YAML作为变量传递时的要求缩进该变量

相关的:

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

startup_script_passed_as_variable: 
    - "line 1"
    - "line 2"
    - "line 3"
如果您的值以#开头(GCE上的启动脚本以#!/bin/bash开头),则引号很重要,因为它将被视为注释

{% if 'startup_script' in properties %}
        - key: startup-script
          value: 
{% for line in properties['startup_script'] %}
            {{line}}
{% endfor %}
{% endif %}

把它放在这里,因为Google Deployment manager没有太多问答材料

在金贾没有干净的方法可以做到这一点。正如您自己所指出的,因为YAML是一种对空格敏感的语言,所以很难有效地使用模板

一种可能的方法是将string属性拆分为一个列表,然后在该列表上迭代

例如,提供财产:

startup-script: |
  #!/bin/bash
  python -m SimpleHTTPServer 8080
您可以在Jinja模板中使用它:

{% if 'startup_script' in properties %}
      - key: startup-script
      value: |
{% for line in properties['startup-script'].split('\n') %}
        {{ line }}
{% endfor %}
这里也是一个例子

这种方法会起作用,但通常情况下,当人们开始考虑使用python模板时,就会出现这种情况。因为您使用的是python中的对象模型,所以不必处理缩进问题。例如:

'metadata': {
    'items': [{
        'key': 'startup-script',
        'value': context.properties['startup_script']
    }]
}

可以在示例中找到python模板的一个示例。

我也这么认为,这就是我最后要做的!(将脚本作为数组传递,每行一项)
{% if 'startup_script' in properties %}
      - key: startup-script
      value: |
{% for line in properties['startup-script'].split('\n') %}
        {{ line }}
{% endfor %}
'metadata': {
    'items': [{
        'key': 'startup-script',
        'value': context.properties['startup_script']
    }]
}