|
示例1:使用include调用类
cat class1.pp
class apache {
$webpkg = $operationsystem ? {
/(?i-mx:(centos|redhat|fedora))/ => 'httpd',
/(?i-mx:(ubuntu|debian))/ => 'apache2',
default => 'httpd',
}
#类中使用流程控制selector,为自定义变量webpkg赋值
package{"$webpkg":
ensure => installed,
}
#类中使用package资源,安装软件包
file{'httpd.conf':
ensure => file,
path => '/etc/httpd/conf/httpd.conf',
source => '/app/httpd.conf',
require => Package["$webpkg"],
}
#类中调用file资源,复制httod的配置文件,而且此资源依赖于package资源
service{'httpd':
ensure => running,
enable => true,
hasrestart => true,
restart => 'service httpd restart',
subscribe => File['httpd.conf'],
}
#类中调用service资源,启动httpd服务,并启动订阅功能在配置文件改变时重启服务
}
#到此都是class的范围,所谓的类就是将多个资源打包在一起,对外呈现为独立个体
include apache #使用include调用类,否则我们只是定义类,并不会执行
示例2:定义资源一样定义类
cat class2.pp
class webserver($pkg,$srv) {
package{"$pkg":
ensure => latest,
}
service{"$srv":
ensure => running,
}
}
if $operatingsystem == "CentOS" or $operatingsystem == "RedHat" {
case $operatingsystemmajrelease {
'7': { $pkgname = 'httpd' $srvname = 'httpd' }
default: { $pkgname = 'nginx' $srvname = 'nginx' }
}
}
class{'webserver':
pkg => "$pkgname",
srv => "$srvname",
} |
|
|