I need to find a solution to this problem that I've come across.
I have a hash (it's supposed to be overriden as a smart class parameter later on) that I am iterating trough in my template.erb to generate multiple File resources that I have defined using an array. I want the iteration to stop after it's trough the first set of key value pairs and then start writing to the next file.
I'll show an example to make things clearer.
#### 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 file ### 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>
# this generates two files from my array $nameid = ['alex','ben'] as expected.
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
However looking inside these files I get two identical files with the output for both "alex_super_mario" and "ben_mega_man" key from my hash $game_code:
The expected output I want to achieve is basically to have the first set of iterations that is done for "alex_super_mario" => { .... to go inside the alex_file.xml together with the alex username.
and the ben_mega_man stuff go inside the ben file.
Is this possible?
<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>