꼭두각시 정의 유형 및 1.erb 파일에서 여러 템플릿 생성

꼭두각시 정의 유형 및 1.erb 파일에서 여러 템플릿 생성

나는 내가 직면한 이 문제에 대한 해결책을 찾아야 합니다.

배열을 사용하여 정의한 여러 파일 리소스를 생성하기 위해 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"은 현재 '값' 해시가 현재 영웅 이름과 일치하는지 결정하려는 것입니다(game_code의 키가 문자열과 일치하지 않는다는 점을 고려하면 어떻게 하고 싶은지 말하기 어렵습니다). 이름ID)

관련 정보