<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 在模板中遍歷數組 在前面的例子中,我們已經看到使用 Ruby 可以根據表達式的結果插入不同的值。 你也可以使用循環對數組中的每個元素生成內容。 #### 操作步驟 1. 添加如下代碼到你的配置清單: ``` $ipaddresses = [ '192.168.0.1', '158.43.128.1', '10.0.75.207' ] file { "/tmp/addresslist.txt": content =&gt; template("admin/addresslist.erb") } ``` 2. 使用如下內容創建 /etc/puppet/modules/admin/templates/addresslist.erb 文件: ``` &lt;% ipaddresses.each do |ip| -%&gt; IP address &lt;%= ip %&gt; is present. &lt;% end -%&gt; ``` 3. 運行 Puppet: ``` # puppet agent --test info: Retrieving plugin info: Caching catalog for cookbook.bitfieldconsulting.com info: Applying configuration version '1304766335' notice: /Stage[main]//Node[cookbook]/File[/tmp/addresslist.txt]/ ensure: defined content as '{md5}7ad1264ebdae101bb5ea0afef474b3ed' notice: Finished catalog run in 0.64 seconds ``` 4. 檢查生成文件的內容: ``` # cat /tmp/addresslist.txt IP address 192.168.0.1 is present. IP address 158.43.128.1 is present. IP address 10.0.75.207 is present. ``` #### 工作原理 1. 在模板的第一行,我們引用了一個數組 ipaddresses,并調用了相應的 each 方法: ``` &lt;% ipaddresses.each do |ip| -%&gt; ``` 2. 使用 Ruby 創建的這個循環將對數組中的每個元素執行一次。 每循環一次,變量 ip 將會設置成數組當前元素的值。 3. 在我們的例子中,ipaddresses 數組包含三個元素,所以下面的行將執行三次, 對每個數組元素執行一次: ``` IP address &lt;%= ip %&gt; is present. ``` 4. 結果將產生如下三行內容: ``` IP address 192.168.0.1 is present. IP address 158.43.128.1 is present. IP address 10.0.75.207 is present. ``` 5. 最后一行是循環的結束: ``` &lt;% end -%&gt; ``` 6. 注意循環的第一行和最后一行都以 -%&gt; 結束,而不是我們以前所看到的以 %&gt; 結束。 使用 - 的作用是要抑制換行,否則會產生換行符,即在文件中產生無用的空白行。 #### 更多用法 模板也可以遍歷哈希,或哈希數組,例如: ``` $interfaces = [ { name => 'eth0', ip => '192.168.0.1' }, { name => 'eth1', ip => '158.43.128.1' }, { name => 'eth2', ip => '10.0.75.207' } ] <% interfaces.each do |interface| -%> Interface <%= interface['name'] %> has the address <%= interface['ip']%>. <% end -%> ``` 結果為: ``` Interface eth0 has the address 192.168.0.1. Interface eth1 has the address 158.43.128.1. Interface eth2 has the address 10.0.75.207. ``` #### 參見本書 * 第 5 章的 [使用 ERB 模板](#ch05sec04) 一節
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看