📜  Puppet-模板

📅  最后修改于: 2020-10-31 14:09:12             🧑  作者: Mango


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

评估模板

使用简单功能评估模板。

$value = template ("testtemplate.erb")

可以指定模板的完整路径,也可以在Puppet的templatedir中拉出所有模板,该目录通常位于/ var / puppet / templates中。可以通过运行puppet –-configprint templatedir查找目录位置。

模板始终由解析器而不是客户端评估,这意味着如果使用的是puppetmasterd,则模板仅需位于服务器上,而无需将其下载到客户端。在使用模板和将文件的所有内容指定为字符串之间,客户端的看法没有区别。这清楚地表明,在puppet启动阶段,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" 
   } 
} 

以下是模板定义。

.cgi"> 
   SetEnv TEST_ENV "/export/svn/test/" 
  

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

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

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

组合模板

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

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

模板中的迭代

人偶模板还支持数组迭代。如果要访问的变量是一个数组,则可以对其进行迭代。

$values = [val1, val2, otherval]

我们可以有如下模板。

Some stuff with  

上面的命令将产生以下结果。

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

模板中的条件

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

broadcast  

模板和变量

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

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

未定义的变量

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

myvar has  value 

超出范围变量

可以使用lookupvar函数显式地查找超出范围的变量。

样例项目模板

#Different types of backup. Will be done in the same order as specified here. 
#Valid options: rdiff-backup, mysql, command 
backups = rdiff-backup, 
mysql, 
command 
  

[rdiff-backup]  

 
   global-exclude-file =  
 
    
      user =  
 
 
   path =  
  

#Optional extra parameters for rdiff-backup  

extra-parameters =   

#How long backups are going to be kept 
keep =  
 
%= scope.lookupvar('apache::user') %>  

[mysql]  

#ssh user to connect for running the backup 
sshuser =  

#ssh private key to be used 
   sshkey = / 
    
  
[command] 

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

command =