
我需要找到解決我遇到的這個問題的方法。
我有一個哈希(稍後應該作為智能類參數覆蓋),我在 template.erb 中迭代該哈希以生成我使用數組定義的多個文件資源。我希望迭代在通過第一組鍵值對後停止,然後開始寫入下一個檔案。
我將舉一個例子來讓事情變得更清楚。
#### init.pp ####
class testing (
$nameid = ['alex','ben'],
$game_code = {
alex_super_mario => {
"type" => "1",
"characters" => {
"bowser" => "123",
"luigi" => "456",
"princess" => "789" }
},
ben_mega_man => {
"type" => "2",
"characters" => {
"something" => "111213",
"else" => "131415",
"lastone" => "161718" }
},
}
) {
define testing(
$base_path = '/var/tmp/testing',
){
file {"${base_path}/${name}_file.xml":
owner => 'root',
group => 'root',
ensure => 'file',
mode => '0644',
content => template("testing/template.xml.erb"),
}
}
testing { [$nameid]: }
}
### 範本檔案### template.xml.erb
<% @nameid.each do |username| %>
<UserName><%= username %></UserName>
<% end %>
<games>
<% @game_code.sort.map do |hero, value| %>
<% value.sort.map do |game,code| %>
<% if game == "characters" %>
<% code.each do |k, v| %>
<game>
<type><%= value['type'] %></type>
<gameid><%= v %></gameid>
<extcharacters>
<char>/var/tmp/<%= k %></path>
</extcharacters>
</game>
<% end %>
<% end %>
<% end %>
<% end %>
</games>
# 這會如預期從我的陣列 $nameid = ['alex','ben'] 產生兩個檔案。
Notice: /Stage[main]/Testing/Testing::Testing[ben]/File[**/var/tmp/testing/ben_file.xml**]/ensure: defined content as '{md5}21af03b1dd7427d17c2460bae323bc24'
Notice: /Stage[main]/Testing/Testing::Testing[alex]/File[**/var/tmp/testing/alex_file.xml**]/ensure: defined content as '{md5}21af03b1dd7427d17c2460bae323bc24'
Notice: Finished catalog run in 0.34 seconds
然而,查看這些文件內部,我得到兩個相同的文件,其中包含來自哈希 $game_code 的“alex_super_mario”和“ben_mega_man”鍵的輸出:
我想要實現的預期輸出基本上是為 "alex_super_mario" => { .... 完成第一組迭代,與 alex 使用者名稱一起進入 alex_file.xml 。
ben_mega_man 的內容位於 ben 檔案內。
這可能嗎?
<UserName>alex</UserName> <--- ## this should go into the alex_file.xml
<UserName>ben</UserName> <--- ## this should go into the ben_file.xml ##
<games>
<game> ## <-- this should go to the ben_file.xml
<type>2</type>
<gameid>111213</gameid>
<extcharacters>
<char>/var/tmp/something</path>
</extcharacters>
</game>
<game>
<type>2</type>
<gameid>161718</gameid>
<extcharacters>
<char>/var/tmp/lastone</path>
</extcharacters>
</game>
<game>
<type>2</type>
<gameid>131415</gameid>
<extcharacters>
<char>/var/tmp/else</path>
</extcharacters>
</game>
<game> ### <------ these should go into the alex_file.xml
<type>1</type>
<gameid>123</gameid>
<extcharacters>
<char>/var/tmp/bowser</path>
</extcharacters>
</game>
<game>
<type>1</type>
<gameid>789</gameid>
<extcharacters>
<char>/var/tmp/princess</path>
</extcharacters>
</game>
<game>
<type>1</type>
<gameid>456</gameid>
<extcharacters>
<char>/var/tmp/luigi</path>
</extcharacters>
</game>
</games>
答案1
您的範本明確循環遍歷 nameid,然後循環遍歷 game_code 中的所有鍵,因此每個產生的檔案當然都會包含其中的所有資料。也許像這樣的事情會做:
<UserName><%= @name %></UserName>
<games>
<% @game_code.sort.map do |hero, value|
if (somecondition)
value.sort.map do |game,code|
<if game == "characters"
code.each do |k, v| %>
<game>
<type><%= value['type'] %></type>
<gameid><%= v %></gameid>
<extcharacters>
<char>/var/tmp/<%= k %></path>
</extcharacters>
</game>
<% end
end
end
end
end %>
</games>
其中「somecondition」是您想要決定當前「value」哈希值是否與您當前英雄名稱匹配的哈希值(很難說出您想要如何做到這一點,因為它們在game_code 中的鍵與字串不匹配在nameid中)