-
puppet及附属依赖包是否已经安装OK?
-
puppet配置文件是否正确?
-
puppet服务是否正常运行?
-
在更新puppet配置文件的情况下,是否能够主动让puppet服务重启或者reload?
-
puppet安装包是否能够自动升级到指定版本?
Puppet基础篇-编写第一个完整测试模块puppet
1
2
3
4
5
6
7
8
9
10
11
|
[root@puppetmaster ~]# cd /etc/puppet/modules/
[root@puppetmaster modules]# mkdir puppet
[root@puppetmaster modules]# cd puppet/
[root@puppetmaster puppet]# mkdir files manifests templates #创建模块目录结构
[root@puppetmaster puppet]# tree ../puppet
../puppet
├── files #存放下载的文件
├── manifests #存放puppet配置
└── templates #存放配置模板,方便pp文件引用
3
directories,
0
files
[root@puppetmaster puppet]#
|
2、创建puppet配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@puppetmaster puppet]# cd manifests/
[root@puppetmaster manifests]# touch init.pp config.pp install.pp service.pp params.pp
[root@puppetmaster manifests]# tree ../
../
├── files
├── manifests
│ ├── config.pp #管理puppet配置
│ ├── init.pp #管理模块所有pp文件配置
│ ├── install.pp #管理puppet安装
│ ├── params.pp #管理模块中变量以及一些判断
│ └── service.pp #管理puppet服务
└── templates
3
directories,
5
files
|
3、编写puppet模块配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@puppetmaster manifests]# vim install.pp
class
puppet::install{ #一个类包含两个子类
include
puppet::puppet_install,puppet::facter_install
}
class
puppet::puppet_install{
package
{
'puppet'
:
ensure => installed, #要求处于被安装状态
}
}
class
puppet::facter_install{
package
{
'facter'
:
ensure => installed,
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@puppetmaster manifests]# vim install.pp
class
puppet::install{ #一个类包含两个资源
package
{
'puppet'
:
ensure => installed,
}
package
{
'facter'
:
ensure => installed,
}
}
[root@puppetmaster manifests]# vim install.pp
class
puppet::install{
package
{ [
'puppet'
,
'facter'
]: #采用数组的形式
ensure => installed,
}
}
|
1
2
3
4
|
[root@agent1 ~]# facter | grep operatingsystemmajrelease
operatingsystemmajrelease =>
5
[root@agent3 ~]# facter | grep operatingsystemmajrelease
operatingsystemmajrelease =>
6
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@puppetmaster manifests]# vim install.pp
class
puppet::install{
include
puppet::puppet_install,puppet::facter_install
}
class
puppet::puppet_install{
package
{
'puppet'
:
ensure => $operatingsystemmajrelease ?{ #判断系统版本
5
=>
'2.7.25-1.el5'
,
6
=>
'2.7.25-1.el6'
,
}
}
}
class
puppet::facter_install{
package
{
'facter'
:
ensure => $operatingsystemmajrelease ?{
5
=>
'1.7.5-1.el5'
,
6
=>
'1.7.5-1.el6'
,
}
}
}
|
1
2
3
4
|
[root@puppetmaster manifests]# vim init.pp
class
puppet{
include
puppet::install
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
$puppetmaster =
'puppetmaster.kisspuppet.com'
node
'puppetmaster_cert.kisspuppet.com'
{
include
motd,puppet
}
node
'agent1_cert.kisspuppet.com'
{
include
motd,puppet
}
node
'agent2_cert.kisspuppet.com'
{
include
motd,puppet
}
node
'agent3_cert.kisspuppet.com'
{
include
motd,puppet
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
$puppetmaster =
'puppetmaster.kisspuppet.com'
class
environments{
include
motd,puppet
}
node
'puppetmaster_cert.kisspuppet.com'
{
include
environments
}
node
'agent1_cert.kisspuppet.com'
{
include
environments
}
node
'agent2_cert.kisspuppet.com'
{
include
environments
}
node
'agent3_cert.kisspuppet.com'
{
include
environments
}
|
1
2
3
4
5
6
7
8
|
[root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
$puppetmaster =
'puppetmaster.kisspuppet.com'
class
environments{
include
motd,puppet
}
node
default
{
include
environments
}
|
1
2
3
4
5
6
7
|
[root@agent1 ~]# rpm -e facter --nodeps
[root@agent1 ~]# rpm -ivh facter-
1.7
.
3
-
1
.el5.x86_64.rpm
warning: facter-
1.7
.
3
-
1
.el5.x86_64.rpm: Header V3 RSA/SHA1 signature: NOKEY, key ID 4bd6ec30
Preparing... ########################################### [
100
%]
1
:facter ########################################### [
100
%]
[root@agent1 ~]# facter --version
1.7
.
3
|
1
2
3
4
5
6
7
8
9
10
|
[root@agent1 ~]# puppet agent -t --noop
notice: Ignoring --listen on onetime run
info: Caching catalog
for
agent1_cert.kisspuppet.com
info: Applying configuration version
'1394794815'
notice: /Stage[main]/Puppet::Facter_install/Package[facter]/ensure: current_value
1.7
.
3
-
1
.el5, should be
1.7
.
5
-
1
.el5 (noop)
notice: Class[Puppet::Facter_install]: Would have triggered
'refresh'
from
1
events
notice: Stage[main]: Would have triggered
'refresh'
from
1
events
notice: Finished catalog run
in
0.23
seconds
[root@agent1 ~]# facter --version
1.7
.
3
|
1
2
3
4
5
6
7
8
|
[root@agent1 ~]# puppet agent -t
notice: Ignoring --listen on onetime run
info: Caching catalog
for
agent1_cert.kisspuppet.com
info: Applying configuration version
'1394794815'
notice: /Stage[main]/Puppet::Facter_install/Package[facter]/ensure: ensure changed
'1.7.3-1.el5'
to
'1.7.5-1.el5'
notice: Finished catalog run
in
6.27
seconds
[root@agent1 ~]# facter --version
1.7
.
5
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@puppetmaster manifests]# vim config.pp
class
puppet::config{
file {
'/etc/puppet/puppet.conf'
: #节点文件存放的路径
ensure => present, #要求存在
content => template(
'puppet/puppet.conf.erb'
), #要求根据模板生成,路径写法为相对路径(templates目录隐藏掉)
owner =>
'root'
, #要求文件属主为root
group =>
'root'
, #要求文件属组为root
mode =>
'0644'
, #要求文件权限为
644
require => Class[
'puppet::install'
], #要求这个文件在配置之前先正确运行install.pp文件,也就是说要求puppet的包应当处于安装状态
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@agent1 ~]# vim /etc/puppet/puppet.conf
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
ssldir = $
var
dir/ssl
[agent]
classfile = $
var
dir/classes.txt
localconfig = $
var
dir/localconfig
server = puppetmaster.kisspuppet.com #变量
certname = agent1_cert.kisspuppet.com #变量
runinterval =
10
listen =
true
|
1
2
3
4
|
[root@agent1 ~]# facter |grep hostname
hostname => agent1
[root@agent3 ~]# facter |grep hostname
hostname => agent3
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@puppetmaster manifests]# vim params.pp
class
puppet::params {
$puppetserver =
'puppetmaster.kisspuppet.com'
#增加puppetserver变量指向puppetmaster名称
case
$hostname{ #增加certname变量
agent1: {
$certname =
'agent1_cert.kisspuppet.com'
}
agent3: {
$certname =
'agent3_cert.kisspuppet.com'
}
default
: { #设置默认不存在的情况下报错
fail(
"certname is not supported on ${::operatingsystem}"
)
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@puppetmaster manifests]# vim ../templates/puppet.conf.erb
### config by puppet ###
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
ssldir = $
var
dir/ssl
[agent]
classfile = $
var
dir/classes.txt
localconfig = $
var
dir/localconfig
server = <%= scope.lookup
var
(
'puppet::params::puppetserver'
) %> #引用变量puppetserver
certname = <%= scope.lookup
var
(
'puppet::params::certname'
) %> #引用变量certname
runinterval =
10
listen =
true
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@puppetmaster manifests]# vim config.pp
class
puppet::config{
include
puppet::params #添加引用关系
file {
'/etc/puppet/puppet.conf'
:
ensure => present,
content => template(
'puppet/puppet.conf.erb'
),
owner =>
'root'
,
group =>
'root'
,
mode =>
'0644'
,
require => Class[
'puppet::install'
],
}
}
|
1
2
3
4
|
[root@puppetmaster manifests]# vim init.pp
class
puppet{
include
puppet::install,puppet::config
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@agent1 ~]# puppet agent -t --noop
notice: Ignoring --listen on onetime run
info: Caching catalog
for
agent1_cert.kisspuppet.com
info: Applying configuration version
'1394797763'
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
--- /etc/puppet/puppet.conf
2014
-
03
-
10
08
:
22
:
33.000000000
+
0800
+++ /tmp/puppet-file20140314-
7231
-f50ehp-
0
2014
-
03
-
14
19
:
49
:
24.000000000
+
0800
@@ -
1
,
3
+
1
,
4
@@
+### config by puppet ### #添加部分
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: current_value {md5}fb17740fd53d8d4dfd6d291788a9bda3, should be {md5}134bae34adddbf30a3fe02ff0eb3c6a6 (noop)
notice: Class[Puppet::Config]: Would have triggered
'refresh'
from
1
events
notice: Stage[main]: Would have triggered
'refresh'
from
1
events
notice: Finished catalog run
in
0.43
seconds
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
[root@agent1 ~]# puppet agent -t
notice: Ignoring --listen on onetime run
info: Caching catalog
for
agent1_cert.kisspuppet.com
info: Applying configuration version
'1394797763'
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
--- /etc/puppet/puppet.conf
2014
-
03
-
10
08
:
22
:
33.000000000
+
0800
+++ /tmp/puppet-file20140314-
7475
-mlybgg-
0
2014
-
03
-
14
19
:
50
:
16.000000000
+
0800
@@ -
1
,
3
+
1
,
4
@@
+### config by puppet ###
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
info: FileBucket adding {md5}fb17740fd53d8d4dfd6d291788a9bda3
info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet
with
sum fb17740fd53d8d4dfd6d291788a9bda3
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed
'{md5}fb17740fd53d8d4dfd6d291788a9bda3'
to
'{md5}134bae34adddbf30a3fe02ff0eb3c6a6'
notice: Finished catalog run
in
0.34
seconds
[root@agent1 ~]# cat /etc/puppet/puppet.conf
### config by puppet ###
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
ssldir = $
var
dir/ssl
[agent]
classfile = $
var
dir/classes.txt
localconfig = $
var
dir/localconfig
server = puppetmaster.kisspuppet.com #根据预先定的puppetserver变量生成
certname = agent1_cert.kisspuppet.com #根据预先定义的certname变量生成
runinterval =
10
[root@agent3 ~]# puppet agent -t
info: Caching certificate
for
agent3_cert.kisspuppet.com
info: Caching certificate_revocation_list
for
ca
info: Caching catalog
for
agent3_cert.kisspuppet.com
info: Applying configuration version
'1394797763'
notice: /Stage[main]/Motd/File[/etc/motd]/content:
--- /etc/motd
2010
-
01
-
12
21
:
28
:
22.000000000
+
0800
+++ /tmp/puppet-file20140314-
2786
-1wb4mas-
0
2014
-
03
-
14
19
:
51
:
27.589533699
+
0800
@@ -
0
,
0
+
1
,
3
@@
+-- --
+--------puppet test---------
+-- --
info: FileBucket adding {md5}d41d8cd98f00b204e9800998ecf8427e
info: /Stage[main]/Motd/File[/etc/motd]: Filebucketed /etc/motd to puppet
with
sum d41d8cd98f00b204e9800998ecf8427e
notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed
'{md5}d41d8cd98f00b204e9800998ecf8427e'
to
'{md5}87ea3a1af8650395038472457cc7f2b1'
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
--- /etc/puppet/puppet.conf
2014
-
03
-
09
01
:
50
:
46.112175841
+
0800
+++ /tmp/puppet-file20140314-
2786
-z4e844-
0
2014
-
03
-
14
19
:
51
:
27.719533700
+
0800
@@ -
1
,
3
+
1
,
4
@@
+### config by puppet ###
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
@@ -
8
,
3
+
9
,
5
@@
localconfig = $
var
dir/localconfig
server = puppetmaster.kisspuppet.com
certname = agent3_cert.kisspuppet.com
+ runinterval =
10
+ listen =
true
info: FileBucket adding {md5}03cbe6d4def560996eeacedfaef229b4
info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet
with
sum 03cbe6d4def560996eeacedfaef229b4
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed
'{md5}03cbe6d4def560996eeacedfaef229b4'
to
'{md5}4f57479998961563e3306b5d0e02a678'
info: Creating state file /
var
/lib/puppet/state/state.yaml
notice: Finished catalog run
in
2.86
seconds
[root@agent3 ~]# cat /etc/puppet/puppet.conf
### config by puppet ###
[main]
logdir = /
var
/log/puppet
rundir = /
var
/run/puppet
ssldir = $
var
dir/ssl
[agent]
classfile = $
var
dir/classes.txt
localconfig = $
var
dir/localconfig
server = puppetmaster.kisspuppet.com
certname = agent3_cert.kisspuppet.com
runinterval =
10
|
1
2
3
4
5
6
7
8
9
|
[root@puppetmaster manifests]# vim service.pp
class
puppet::service{
service {
'puppet'
:
ensure => running, #设置puppet服务一直处于运行状态
hasstatus =>
true
, #通过标准的命令“service server_name status"进行检查状态
hasrestart =>
true
, #设置puppet服务具有标准的restart命令
|