2016-03-21 42 views
2

试图用YAML和神社使用谷歌部署管理器与一个多变量,如:谷歌部署管理:MANIFEST_EXPANSION_USER_ERROR多变量

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

ERROR: (gcloud.deployment-manager.deployments.create) Error in Operation operation-1432566282260-52e8eed22aa20-e6892512-baf7134:

MANIFEST_EXPANSION_USER_ERROR
Manifest expansion encountered the following errors: while scanning a simple key in "" could not found expected ':' 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 %} 

回答

3

问题是YAML和金贾的组合。 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 &用于谷歌部署管理器材料。

0

Jinja没有干净的方法来做到这一点。正如你自己所指出的那样,因为YAML是一个对空白敏感的语言,所以很难有效地进行模板化。

一个可能的破解是将字符串属性拆分为列表,然后迭代列表。

例如,提供物业:

startup-script: | 
    #!/bin/bash 
    python -m SimpleHTTPServer 8080 

您可以在神社模板中使用它:

{% if 'startup_script' in properties %} 
     - key: startup-script 
     value: | 
{% for line in properties['startup-script'].split('\n') %} 
     {{ line }} 
{% endfor %} 

这里也是这方面的一个full working example

这种方法可行,但通常情况下,人们开始考虑使用python模板。因为您正在使用python中的对象模型,所以您不必处理缩进问题。例如:

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

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

+0

想象一下,这就是我最终做的! (作为一个数组传递脚本,每行一个项目) –