puppet学习与精通之Package资源详细介绍及案例分析

时间:2022-11-23 00:40:15
一、系统环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 、puppet服务端
Release:RHEL6. 4
HOSTNAME: puppetserver.rsyslog.org
TCP/IP:  172.16 . 200.100 / 24
Packages: 
puppet-server- 2.7 . 21 - 1 .el6.noarch
mcollective-client- 2.2 . 4
activemq- 5.5 . 0
2 、puppet节点
Release: RHEL5. 8
HOSTNAME: agent1.rsyslog.org
TCP/IP:  172.16 . 200.101 / 24
Packages:
puppet- 2.7 . 21 - 1 .el5
mcollective- 2.2 . 4 - 1 .el5
3 、puppet节点
Release: RHEL6. 4
HOSTNAME: agent3.rsyslog.org
TCP/IP:  172.16 . 200.103 / 24
Packages:
puppet- 2.7 . 21 - 1 .el6
mcollective- 2.2 . 4 - 1 .el6

二、资源介绍

1、实现功能
1.1 管理那些软件包被安装,那些软件包被卸载
1.2 管理软件包是否更新
1.3 要求系统配置yum源(RedHat系统)、zypper源(Suse系统)等等
1.4 有关不同操作系统支持类型请查阅http://docs.puppetlabs.com/references/stable/type.html#package

Provider holdable install options installable purgeable uninstall options uninstallable upgradeable versionable
aix

X

X X X
appdmg

X




apple

X




apt X
X X
X X X
aptitude X
X X
X X X
aptrpm

X X
X X X
blastwave

X

X X
dpkg X
X X
X X
fink X
X X
X X X
freebsd

X

X

gem

X

X X X
hpux

X

X

macports

X

X X X
msi
X X
X X

nim

X

X X X
openbsd

X

X
X
opkg

X

X X
pacman

X

X X
pip

X

X X X
pkg X
X

X X X
pkgdmg

X




pkgin

X

X

pkgutil

X

X X
portage

X

X X X
ports

X

X X
portupgrade

X

X X
rpm

X

X X X
rug

X

X X X
sun
X X

X X
sunfreeware

X

X X
up2date

X

X X
urpmi

X

X X X
windows
X X
X X

yum

X X
X X X
zypper

X

X X X


2、支持参数
2.1 ensure => {present|installed|absent|latest|purged|"version"}, 指定文件的目标状态
=> present|installed, 检查文件是否存在,不存在则新建之
=> absent, 无其他软件依赖,可删除,否则会报错。
=> latest, 检查文件是否为最新版本,否则升级为最新版本
=> purged, 删除该包包括所有依赖的包,有风险慎用
=> "2.7.21-1", 指定某一个版本处于安装状态


2.2 name => package name, 包的名字,默认与title相同可不写

2.3 provider => rpm, 要求通过rpm命令安装包,需要通过source指定安装那些包


三、资源示例
1、示例一
1.1 实现功能

*判断系统为RedHat的情况下安装mysql-server和mysql,系统为SLES的情况下安装mysql和mysql-client,其他系统安装mysql-server和mysql包
*要求安装的包一直处于安装状态
1.2 配置说明

1
2
3
4
5
6
7
8
9
10
class  mysql::install{
     package  "mysql" :
                name => $operatingsystem ?
                        {
                        RedHat => [ "mysql-server" , "mysql" ],
                        SLES => [ "mysql" , "mysql-client" ],
                        default  => [ "mysql-server" , "mysql" ],
                },
                ensure => installed,
     }

1.3 客户端agent1测试

1
2
3
4
5
6
7
8
9
10
11
[root@agent1 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts  in  / var /lib/puppet/lib/facter/my_apply2.rb
info: Loading facts  in  / var /lib/puppet/lib/facter/my_apply1.rb
info: Loading facts  in  / var /lib/puppet/lib/facter/my_apply3.rb
info: Loading facts  in  / var /lib/puppet/lib/facter/backup_date.rb
info: Caching catalog  for  agent1.rsyslog.org
info: Applying configuration version  '1378200479'
notice: /Stage[main]/Mysql::Install/Package[mysql]/ensure: created
notice: /Stage[main]/Mysql::Service/Service[mysqld]/ensure: ensure changed  'stopped'  to  'running'
notice: Finished catalog run  in  9.68  seconds

2、示例二
2.1 实现功能

*要求安装的autofs包版本为5.0.1-0.rc2.163.el5
2.2 配置说明

1
2
3
4
5
class  autofs::install{
         package  "autofs" :
                 ensure =>  "5.0.1-0.rc2.163.el5" ,
         }
}

2.3 客户端agent2测试

1
2
3
4
[root@agent1 ~]# puppet agent --test
info: Caching catalog  for  agent1.rsyslog.org
info: Applying configuration version  '1378201631'
notice: /Stage[main]/Autofs::Install/Package[autofs]/ensure: created

3、示例三
3.1 实现功能

*要求安装的postfix包版本更新至最新版本
3.2 配置说明

1
2
3
4
5
class  postfix::install{
         package  "postfix" :
                 ensure => latest,
         }
}

2.3 客户端agent2测试

1
2
3
4
5
6
7
8
9
10
[root@agent1 rhel5]# rpm -qa postfix
postfix- 2.3 . 3 - 2.3 .el5_6
[root@agent1 rhel5]# puppet agent --test
info: Caching catalog  for  agent1.rsyslog.org
info: Applying configuration version  '1378202947'
notice: /Stage[main]/Postfix::Install/Package[postfix]/ensure: ensure changed  '2.3.3-2.3.el5_6'  to  '2:2.5.17-1.rhel5'
notice: Finished catalog run  in  6.42  seconds
[root@agent1 rhel5]# rpm -qa postfix
postfix- 2.5 . 17 - 1 .rhel5
[root@agent1 rhel5]#

4、示例四
4.1 实现功能

*要求postfix包不允许被安装
4.2 配置说明

1
2
3
4
5
class  postfix::install{
         package  "postfix" :
                 ensure => absent,
         }
}

4.3 客户端agent2测试

1
2
3
4
5
6
7
8
[root@agent1 rhel5]# rpm -qa postfix
postfix- 2.5 . 17 - 1 .rhel5
[root@agent1 rhel5]# puppet agent --test
info: Caching catalog  for  agent1.rsyslog.org
info: Applying configuration version  '1378203359'
notice: /Stage[main]/Postfix::Install/Package[postfix]/ensure: removed
notice: Finished catalog run  in  1.00  seconds
[root@agent1 rhel5]# rpm -qa postfix

5、示例五
5.1 实现功能

*要求通过rpm命令安装本地rpm包/tmp/rhel5/postfix-2.5.17-1.rhel5.x86_64.rpm
5.2 配置说明

1
2
3
4
5
6
7
class  postfix::install{
         package  "postfix" :
                 ensure => present,
                 provider => rpm,
                 source =>  "/tmp/rhel5/postfix-2.5.17-1.rhel5.x86_64.rpm" ,
         }
}

5.3 客户端agent1测试

1
2
3
4
5
6
7
8
[root@agent1 rhel5]# rpm -qa postfix
[root@agent1 rhel5]# puppet agent --test --verbose
info: Caching catalog  for  agent1.rsyslog.org
info: Applying configuration version  '1378204203'
notice: /Stage[main]/Postfix::Install/Package[postfix]/ensure: created
notice: Finished catalog run  in  3.43  seconds
[root@agent1 rhel5]# rpm -qa postfix
postfix- 2.5 . 17 - 1 .rhel5