
私が遭遇したこの問題の解決策を見つける必要があります。
私は、配列を使用して定義した複数のファイル リソースを生成するために、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'] から 2 つのファイルが生成されます。
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」の両方のキーの出力を含む 2 つの同一ファイルが表示されます。
私が達成したい期待出力は、基本的に、"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」は、現在の「値」ハッシュが現在のヒーロー名と一致するかどうかを判断します(game_code のキーが nameid の文字列と一致しないことを考えると、それをどのように行うかはわかりません)。