Puppet - 模板

模板是一种以标准格式获取内容的方法,可以在多个位置使用.在Puppet中,使用erb支持模板和模板,erb作为标准Ruby库的一部分,可以在Ruby on Rails项目中用于除Ruby之外的其他项目.作为标准实践,需要对Ruby有基本的了解.当用户尝试管理模板文件的内容时,模板化非常有用.当无法通过内置Puppet类型管理配置时,模板起着关键作用.

评估模板

使用简单函数评估模板./p>

 
 $ value = template("testtemplate.erb")

One可以指定模板的完整路径,也可以在Puppet的templatedir中提取所有模板,模板通常位于/var/puppet/templates.通过运行puppet --configprint templatedir可以找到目录位置.

模板始终由解析器评估,而不是客户端,这意味着如果使用puppetmasterd,则仅模板需要在服务器上,永远不需要将它们下载到客户端.客户端在使用模板和将文件的所有内容指定为字符串之间的看法没有区别.这清楚地表明,在木偶启动阶段,由puppetmasterd首先学习客户特定变量.

使用模板

以下是生成测试网站的tomcat配置.

define testingsite($cgidir, $tracdir) { 
   file { "testing-$name": 
   path => "/etc/tomcat/testing/$name.conf", 
   owner => superuser, 
   group => superuser, 
   mode => 644, 
   require => File[tomcatconf], 
   content => template("testsite.erb"), 
   notify => Service[tomcat] 
}  
   symlink { "testsym-$name": 
      path => "$cgidir/$name.cgi", 
      ensure => "/usr/share/test/cgi-bin/test.cgi" 
   } 
}

以下是模板定义.

<Location "/cgi-bin/ <%= name %>.cgi"> 
   SetEnv TEST_ENV "/export/svn/test/<%= name %>" 
</Location>  

# You need something like this to authenticate users 
<Location "/cgi-bin/<%= name %>.cgi/login"> 
   AuthType Basic 
   AuthName "Test" 
   AuthUserFile /etc/tomcat/auth/svn 
   Require valid-user 
</Location>

这会将每个模板文件推送到一个单独的文件中,然后需要告诉Apache加载这些配置文件.

Include /etc/apache2/trac/[^.#]*

组合模板

使用以下命令可以轻松组合两个模板.

 
 template('/path/to/template1', '/path/to/template2')

模板中的迭代

Puppet模板也支持数组迭代.如果变量1正在访问是一个数组,那么可以迭代它.

 
 $ values = [val1,val2,otherval]

我们可以使用以下模板.

<% values.each do |val| -%> 
Some stuff with <%= val %> 
<% end -%>

上述命令将产生以下结果.

Some stuff with val1 
Some stuff with val2 
Some stuff with otherval

模板中的条件

erb 模板支持条件.以下构造是一种快速简便的方法,可以有条件地将内容放入文件中.

<% if broadcast != "NONE" %> broadcast <%= broadcast %> <% end %>

模板和变量

除了填写文件内容外,还可以使用模板填写变量.

 
 testvariable = template('/var/puppet/template/testvar')

未定义变量

如果需要在使用之前检查变量是否已定义,则以下命令有效.

<% if has_variable?("myvar") then %> 
myvar has <%= myvar %> value 
<% end %>

超出范围变量

可以使用lookupvar函数显式查找范围外变量.

<%= scope.lookupvar('apache::user') %>

示例项目模板

<#Autogenerated by puppet. Do not edit. 
[default] 
#Default priority (lower value means higher priority) 
priority = <%= @priority %> 
#Different types of backup. Will be done in the same order as specified here. 
#Valid options: rdiff-backup, mysql, command 
backups = <% if @backup_rdiff %>rdiff-backup, 
<% end %><% if @backup_mysql %>mysql, 
<% end %><% if @backup_command %>command<% end %> 
<% if @backup_rdiff -%>  

[rdiff-backup]  

<% if @rdiff_global_exclude_file -%> 
   global-exclude-file = <%= @rdiff_global_exclude_file %> 
<% end -%> 
   <% if @rdiff_user -%> 
      user = <%= @rdiff_user %> 
<% end -%> 
<% if @rdiff_path -%> 
   path = <%= @rdiff_path %> 
<% end -%>  

#Optional extra parameters for rdiff-backup  

extra-parameters = <%= @rdiff_extra_parameters %>  

#How long backups are going to be kept 
keep = <%= @rdiff_keep %> 
<% end -%> 
<% if @backup_mysql -%>%= scope.lookupvar('apache::user') %>  

[mysql]  

#ssh user to connect for running the backup 
sshuser =  <%= @mysql_sshuser %>

#ssh private key to be used 
   sshkey = <%= @backup_home %>/<%= @mysql_sshkey %> 
   <% end -%> 
<% if @backup_command -%>  
[command] 

#Run a specific command on the backup server after the backup has finished  

command = <%= @command_to_execute %> 
<% end -%>