2016-11-10 53 views
-1

我喜歡使用單個erb模板創建不同的文件。這是我的erb。在單個木偶清單中爲單個erb模板使用不同的文件

# managed by puppet 
# changes will be overwritten 
# 
<% if (@tanuki_ssl != nil) -%> 
# config include 
# ssl section for trust and key stores 
# 
<% unless @tanuki_ssl['truststore_path'].nil? -%> 
wrapper.java.additional.940=-Djavax.net.ssl.trustStore=<%= @tanuki_ssl['truststore_path'] %> 
<% end -%> 
<% unless @tanuki_ssl['truststore_pass'].nil? -%> 
wrapper.java.additional.941=-Djavax.net.ssl.trustStorePassword=<%= @tanuki_ssl['truststore_pass'] %> 
<% end -%> 
<% end -%> 
<% if (@tanuki_proxy != nil) -%> 
# config include 
# proxy section 
# 
wrapper.java.additional.951=-Dhttp.proxySet=true 
wrapper.java.additional.952=-Dhttp.proxyHost=<%= @tanuki_proxy['host'] %> 
wrapper.java.additional.953=-Dhttp.proxyPort=<%= @tanuki_proxy['port'] %> 
wrapper.java.additional.954=-Dhttp.nonProxyHosts=<%= @tanuki_proxy['exceptions'].join('|') %>|<%= @ipaddress %>|<%= @hostname %> 
wrapper.java.additional.955=-Dhttps.proxySet=true 
wrapper.java.additional.956=-Dhttps.proxyHost=<%= @tanuki_proxy['host'] %> 
wrapper.java.additional.957=-Dhttps.proxyPort=<%= @tanuki_proxy['port'] %> 
wrapper.java.additional.958=-Dhttps.nonProxyHosts=<%= @tanuki_proxy['exceptions'].join('|') %>|<%= @ipaddress %>|<%= @hostname %> 
<% end -%> 

在我的傀儡清單我寫了兩個文件資源:

if $tanuki_hash['ssl_enabled'] { 

    $tanuki_ssl = $tanuki_hash['ssl'] 

    file { "${tanuki_path}/${dirname}/conf/940-ssl.inc": 
    content => template('profile/app/wrapper.inc.erb'), 
    } 
} 

if $tanuki_hash['proxy_enabled'] { 

    if $tanuki_hash['override_system_proxy'] { 
    $tanuki_proxy=$tanuki_hash['proxy'] 
    } else { 
    $tanunki_proxy=$proxy_hash 
    } 

    file { "${tanuki_path}/${dirname}/conf/950-proxy.inc": 
    content => template('profile/app/wrapper.inc.erb'), 
    } 
} 

我我跑的木偶現在我得到一個文件940 ssl.incl與預期的內容。只有第一部分將被解釋。

但是文件950-proxy.inc也有ssl部分。

那麼,我怎麼可以只傳遞每個文件所需的變量?

乾杯

基督教

回答

0

我發現另一種方法適用於我。我創建了一個定義:

define tools::app::tanuki::include (
    Hash $property_hash  = {}, 
    String $include_template = 'tools/app/tanuki/wrapper.inc.erb', 
    String $include_filename = $title, 
){ 
    if $property_hash != {} { 
    file { $include_filename: 
     content => template($include_template), 
    } 
    } 
} 

在我的傀儡清單我改變了代碼如下:

if $tanuki_hash['ssl_enabled'] { 

    $include_ssl = { type => 'ssl' } 
    $tanuki_ssl_hash = merge($tanuki_hash['ssl'],$include_ssl) 

    tools::app::tanuki::include { "${tanuki_path}/${dirname}/conf/940-ssl.inc": 
    property_hash => $tanuki_ssl_hash, 
    } 
} 

if $tanuki_hash['proxy_enabled'] { 

    $include_proxy = { type => 'proxy' } 

    if $tanuki_hash['override_system_proxy'] { 
    $tanuki_proxy=$tanuki_hash['proxy'] 
    $tanuki_proxy_hash = merge($tanuki_hash['proxy'],$include_proxy) 
    } else { 
    $tanunki_proxy=$proxy_hash 
    $tanuki_proxy_hash = merge($proxy_hash,$include_proxy) 
    } 

    tools::app::tanuki::include { "${tanuki_path}/${dirname}/conf/950-proxy.inc": 
    property_hash => $tanuki_proxy_hash, 
    } 
} 

這樣我就可以定義不同的包括與僱員再培訓局,這是我修改一點點。

# managed by puppet 
# changes will be overwritten 
# 
<% if (@property_hash['type'] == 'ssl') -%> 
# config include 
# ssl section for trust and key stores 
# 
<% unless @property_hash['truststore_path'].nil? -%> 
wrapper.java.additional.940=-Djavax.net.ssl.trustStore=<%= @property_hash['truststore_path'] %> 
<% end -%> 
<% unless @property_hash['truststore_pass'].nil? -%> 
wrapper.java.additional.941=-Djavax.net.ssl.trustStorePassword=<%= @property_hash['truststore_pass'] %> 
<% end -%> 
<% elsif (@property_hash['type'] == 'proxy') -%> 
# config include 
# proxy section 
# 
wrapper.java.additional.951=-Dhttp.proxySet=true 
wrapper.java.additional.952=-Dhttp.proxyHost=<%= @property_hash['host'] %> 
wrapper.java.additional.953=-Dhttp.proxyPort=<%= @property_hash['port'] %> 
wrapper.java.additional.954=-Dhttp.nonProxyHosts=<%= @property_hash['exceptions'].join('|') %>|<%= @ipaddress %>|<%= @hostname %> 
wrapper.java.additional.955=-Dhttps.proxySet=true 
wrapper.java.additional.956=-Dhttps.proxyHost=<%= @property_hash['host'] %> 
wrapper.java.additional.957=-Dhttps.proxyPort=<%= @property_hash['port'] %> 
wrapper.java.additional.958=-Dhttps.nonProxyHosts=<%= @property_hash['exceptions'].join('|') %>|<%= @ipaddress %>|<%= @hostname %> 
<% end -%> 
0

這是不可能的不同變量傳遞給ERB template()功能,所以我覺得你需要:

  1. 使用epp() functionEmbedded Puppet syntax代替的ERB,它可以讓你在調用它時將不同的局部變量傳遞到模板中。
  2. 使用不同的模板或不同的範圍。在同一範圍內渲染相同的模板通常應該呈現相同的結果,這正是您要處理的內容。
相關問題