2013-04-02 21 views
0

我在廚師模板中有以下代碼,但在上傳到廚師服務器時出錯。我該如何解決?廚師模板DSL - 如何從json加載散列並迭代鍵和值

<% 
    contents_hash = File.read('/tmp/cluster_hash') 
     neoservers_hash = JSON.parse(contents_hash) 
-%> 

<% "#{neoservers_hash}".each_pair do |id, ipaddress| %> 
<%= "server.#{id}=#{ipaddress}:2888:3888" %> 
<% end %> 

當我嘗試上傳菜譜,我收到以下錯誤:

$ knife cookbook upload neo4j -E development 
Uploading neo4j   [0.1.0] 
FATAL: Erb template templates/default/coord.cfg.erb has a syntax error: 
FATAL: -:7: syntax error, unexpected tIDENTIFIER, expecting '}' 
FATAL: _buf << ("server.#{id}=#{ipaddress}:2888:3888").to_s; _buf << ' 
FATAL:     ^
FATAL: -:8: unterminated string meets end of file 

回答

1

你在這一行一個奇怪的語法:

<% "#{neoservers_hash".each_pair do |id, ipaddress| %> 

你似乎嘗試使用使用neoserver_hash變量進行字符串評估,因爲neoserver_hash是哈希值而不是字符串,所以該值不會真正起作用。你也錯過了大括號。相反,你可能想完全擺脫字符串評估和使用這樣的:

<% neoservers_hash.each_pair do |id, ipaddress| %> 
+0

我固定的錯字 – Tampa

+0

如果修復錯字,從根本上改變你的問題的前提。因爲這個錯字(和錯誤的字符串評估)是錯誤的全部原因。 –

+0

錯誤是一樣的。運行代碼時不是一種類型 – Tampa