puppet类:
为了通用目标或目的的组织在一起的一个或者多个资源;因此它是命名的代码块,在某个位置创建之后可在puppet全局使用.类似于其他编程语言中的类的功能,puppet的类可以继承,也可以包含子类。
类的定义使用class name,模块下的init.pp文件定义和模块同名的类且唯一,可以为空或嵌套其他类.
puppet class(类)有两种:
1、有参数的类
2、无参数的类
puppet class(类)的特性:
1、class 类只有调用才会执行,调用称作:声明一个类
使用include class_name
2、类的继承:
class B_name inherits A_name
3、子类的命名方式:
父类::子类
示例:
修改node.pp文件,增加sh-web1主机匹配.
注释:sh-web1声明nginx类.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
node
/sh-
(proxy|web)\d+/ {
case
$::
hostname
{
"sh-proxy2"
: {
include apache
user {
"test1"
:
ensure => present,
}
}
"sh-web1"
: {
include nginx
}
}
}
|
puppet代码nginx class.
1
2
3
4
5
6
|
modules
/nginx/manifests/init
.pp文件(无参数的类定义).
class nginx {
package {
"nginx"
:
ensure=> present,
}
}
|
puppet代码apche class.
1
2
3
4
5
6
|
modules
/apache/manifests/init
.pp文件(有参数的类定义).
class apache ($sta =
"present"
) {
package {
"httpd"
:
ensure=> $sta,
}
}
|
sh-web1更新agent安装nginx报错如下:
1
2
3
4
5
6
7
8
9
|
[root@sh-web1 puppet]
# 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
[main]
/Nginx/Package
[nginx]
/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源即可.
1
|
# yum install epel-release -y
|
更新安装nginx.
1
2
3
4
5
6
7
8
|
[root@sh-web1 puppet]
# 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
[main]
/Nginx/Package
[nginx]
/ensure
: created
Notice: Finished catalog run
in
34.99 seconds
|
Class类的继承(inherits)
注意:子类的命名不支持特殊符号'.',支持下划线'_'
示例:
新建:modules\nginx\manifests\nginx.conf.pp
1
2
3
4
5
6
7
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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.
1
2
3
4
5
6
7
8
9
|
[root@sh-web1 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
'1505457148'
Notice:
/Stage
[main]
/Nginx
::Nginxconf
/File
[
/etc/nginx/nginx
.conf]
/ensure
: defined content as
'{md5}142cb8f11006c0d78607c66cb82ae83d'
Notice:
/Stage
[main]
/Nginx/Package
[nginx]
/ensure
: created
Notice: Finished catalog run
in
8.69 seconds
|
查看更新内容:
1
2
|
[root@sh-web1 nginx]
# cat nginx.conf | grep 65535
worker_connections 65535;
|
类的嵌套:
模块下的init.pp文件类必须和模块名字相同且唯一,当然Class(类)中也支持嵌套声明其他类或者定义空类.
示例:以apache为例.
test\modules\apache\manifests
1
2
3
4
5
|
cat
init.pp
class apache {
include
install
include service
}
|
1
2
3
4
5
6
|
cat
install
.pp
class
install
{
package {
"httpd"
:
ensure=> present,
}
}
|
1
2
3
4
5
6
7
8
9
10
|
cat
service.pp
class service {
service {
"httpd"
:
name=>
"httpd"
,
ensure=> running,
enable
=>
true
,
provider => init,
require=> Class[
"install"
],
#注意:此处要求启动服务的前提时要完成install类的操作,如果不定义可能会报下面的错.
}
}
|
报错:
1
2
3
4
5
6
7
8
9
|
[root@sh-proxy2 puppet]
# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-proxy2.localdomain
Info: Applying configuration version
'1505714213'
Error:
/Service
[httpd]: Could not evaluate: Could not
find
init script
for
'httpd'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice:
/Stage
[main]
/Install/Package
[httpd]
/ensure
: created
Notice: Finished catalog run
in
2.75 seconds
|
添加完require正确的更新:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@sh-proxy2 puppet]
# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-proxy2.localdomain
Info: Applying configuration version
'1505714421'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice:
/Stage
[main]
/Install/Package
[httpd]
/ensure
: created
Notice:
/Stage
[main]
/Service/Service
[httpd]
/ensure
: ensure changed
'stopped'
to
'running'
Info:
/Service
[httpd]: Unscheduling refresh on Service[httpd]
Notice: Finished catalog run
in
3.27 seconds
[root@sh-proxy2 puppet]
# /etc/init.d/httpd status
httpd (pid 88530) is running...
|
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1966274