对 epp 模板使用 puppet 哈希 [英] Using puppet hash for epp templates
问题描述
我在 erb 模板中有下一个代码:
I have next code in erb template:
<% if @proxy_cache_path.is_a?(Hash) -%>
<% @proxy_cache_path.sort_by{|k,v| k}.each do |key,value| -%>
proxy_cache_path <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<% end -%>
如何将其移植到 epp 模板中?我发现它的信息非常少.请帮忙.
How to porting it for epp template? Im find very low information for it. Please help.
推荐答案
您可以这样做:
显示示例类以及如何声明 ERB 和 EPP 模板以进行比较:
Showing an example class and how to declare both an ERB and EPP template for comparison:
# manifests/init.pp
class foo () {
$proxy_cache_path = {
'apples' => 1,
'bananas' => 2,
}
$proxy_cache_levels = 2
$proxy_cache_max_size = 2
$proxy_cache_inactive = 2
# Showing use of ERB:
file { '/foo':
ensure => file,
content => template('foo/mytemplate.erb')
}
# Showing use of EPP, which requires an explicit parameters hash:
file { '/bar':
ensure => file,
content => epp('foo/mytemplate.epp', {
'proxy_cache_path' => $proxy_cache_path,
'proxy_cache_levels' => $proxy_cache_levels,
'proxy_cache_max_size' => $proxy_cache_max_size,
'proxy_cache_inactive' => $proxy_cache_inactive,
}),
}
}
更正*ERB文件的内容以供比较:
Corrected* contents of the ERB file for comparison:
# templates/mytemplate.erb
<% if @proxy_cache_path.is_a?(Hash) -%>
<% @proxy_cache_path.sort_by{|k,v| k}.each do |key,value| -%>
proxy_cache_path <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<% end -%>
<% end -%>
(*问题中的代码缺少结束的end
.)
(*The code in the question is missing the closing end
.)
EPP 文件内容:
# templates/mytemplate.epp
<%- | Hash[String, Integer] $proxy_cache_path, Integer $proxy_cache_levels, Integer $proxy_cache_max_size, Integer $proxy_cache_inactive | -%>
<% include stdlib -%>
<% $proxy_cache_path.keys.sort.each |$key| { -%>
proxy_cache_path <%= $key %> keys_zone=<%= $proxy_cache_path[$key] %> levels=<%= $proxy_cache_levels %> max_size=<%= $proxy_cache_max_size %> inactive=<%= $proxy_cache_inactive -%>
<% } -%>
关于 EPP 模板文件内容的注意事项:
Things to note about the EPP template file content:
1) 参数及其类型在模板的第一行定义.这条线的使用是可选的,但是很好的做法.
1) The parameters and their types are defined on the first line of the template. Use of this line is optional, but good practice.
2) 因为我们在第一行声明了类型,所以测试 $proxy_cache_path
是否是一个哈希是不必要和多余的.
2) Since we declared the types on the first line, it is unnecessary and redundant to test if $proxy_cache_path
is a Hash.
3) 为了访问keys
和sort
函数,我们需要包含stdlib.这与 Ruby (ERB) 不同,后者将这些方法内置于语言中.
3) We need to include stdlib in order to access the functions keys
and sort
. This is different to Ruby (ERB) where these methods are built-in to the language.
4) 我简化了相对于 Ruby (ERB) 的代码,因为 Puppet (EPP) 没有 sort_by
函数 - 实际上也不需要在 ERB 中使用它,这可能是重写为:
4) I simplified the code relative to the Ruby (ERB) because Puppet (EPP) has no sort_by
function - and actually there was no need to use it in the ERB either, which could be re-written as:
<% if @proxy_cache_path.is_a?(Hash) -%>
<% @proxy_cache_path.sort.each do |key,value| -%>
proxy_cache_path <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<% end -%>
<% end -%>
最后一些测试:
# spec/classes/test_spec.rb:
require 'spec_helper'
describe 'foo', :type => :class do
it 'content in foo should be the same as in bar' do
foo = catalogue.resource('file', '/foo').send(:parameters)[:content]
bar = catalogue.resource('file', '/bar').send(:parameters)[:content]
expect(foo).to eq bar
end
end
测试通过.
在此处查看文档.
这篇关于对 epp 模板使用 puppet 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!