尝试在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 %}给出MANIFEST_EXPANSION_USER_ERROR
错误:(gcloud.deployment-manager.deployments.create)操作中的operation-1432566282260-52e8eed22aa20-e6892512-baf7134:错误 MANIFEST_EXPANSION_USER_ERROR 清单扩展遇到以下错误:扫描“无法找到预期的”:“in”中的简单键
尝试(失败):
{% if 'startup_script' in properties %}
- key: startup-script
value: {{ startup_script_passed_as_variable }}
{% endif %}也是
{% if 'startup_script' in properties %}
- key: startup-script
value: |
{{ startup_script_passed_as_variable }}
{% endif %}和
{% if 'startup_script' in properties %}
- key: startup-script
value: |
{{ startup_script_passed_as_variable|indent(12) }}
{% endif %}发布于 2016-03-21 13:35:34
问题是YAML和Jinja的结合。Jinja转义该变量,但当作为变量传递时,无法按照YAML的要求将其缩进。
相关:https://github.com/saltstack/salt/issues/5480
解决方案:将多行变量作为数组传递
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 %}把它放在这里,因为没有太多的Q&A材料的谷歌部署经理。
发布于 2016-04-06 23:01:55
在金加,没有干净的方法可以做到这一点。正如您自己所指出的,因为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模板的一个示例。
https://stackoverflow.com/questions/36132503
复制相似问题