将单个erb模板用于单个puppet清单中的不同文件

时间:2016-11-10 10:32:22

标签: templates puppet 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 -%>

在我的puppet清单中,我写了两个文件资源:

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。只有第一个if部分才会被解释。

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

那么,我如何只为每个文件传递所需的变量?

干杯

基督教

2 个答案:

答案 0 :(得分:0)

不可能将不同的变量传递给ERB template()函数,所以我认为您需要:

  1. 使用epp() functionEmbedded Puppet syntax代替ERB,这样可以在调用时将不同的局部变量传递到模板中。
  2. 使用不同的模板或不同的范围。在同一范围内渲染相同的模板通常应该呈现相同的结果,这正是您尝试反对的。

答案 1 :(得分: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,
  }
}

所以我可以在erb上定义不同的包含,我稍微修改了一下。

# 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 -%>