puppet define 定义
对于重复执行的代码,puppet支持用define来定义函数,然后利用变量来实现。
class user::login {
define admin(uid,uname" :
ensure => "present",
managehome => 'true',
name => "uid",
gid => "uname",
require => Group["uname" :
ensure => "present",
gid => $gid,
}
}
}
define 定义admin的函数,并且定义了几个变量,然后在define中定义所使用的资源,
调用时,先用include包含这个类,然后就可以直接调用这个函数。
class user::user {
include user::login #如果init.pp 包含user::login 这个pp,这里可以不用输入
user::login::admin {'wang':
uname => "wang",
uid => '1000',
gid => '1000',
}
}
事例直接生成gid和uid为1000的为wang的用户名。
puppet 支持使用模版
编写一个模版,需要变量对方使用(%= %) 格式
cat /etc/puppet/modules/apache/templates/vhosts.conf.erb
ServerName <%= domain %>
ServerAdmin admin@<%= domain %>
DocumentRoot <%= docroot %>
ErrorLog logs/<%= error_log %>
CustomLog logs/<%= access_log %> common
调用模版时,在属性上加上template表明使用模版,后面的路径使用相对路径,
apache/vhosts.conf.erb 相当于 /etc/puppet/modules/apache/templates/vhosts.conf.erb
cat vhosts.pp
class apache::vhosts {
define vhost (access_log,port,$docroot) {
file { "vhosts.conf":
name => "/etc/httpd/conf.d/vhosts.conf",
mode => "0744",
owner => "apache",
group => "apache",
require => Class["apache::install"],
notify => Class["apache::service"],
content => template("apache/vhosts.conf.erb"),
}
}
}
cat wang.pp
class apache::wang {
include apache::vhosts #这里可以不用输入,因为init.pp包含进apache::vhosts
apache::vhosts::vhost { "wang":
domain => "wang",
access_log => "logs/wang_access_log",
error_log => "logs/wang_error_log",
port => "80",
docroot => "/var/www/wang",
}
}