我需要find解决我遇到的这个问题。
我有一个散列(它应该被重写为稍后的智能类参数),我在我的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
然而,看看这些文件,我得到两个相同的文件与输出为“alex_super_mario”和“ben_mega_man”键从我的哈希$ game_code:
我想要实现的预期输出基本上是第一次为“alex_super_mario”=> {…进入alex_file.xml和alex用户名一起进行迭代。
ben_mega_man的东西进入本文件。
这可能吗?
<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>
你的模板明确地循环遍历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键不匹配string在nameid)