2016-03-21 19 views
2

のような複数行の変数、でYAMLと神社とGoogleのデプロイメント・マネージャーを使用しようとすると:Googleの展開マネージャー: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 & Google Deployment Managerの資料。

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

これは私がやったことです! (スクリプトを配列として渡し、それぞれの行にアイテムを渡します) –

関連する問題