• Puppet基础篇7-编写第一个完整测试模块puppet
  • 工欲善其事必先利其器
    • 1、创建puppet模块目录结构
    • 2、创建puppet配置文件
    • 3、编写puppet模块配置文件

    Puppet基础篇7-编写第一个完整测试模块puppet

    工欲善其事必先利其器

    将Puppet部署到生产中第一个要编写的模块就是puppet本身,虽然puppet可以运行其它所有模块完成各自的部署,但是puppet一旦出问题,那么一切都会停止工作。当然除了puppet自身模块外,还需要保证网络的通畅以及其它你附加的环境等等。

    之前编写过简单的motd模块,大致了解了一些模块的结构以及简单的pp语法,接下来我们进行详细的讲解。
    那么编写一个完整的puppet模块应该考虑哪些因素呢?

    • puppet及附属依赖包是否已经安装OK?

    • puppet配置文件是否正确?

    • puppet服务是否正常运行?

    • 在更新puppet配置文件的情况下,是否能够主动让puppet服务重启或者reload?

    • puppet安装包是否能够自动升级到指定版本?

    接下来以agent1和agent3为例进行讲解

    1、创建puppet模块目录结构

    1. [root@puppetmaster ~]# cd /etc/puppet/modules/
    2. [root@puppetmaster modules]# mkdir puppet
    3. [root@puppetmaster modules]# cd puppet/
    4. [root@puppetmaster puppet]# mkdir files manifests templates #创建模块目录结构
    5. [root@puppetmaster puppet]# tree ../puppet
    6. ../puppet
    7. ├── files #存放下载的文件
    8. ├── manifests #存放puppet配置
    9. └── templates #存放配置模板,方便pp文件引用
    10. 3 directories, 0 files
    11. [root@puppetmaster puppet]#

    2、创建puppet配置文件

    1. [root@puppetmaster puppet]# cd manifests/
    2. [root@puppetmaster manifests]# touch init.pp config.pp install.pp service.pp params.pp
    3. [root@puppetmaster manifests]# tree ../
    4. ../
    5. ├── files
    6. ├── manifests
    7. ├── config.pp #管理puppet配置
    8. ├── init.pp #管理模块所有pp文件配置
    9. ├── install.pp #管理puppet安装
    10. ├── params.pp #管理模块中变量以及一些判断
    11. └── service.pp #管理puppet服务
    12. └── templates
    13. 3 directories, 5 files

    3、编写puppet模块配置文件

    整个过程应该是这样,首先应该安装puppet(install.pp),然后配置puppet(config.pp),最后启动puppet服务(service.pp)

    注意: 接下来的过程不是一步到位的,是一个循序渐进的过程,一步步指导直到完成一个比较完整的模块。

    3.1、编写安装配置文件install.pp

    通过package资源实现,更多有关package的语法及案例请访问http://kisspuppet.com/2013/11/11/package/

    需要注意的是:class名称要和创建的模块名保持一致,名称为puppet,由于在整个配置文件中init.pp为起始配置文件,包含的都应该是子配置文件,所有应该写成“class主类名称::class子类名称”,而class子类名称需要和创建的pp文件名保持一致,比如puppet::install,那么创建的子类名称就应该是install.pp

    3.1.1、编写不具备判断条件的配置文件

    节点安装puppet主要还依赖于facter

    1. [root@puppetmaster manifests]# vim install.pp
    2. class puppet::install{ #一个类包含两个子类
    3. include puppet::puppet_install,puppet::facter_install
    4. }
    5. class puppet::puppet_install{
    6. package { 'puppet':
    7. ensure => installed, #要求处于被安装状态
    8. }
    9. }
    10. class puppet::facter_install{
    11. package { 'facter':
    12. ensure => installed,
    13. }
    14. }

    也可以用以下两种写法

    1. [root@puppetmaster manifests]# vim install.pp
    2. class puppet::install{ #一个类包含两个资源
    3. package { 'puppet':
    4. ensure => installed,
    5. }
    6. package { 'facter':
    7. ensure => installed,
    8. }
    9. }
    10. [root@puppetmaster manifests]# vim install.pp
    11. class puppet::install{
    12. package { ['puppet','facter']: #采用数组的形式
    13. ensure => installed,
    14. }
    15. }

    3.1.2、编写具备判断系统版本条件的模块

    存在这样一种情况,在我的yum源中有很多puppet版本,而我只希望所有节点只安装我指定的版本,比如2.7.25,那么如何设置呢?其次,还应该考虑一种情况,节点的系统版本可能会不一样,比如有RHEL5、RHEL6等,那么如何让puppet模块自己去判断呢?

    通过以下facter进行判断

    1. [root@agent1 ~]# facter | grep operatingsystemmajrelease
    2. operatingsystemmajrelease => 5
    3. [root@agent3 ~]# facter | grep operatingsystemmajrelease
    4. operatingsystemmajrelease => 6

    应该是以下写法比较合理

    1. [root@puppetmaster manifests]# vim install.pp
    2. class puppet::install{
    3. include puppet::puppet_install,puppet::facter_install
    4. }
    5. class puppet::puppet_install{
    6. package { 'puppet':
    7. ensure => $operatingsystemmajrelease ?{ #判断系统版本
    8. 5 => '2.7.25-1.el5',
    9. 6 => '2.7.25-1.el6',
    10. }
    11. }
    12. }
    13. class puppet::facter_install{
    14. package { 'facter':
    15. ensure => $operatingsystemmajrelease ?{
    16. 5 => '1.7.5-1.el5',
    17. 6 => '1.7.5-1.el6',
    18. }
    19. }
    20. }

    3.1.3 添加子类到init.pp中

    1. [root@puppetmaster manifests]# vim init.pp
    2. class puppet{
    3. include puppet::install
    4. }

    3.1.4 应用到puppet主配置文件site.pp中的节点上

    1. [root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
    2. $puppetmaster = 'puppetmaster.kisspuppet.com'
    3. node 'puppetmaster_cert.kisspuppet.com'{
    4. include motd,puppet
    5. }
    6. node 'agent1_cert.kisspuppet.com'{
    7. include motd,puppet
    8. }
    9. node 'agent2_cert.kisspuppet.com'{
    10. include motd,puppet
    11. }
    12. node 'agent3_cert.kisspuppet.com'{
    13. include motd,puppet
    14. }

    也可以是以下写法

    1. [root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
    2. $puppetmaster = 'puppetmaster.kisspuppet.com'
    3. class environments{
    4. include motd,puppet
    5. }
    6. node 'puppetmaster_cert.kisspuppet.com'{
    7. include environments
    8. }
    9. node 'agent1_cert.kisspuppet.com'{
    10. include environments
    11. }
    12. node 'agent2_cert.kisspuppet.com'{
    13. include environments
    14. }
    15. node 'agent3_cert.kisspuppet.com'{
    16. include environments
    17. }

    如何所有节点都使用相同的模块,也可以是以下写法

    1. [root@puppetmaster ~]# vim /etc/puppet/manifests/site.pp
    2. $puppetmaster = 'puppetmaster.kisspuppet.com'
    3. class environments{
    4. include motd,puppet
    5. }
    6. node default{
    7. include environments
    8. }

    3.1.5、进行简单的测试

    降低facter版本为1.7.3

    1. [root@agent1 ~]# rpm -e facter --nodeps
    2. [root@agent1 ~]# rpm -ivh facter-1.7.3-1.el5.x86_64.rpm
    3. warning: facter-1.7.3-1.el5.x86_64.rpm: Header V3 RSA/SHA1 signature: NOKEY, key ID 4bd6ec30
    4. Preparing... ########################################### [100%]
    5. 1:facter ########################################### [100%]
    6. [root@agent1 ~]# facter --version
    7. 1.7.3

    通过—noop进行尝试性测试,可以看到节点变化情况,但是不进行更改,这也是puppet强大的地方之一

    1. [root@agent1 ~]# puppet agent -t --noop
    2. info: Caching catalog for agent1_cert.kisspuppet.com
    3. info: Applying configuration version '1394794815'
    4. notice: /Stage[main]/Puppet::Facter_install/Package[facter]/ensure: current_value 1.7.3-1.el5, should be 1.7.5-1.el5 (noop)
    5. notice: Class[Puppet::Facter_install]: Would have triggered 'refresh' from 1 events
    6. notice: Stage[main]: Would have triggered 'refresh' from 1 events
    7. notice: Finished catalog run in 0.23 seconds
    8. [root@agent1 ~]# facter --version
    9. 1.7.3

    强制执行,可以看到管理端的facter版本变成了puppet模块中指定的版本1.7.5,这其实也说明了rpm包升级的方法!

    1. [root@agent1 ~]# puppet agent -t
    2. info: Caching catalog for agent1_cert.kisspuppet.com
    3. info: Applying configuration version '1394794815'
    4. notice: /Stage[main]/Puppet::Facter_install/Package[facter]/ensure: ensure changed '1.7.3-1.el5' to '1.7.5-1.el5'
    5. notice: Finished catalog run in 6.27 seconds
    6. [root@agent1 ~]# facter --version
    7. 1.7.5

    整个过程是这样,节点同步puppetmaster端后发现facter版本号不对,根据系统类型马上调用底层的安装工具yum(其它系统如suse会调用zypper等)进行安装,整个过程是透明的,而这正是puppet所呈现的强大功能之二。

    3.2、编写配置文件config.pp

    通过file资源实现,更多有关file资源的配置及案例请访问http://kisspuppet.com/2013/11/14/file/

    3.2.1、我们暂时只配置puppet.conf文件

    1. [root@puppetmaster manifests]# vim config.pp
    2. class puppet::config{
    3. file { '/etc/puppet/puppet.conf': #节点文件存放的路径
    4. ensure => present, #要求存在
    5. content => template('puppet/puppet.conf.erb'), #要求根据模板生成,路径写法为相对路径(templates目录隐藏掉)
    6. owner => 'root', #要求文件属主为root
    7. group => 'root', #要求文件属组为root
    8. mode => '0644', #要求文件权限为644
    9. require => Class['puppet::install'], #要求这个文件在配置之前先正确运行install.pp文件,也就是说要求puppet的包应当处于安装状态
    10. }
    11. }

    3.2.2、编写puppet.conf.erb模板

    puppet的erb模板的存在是为了解决每个节点单独配置一个文件的问题,因为erb模板可以引用fact变量,变量的内容会根据节点系统的不同而变化。

    以下为其中一个节点目前的puppet.conf配置文件,我们先找出会变化的内容

    1. [root@agent1 ~]# vim /etc/puppet/puppet.conf
    2. [main]
    3. logdir = /var/log/puppet
    4. rundir = /var/run/puppet
    5. ssldir = $vardir/ssl
    6. [agent]
    7. classfile = $vardir/classes.txt
    8. localconfig = $vardir/localconfig
    9. server = puppetmaster.kisspuppet.com #变量
    10. certname = agent1_cert.kisspuppet.com #变量
    11. runinterval = 10

    接下来解决这两个变量

    之前我们说过创建params.pp就是为了解决变量问题,我们先用这个解决

    找出fact值具有唯一性的fact,比如hostname

    1. [root@agent1 ~]# facter |grep hostname
    2. hostname => agent1
    3. [root@agent3 ~]# facter |grep hostname
    4. hostname => agent3

    编写params.pp文件,增加certname变量

    1. [root@puppetmaster manifests]# vim params.pp
    2. class puppet::params {
    3. $puppetserver = 'puppetmaster.kisspuppet.com' #增加puppetserver变量指向puppetmaster名称
    4. case $hostname{ #增加certname变量
    5. agent1: {
    6. $certname = 'agent1_cert.kisspuppet.com'
    7. }
    8. agent3: {
    9. $certname = 'agent3_cert.kisspuppet.com'
    10. }
    11. default: { #设置默认不存在的情况下报错
    12. fail("certname is not supported on ${::operatingsystem}")
    13. }
    14. }
    15. }

    注意:这种创建变量的方法在大量节点的情况下显然不是最好的方法,能否通过fact变量实现呢,答案是可以的,可写成以下方式

    1. [root@puppetmaster manifests]# vim params.pp
    2. class puppet::params {
    3. $puppetserver = 'puppetmaster.kisspuppet.com'
    4. $certname = "${::hostname}_cert.kisspuppet.com" #通过fact:hostname实现
    5. case $operatingsystemmajrelease{
    6. 5: {
    7. $puppet_release = '2.7.23-1.el5'
    8. $facter_release = '1.7.3-1.el5'
    9. }
    10. 6: {
    11. $puppet_release = '2.7.23-1.el6'
    12. $facter_release = '1.7.3-1.el6'
    13. }
    14. default: {
    15. fail("Module puppet is not supported on ${::operatingsystem}")
    16. }
    17. }
    18. }

    备注:这里使用的是默认的fact,如果要通过系统没有的fact应当如何实现呢,后面《Puppet扩展篇1-自定义fact结合ENC(hirea)的应用实践》会有介绍

    思考:通过变量,尤其是hostname变量确定certname只能保证在hostname不变的情况下才能保证certname不变,否则节点任意修改hostname就会造成certname变更,前期的认证失效,又需要重新认证。那么有没有什么办法解决在hostname变化的情况下certname不变呢,也就是说不需要再次申请证书呢? 后面《Puppet扩展篇1-自定义fact结合ENC(hirea)的应用实践》会有介绍。

    在模板中引用certname变量
    注意模板存放的位置要和config.pp中引用模板的位置保持一致

    1. [root@puppetmaster manifests]# vim ../templates/puppet.conf.erb
    2. ### config by puppet ###
    3. [main]
    4. logdir = /var/log/puppet
    5. rundir = /var/run/puppet
    6. ssldir = $vardir/ssl
    7. [agent]
    8. classfile = $vardir/classes.txt
    9. localconfig = $vardir/localconfig
    10. server = <%= scope.lookupvar('puppet::params::puppetserver') %> #引用变量puppetserver
    11. certname = <%= scope.lookupvar('puppet::params::certname') %> #引用变量certname
    12. runinterval = 10

    由于config.pp依赖于params.pp中的变量,所以config.pp中应当应用class puppet::params

    3.3.3 确定依赖关系

    1. [root@puppetmaster manifests]# vim config.pp
    2. class puppet::config{
    3. include puppet::params #添加引用关系
    4. file { '/etc/puppet/puppet.conf':
    5. ensure => present,
    6. content => template('puppet/puppet.conf.erb'),
    7. owner => 'root',
    8. group => 'root',
    9. mode => '0644',
    10. require => Class['puppet::install'],
    11. }
    12. }

    init.pp中应当包含class puppet::config

    1. [root@puppetmaster manifests]# vim init.pp
    2. class puppet{
    3. include puppet::install,puppet::config
    4. }

    3.3.4 更新测试

    先进行noop测试

    1. [root@agent1 ~]# puppet agent -t --noop
    2. info: Caching catalog for agent1_cert.kisspuppet.com
    3. info: Applying configuration version '1394797763'
    4. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
    5. --- /etc/puppet/puppet.conf 2014-03-10 08:22:33.000000000 +0800
    6. +++ /tmp/puppet-file20140314-7231-f50ehp-0 2014-03-14 19:49:24.000000000 +0800
    7. @@ -1,3 +1,4 @@
    8. +### config by puppet ### #添加部分
    9. [main]
    10. logdir = /var/log/puppet
    11. rundir = /var/run/puppet
    12. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: current_value {md5}fb17740fd53d8d4dfd6d291788a9bda3, should be {md5}134bae34adddbf30a3fe02ff0eb3c6a6 (noop)
    13. notice: Class[Puppet::Config]: Would have triggered 'refresh' from 1 events
    14. notice: Stage[main]: Would have triggered 'refresh' from 1 events
    15. notice: Finished catalog run in 0.43 seconds

    强制执行更新

    1. [root@agent1 ~]# puppet agent -t
    2. info: Caching catalog for agent1_cert.kisspuppet.com
    3. info: Applying configuration version '1394797763'
    4. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
    5. --- /etc/puppet/puppet.conf 2014-03-10 08:22:33.000000000 +0800
    6. +++ /tmp/puppet-file20140314-7475-mlybgg-0 2014-03-14 19:50:16.000000000 +0800
    7. @@ -1,3 +1,4 @@
    8. +### config by puppet ###
    9. [main]
    10. logdir = /var/log/puppet
    11. rundir = /var/run/puppet
    12. info: FileBucket adding {md5}fb17740fd53d8d4dfd6d291788a9bda3
    13. info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet with sum fb17740fd53d8d4dfd6d291788a9bda3
    14. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed '{md5}fb17740fd53d8d4dfd6d291788a9bda3' to '{md5}134bae34adddbf30a3fe02ff0eb3c6a6'
    15. notice: Finished catalog run in 0.34 seconds
    16. [root@agent1 ~]# cat /etc/puppet/puppet.conf
    17. ### config by puppet ###
    18. [main]
    19. logdir = /var/log/puppet
    20. rundir = /var/run/puppet
    21. ssldir = $vardir/ssl
    22. [agent]
    23. classfile = $vardir/classes.txt
    24. localconfig = $vardir/localconfig
    25. server = puppetmaster.kisspuppet.com #根据预先定的puppetserver变量生成
    26. certname = agent1_cert.kisspuppet.com #根据预先定义的certname变量生成
    27. runinterval = 10
    28. [root@agent3 ~]# puppet agent -t
    29. info: Caching certificate for agent3_cert.kisspuppet.com
    30. info: Caching certificate_revocation_list for ca
    31. info: Caching catalog for agent3_cert.kisspuppet.com
    32. info: Applying configuration version '1394797763'
    33. notice: /Stage[main]/Motd/File[/etc/motd]/content:
    34. --- /etc/motd 2010-01-12 21:28:22.000000000 +0800
    35. +++ /tmp/puppet-file20140314-2786-1wb4mas-0 2014-03-14 19:51:27.589533699 +0800
    36. @@ -0,0 +1,3 @@
    37. +-- --
    38. +--------puppet test---------
    39. +-- --
    40. info: FileBucket adding {md5}d41d8cd98f00b204e9800998ecf8427e
    41. info: /Stage[main]/Motd/File[/etc/motd]: Filebucketed /etc/motd to puppet with sum d41d8cd98f00b204e9800998ecf8427e
    42. notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}87ea3a1af8650395038472457cc7f2b1'
    43. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
    44. --- /etc/puppet/puppet.conf 2014-03-09 01:50:46.112175841 +0800
    45. +++ /tmp/puppet-file20140314-2786-z4e844-0 2014-03-14 19:51:27.719533700 +0800
    46. @@ -1,3 +1,4 @@
    47. +### config by puppet ###
    48. [main]
    49. logdir = /var/log/puppet
    50. rundir = /var/run/puppet
    51. @@ -8,3 +9,5 @@
    52. localconfig = $vardir/localconfig
    53. server = puppetmaster.kisspuppet.com
    54. certname = agent3_cert.kisspuppet.com
    55. + runinterval = 10
    56. +
    57. +
    58. +
    59. +
    60. + = true
    61. info: FileBucket adding {md5}03cbe6d4def560996eeacedfaef229b4
    62. info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet with sum 03cbe6d4def560996eeacedfaef229b4
    63. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed '{md5}03cbe6d4def560996eeacedfaef229b4' to '{md5}4f57479998961563e3306b5d0e02a678'
    64. info: Creating state file /var/lib/puppet/state/state.yaml
    65. notice: Finished catalog run in 2.86 seconds
    66. [root@agent3 ~]# cat /etc/puppet/puppet.conf
    67. ### config by puppet ###
    68. [main]
    69. logdir = /var/log/puppet
    70. rundir = /var/run/puppet
    71. ssldir = $vardir/ssl
    72. [agent]
    73. classfile = $vardir/classes.txt
    74. localconfig = $vardir/localconfig
    75. server = puppetmaster.kisspuppet.com
    76. certname = agent3_cert.kisspuppet.com
    77. runinterval = 10

    3.3、编写配置文件service.pp

    通过service资源实现,更多有关service资源及案例请访问http://kisspuppet.com/2013/11/12/service/

    3.3.1、编写service.pp文件

    1. [root@puppetmaster manifests]# vim service.pp
    2. class puppet::service{
    3. service { 'puppet':
    4. ensure => running, #设置puppet服务一直处于运行状态
    5. hasstatus => true, #通过标准的命令“service server_name status"进行检查状态
    6. hasrestart => true, #设置puppet服务具有标准的restart命令
    7. enable => true, #要求开机自动启动,其实通过chkconfig设置puppet状态为on
    8. }
    9. }

    3.3.2、更新config.pp文件,增加通知服务重启功能

    这个设置完成后,我们再想想我们预先确定的要求是配置在更新后要求puppet服务自动重启,应当做如下设置

    1. [root@puppetmaster manifests]# vim config.pp
    2. class puppet::config{
    3. include puppet::params
    4. file { '/etc/puppet/puppet.conf':
    5. ensure => present,
    6. content => template('puppet/puppet.conf.erb'),
    7. owner => 'root',
    8. group => 'root',
    9. mode => '0644',
    10. require => Class['puppet::install'],
    11. notify => Class['puppet::service'], #配置更新后主动通过puppet服务重启
    12. }
    13. }

    3.3.3、添加class puppet::service到init.pp中

    1. [root@puppetmaster manifests]# vim init.pp
    2. class puppet{
    3. include puppet::install,puppet::config,puppet::service
    4. }

    3.3.4、测试

    测试一:查看是否设置了开机启动,查看puppet服务状态

    1. [root@agent1 ~]# chkconfig puppet off
    2. [root@agent1 ~]# /etc/init.d/puppet status
    3. puppetd is stopped
    4. [root@agent1 ~]# puppet agent -t
    5. info: Caching catalog for agent1_cert.kisspuppet.com
    6. info: Applying configuration version '1394798692'
    7. notice: /Stage[main]/Puppet::Service/Service[puppet]/ensure: ensure changed 'stopped' to 'running'
    8. notice: Finished catalog run in 1.42 seconds
    9. [root@agent1 ~]# chkconfig --list | grep puppet
    10. puppet 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    11. [root@agent1 ~]# /etc/init.d/puppet status
    12. puppetd (pid 8537) is running...

    测试二、查看配置被更改还原后,服务是否会自动重启

    1. [root@agent1 ~]# echo "#add a line" >>/etc/puppet/puppet.conf
    2. [root@agent1 ~]# tailf /var/log/messages
    3. Mar 14 21:18:52 agent1 puppet-agent[10803]: Reopening log files
    4. Mar 14 21:18:52 agent1 puppet-agent[10803]: Starting Puppet client version 2.7.25
    5. Mar 14 21:18:53 agent1 puppet-agent[10803]: Finished catalog run in 0.27 seconds
    6. Mar 14 21:19:05 agent1 puppet-agent[10803]: Finished catalog run in 0.35 seconds
    7. Mar 14 21:19:16 agent1 puppet-agent[10803]: Finished catalog run in 0.71 seconds
    8. Mar 14 21:19:27 agent1 puppet-agent[10803]: Finished catalog run in 0.30 seconds
    9. Mar 14 21:19:38 agent1 puppet-agent[10803]: Finished catalog run in 0.37 seconds
    10. Mar 14 21:19:50 agent1 puppet-agent[10803]: Finished catalog run in 0.42 seconds
    11. Mar 14 21:20:01 agent1 puppet-agent[10803]: Finished catalog run in 0.28 seconds
    12. Mar 14 21:20:12 agent1 puppet-agent[10803]: Finished catalog run in 0.36 seconds
    13. Mar 14 21:20:23 agent1 puppet-agent[10803]: Finished catalog run in 0.27 seconds
    14. Mar 14 21:20:34 agent1 puppet-agent[10803]: (/Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content) content changed '{md5}898865b650b9af4cae1886a894ce656e' to '{md5}8c67cb8c039bb6436556b91f0c6678c4'
    15. Mar 14 21:20:34 agent1 puppet-agent[10803]: Caught TERM; calling stop
    16. Mar 14 21:20:36 agent1 puppet-agent[13068]: Reopening log files
    17. Mar 14 21:20:36 agent1 puppet-agent[13068]: Starting Puppet client version 2.7.25 #重启服务

    3.3.5、服务设置reload动作

    在有些场合,我们仅仅需要在修改配置后,让服务重新reload而不是restart,这又当如何设置呢

    1. [root@puppetmaster manifests]# vim config.pp
    2. class puppet::service{
    3. service { 'puppet':
    4. ensure => running,
    5. hasstatus => true,
    6. hasrestart => true,
    7. enable => true,
    8. provider => init,
    9. path => "/etc/init.d", #设置启动脚本的搜索路径
    10. restart => "/etc/init.d/sshd reload", #将restart改成reload
    11. start => "/etc/init.d/sshd start",
    12. stop => "/etc/init.d/sshd stop",
    13. }
    14. }

    测试可以看出服务并没有停止,而是refresh了

    1. [root@agent1 ~]# echo "#add a line" >>/etc/puppet/puppet.conf
    2. [root@agent1 ~]# tailf /var/log/messages
    3. Mar 14 21:32:03 agent1 puppet-agent[13068]: Finished catalog run in 0.33 seconds
    4. Mar 14 21:32:13 agent1 puppet-agent[13068]: Reparsing /etc/puppet/puppet.conf
    5. Mar 14 21:32:14 agent1 puppet-agent[13068]: (/Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content) content changed '{md5}898865b650b9af4cae1886a894ce656e' to '{md5}8c67cb8c039bb6436556b91f0c6678c4'
    6. Mar 14 21:32:14 agent1 puppet-agent[13068]: (/Service[puppet]) Triggered 'refresh' from 1 events
    7. Mar 14 21:32:14 agent1 puppet-agent[13068]: Finished catalog run in 0.32 seconds
    8. Mar 14 21:32:25 agent1 puppet-agent[13068]: Finished catalog run in 0.25 seconds
    9. Mar 14 21:32:35 agent1 puppet-agent[13068]: Reparsing /etc/puppet/puppet.conf
    10. Mar 14 21:32:36 agent1 puppet-agent[13068]: Finished catalog run in 0.25 seconds

    4、优化代码

    4.1、 将install.pp中的判断语句添加到params.pp中

    1. [root@puppetmaster manifests]# vim params.pp
    2. class puppet::params {
    3. $puppetserver = 'puppetmaster.kisspuppet.com'
    4. case $hostname{
    5. agent1: {
    6. $certname = 'agent1_cert.kisspuppet.com'
    7. }
    8. agent3: {
    9. $certname = 'agent3_cert.kisspuppet.com'
    10. }
    11. default: {
    12. fail("certname is not supported on ${::operatingsystem}")
    13. }
    14. }
    15. case $operatingsystemmajrelease{ #添加系统版本变量
    16. 5: {
    17. $puppet_release = '2.7.23-1.el5'
    18. $facter_release = '1.7.3-1.el5'
    19. }
    20. 6: {
    21. $puppet_release = '2.7.23-1.el6'
    22. $facter_release = '1.7.3-1.el6'
    23. }
    24. default: {
    25. fail("Module puppet is not supported on ${::operatingsystem}")
    26. }
    27. }
    28. }
    29. [root@puppetmaster manifests]# vim install.pp #通过变量引用
    30. class puppet::install{
    31. include puppet::puppet_install,puppet::facter_install
    32. }
    33. class puppet::puppet_install{
    34. package { 'puppet':
    35. ensure => $puppet::params::puppet_release, #puppet里引用变量的方法为“$class::子class::变量”
    36. }
    37. }
    38. class puppet::facter_install{
    39. package { 'facter':
    40. ensure => $puppet::params::facter_release,
    41. }
    42. }

    4.2、测试(略)

    技术源于分享…..

    ssh模块案例详解:http://dreamfire.blog.51cto.com/418026/1257719