Puppet Class(类)特性(十一)
puppet类:为了通用目标或目的的组织在一起的一个或者多个资源;因此它是命名的代码块,在某个位置创建之后可在puppet全局使用.类似于其他编程语言中的类的功能,puppet的类可以继承,也可以包含子类。
类的定义使用class name,模块下的init.pp文件定义和模块同名的类且唯一,可以为空或嵌套其他类.
puppet> 1、有参数的类
2、无参数的类
puppet> 1、class 类只有调用才会执行,调用称作:声明一个类
使用include> 2、类的继承:
class B_name inherits A_name
3、子类的命名方式:
父类::子类
示例:
修改node.pp文件,增加sh-web1主机匹配.
注释:sh-web1声明nginx类.
node /sh-(proxy|web)\d+/ {
case $::hostname {
"sh-proxy2": {
include apache
user {"test1":
ensure => present,
}
}
"sh-web1": {
include nginx
}
}
}
puppet代码nginx>modules/nginx/manifests/init.pp文件(无参数的类定义).
class nginx {
package {"nginx":
ensure=> present,
}
}
puppet代码apche>modules/apache/manifests/init.pp文件(有参数的类定义).
class apache ($sta = "present") {
package {"httpd":
ensure=> $sta,
}
}
sh-web1更新agent安装nginx报错如下:
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505454999'
Error: Execution of '/usr/bin/yum -d 0 -e 0 -y list nginx' returned 1: Error: No matching Packages to list
Error: /Stage/Nginx/Package/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y list nginx' returned 1: Error: No matching Packages to list
Notice: Finished catalog run in 6.93 seconds
原因:没有release源,更新安装eple-release源即可.
# yum install epel-release -y 更新安装nginx.
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505454999'
Notice: /Stage/Nginx/Package/ensure: created
Notice: Finished catalog run in 34.99 seconds
Class类的继承(inherits)
注意:子类的命名不支持特殊符号'.',支持下划线'_'
示例:
新建:modules\nginx\manifests\nginx.conf.pp
class nginx::nginxconf inherits nginx {
file {"/etc/nginx/nginx.conf":
ensure=> file,
mode=> 0644,
source=> 'puppet:///modules/nginx/nginx.conf',
}
}
注意:puppet:///modules/nginx/nginx.conf#不需要写files目录,指定模块文件后puppet默认回去files目录下找对应的文件.
files目录存放puppet代码调用的源文件.
\modules\nginx\files\nginx.conf
修改主机声明的class:
node /sh-(proxy|web)\d+/ {
case $::hostname {
"sh-proxy2": {
include apache
user {"test1":
ensure => present,
}
}
"sh-web1": {
include nginx::nginxconf
}
}
}
注释:声明nginx::nginxconf子类后,会继承父类nginx,安装nginx软件包,然后替换文件.
sh-web1主机更新puppet agent.
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505457148'
Notice: /Stage/Nginx::Nginxconf/File/ensure: defined content as '{md5}142cb8f11006c0d78607c66cb82ae83d'
Notice: /Stage/Nginx/Package/ensure: created
Notice: Finished catalog run in 8.69 seconds
查看更新内容:
# cat nginx.conf | grep 65535
worker_connections65535;
类的嵌套:
模块下的init.pp文件类必须和模块名字相同且唯一,当然Class(类)中也支持嵌套声明其他类或者定义空类.
示例:以apache为例.
test\modules\apache\manifests
cat init.pp
class apache {
include install
include service
}
cat install.pp
class install {
package {"httpd":
ensure=> present,
}
}
cat service.pp
class service {
service {"httpd":
name=> "httpd",
ensure=> running,
enable=> true,
provider => init,
require=> Class["install"],#注意:此处要求启动服务的前提时要完成install类的操作,如果不定义可能会报下面的错.
}
}
报错:
# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-proxy2.localdomain
Info: Applying configuration version '1505714213'
Error: /Service: Could not evaluate: Could not find init script for 'httpd'
Notice: /Stage/Admin/Exec/returns: executed successfully
Notice: /Stage/Install/Package/ensure: created
Notice: Finished catalog run in 2.75 seconds
添加完require正确的更新:
# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-proxy2.localdomain
Info: Applying configuration version '1505714421'
Notice: /Stage/Admin/Exec/returns: executed successfully
Notice: /Stage/Install/Package/ensure: created
Notice: /Stage/Service/Service/ensure: ensure changed 'stopped' to 'running'
Info: /Service: Unscheduling refresh on Service
Notice: Finished catalog run in 3.27 seconds
# /etc/init.d/httpd status
httpd (pid88530) is running...
页:
[1]