2016-04-13 28 views
2

我試圖讓我們的配置更加模塊化一點。目前,我們主要針對每個環境使用硬編碼的模板文件,這些模板文件與處於init狀態的jinja的每個環境相匹配。我將狀態分開,配置並添加一些需要在所有環境中保持相同配置值的缺省值。SaltStack - 如何在支柱中使用字典並通過模板文件中的值循環?

這裏是我的支柱的例子:

/../pillars/amq/amq.sls 
default_routes: 
    Queue1: 
    - from_uri: 'activemq:fromSomeURI1' 
    - process_ref: 'processorName1' 
    - to_uri: 'activemq:toSomeOutURI1' 
    Queue2: 
    - from_uri: 'activemq:fromSomeURI2' 
    - process_ref: 'processorName2' 
    - to_uri: 'activemq:toSomeOutURI2' 

這裏是我的模板文件的例子:

/../salt/amq/conf/camel.xml.template 
lines lines lines 
lines lines lines 
... 
     {% for route, args in pillar.get('default_routes', {}).items() %} 

     <route> 
      <from uri="{{ route.from_uri }}"/> 
      <process ref="{{ route.process_ref }}"/> 
      <to uri="{{ route.to_uri }}"/> 
     </route> 

     {% endfor %} 

... 
lines lines lines 
lines lines lines 

我需要能夠做的是增加值的字典支柱,然後遍歷該默認值列表,從camel.xml.template中構建出所有環境中的默認路由。然後支柱也將存儲環境特定的值,我將以非常相似的方式添加到文件中。

任何幫助,非常感謝。我已經嘗試了很多不同的東西,要麼出現錯誤,要麼從文件中刪除默認行。

謝謝!

回答

5

您如何定義支柱有一些不一致之處。

使用this tool,你YAML轉換到Python給

"default_routes": { 
     "Queue1": [ 
      { 
      "from_uri": "activemq:fromSomeURI1" 
      }, 
      { 
      "process_ref": "processorName1" 
      }, 
      { 
      "to_uri": "activemq:toSomeOutURI1" 
      } 
     ], 
     "Queue2": [ 
      { 
      "from_uri": "activemq:fromSomeURI2" 
      }, 
      { 
      "process_ref": "processorName2" 
      }, 
      { 
      "to_uri": "activemq:toSomeOutURI2" 
      } 
     ] 
     } 

,當我想你的意思像

"default_routes": { 
    "Queue1": { 
     "to_uri": "activemq:toSomeOutURI1", 
     "process_ref": "processorName1", 
     "from_uri": "activemq:fromSomeURI1" 
    }, 
    "Queue2": { 
     "to_uri": "activemq:toSomeOutURI2", 
     "process_ref": "processorName2", 
     "from_uri": "activemq:fromSomeURI2" 
    } 
    } 

東西,你應該將YAML改變

default_routes: 
    Queue1: 
    from_uri: 'activemq:fromSomeURI1' 
    process_ref: 'processorName1' 
    to_uri: 'activemq:toSomeOutURI1' 
    Queue2: 
    from_uri: 'activemq:fromSomeURI2' 
    process_ref: 'processorName2' 
    to_uri: 'activemq:toSomeOutURI2' 

但即使那麼,你的模板

有一個缺陷
{% for route, args in pillar.get('default_routes', {}).items() %} 

該行將把route設置爲密鑰名稱,將args設置爲字典。所以第一次,routeQueue1args將字典的其餘部分。

你必須改變的東西{{ route.from_uri }}{{ args.from_uri }}因爲ARGS是具有像from_uri

+0

好,感謝按鍵實際字典。我做了這些更新,但我仍然沒有得到for循環中的行的任何輸出。從模板生成的文件不會生成for循環應該輸出的內容。也沒有錯誤。 – user797963

+0

@ user797963首先,你應該看看是否爲minion設置了「default_routes」。 「salt」your_minion「pillar.items」應該揭示default_routes柱子是否設置正確。如果是,那麼我們可以轉到下一步調試。 – RedBaron

+0

巨大的幫助,非常感謝。這爲我奠定了基礎,讓我能夠掌握鹽柱,並且向我明確了YAML是如何轉化爲蟒蛇的,然後您可以通過Jinja訪問(那個pillar.items命令可以查看僕從可以訪問的支柱值巨大)。 YAML到python工具也是非常寶貴的。再次感謝! – user797963

相關問題