puppet成长日记三 Service资源详细介绍及案例分析

puppet成长日记三 Service资源详细介绍及案例分析

一、系统环境

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 服务能够在配置文件更改的情况下自动重启
2、支持参数
ensure => running|stopped 指定服务的目标状态

enable => true|false 指定服务是否开机自启动,并非对所有均有效

name => "service name",该资源的namevar, 服务的名字,通常就是在/etc/init.d/目录下的名字,默认与title相同

hasstatus => true|false, 指出管理脚本是否支持status参数,puppet用status参数来判断服务是否已经在运行了,如果不支持status参数,puppet利用查找运行进程列表里面是否有服务名来判断服务是否在运行.

hasrestart => true|false, 指出管理脚本是否支持restart参数,如果不支持,就用stop和start实现restart效果.

path => "/etc/rc.d/init.d", 启动脚本的搜索路径,可以用冒号分割多个路径,或者用数组指定

provider => base|daemontools|init,   默认为init


三、资源示例

1、示例一

1.1 实现功能
*要求系统启动后,sshd服务自动启动
*要求通过系统进程方式查看sshd服务运行状态
*要求服务关闭后能够自动重启
*要求配置文件被更改后服务能够执行restart动作
1.2 配置说明

class ssh::service{
        service { $ssh::params::ssh_service_name:
                ensure => running,
                hasstatus => false,
                hasrestart => true,
                enable => true,
                subscribe => Class["ssh::config"],
        }
}
或
class ssh::service{
        service { $ssh::params::ssh_service_name:
                ensure => stopped,
                hasstatus => false,
                hasrestart => true,
                enable => true,
                subscribe => Class["ssh::config"],
        }
}

1.3 客户端agent3测试

[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.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/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378281102'
notice: Finished catalog run in 0.38 seconds
[root@agent3 ~]#
[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.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/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378280861'
notice: /Stage[main]/Ssh::Service/Service[sshd]/ensure: ensure changed 'running' to 'stopped'
notice: Finished catalog run in 0.54 seconds

测试结果:ensure => running, hasstatus => false,的情况下服务如果被关闭是起不来的,ensure => stopped, hasstatus => false,的情况下服务如果是运行的可以被关闭
建议:写SysV脚本放到/etc/init.d目录下来实现


2、示例二
2.1 实现功能

*要求服务配置文件被改动后,服务能够自动reload而不是自动restart(也可以通过exec资源实现)
2.2 配置说明

class ssh::service{
        service { $ssh::params::ssh_service_name:
                ensure => running,
                hasstatus => true,
                hasrestart => true,
                enable => true,
                subscribe => Class["ssh::config"],
#               provider => base|daemontools|init,
                provider => init,
                path => "/etc/rc.d/init.d",
                restart => "/etc/rc.d/init.d/sshd reload",
                start => "/etc/rc.d/init.d/sshd start",
                stop => "/etc/rc.d/init.d/sshd stop",
        }
}
class ssh::config{
        file { $ssh::params::ssh_service_config:
                ensure => present,
                owner => 'root',
                group => 'root',
                mode => 0640,
                source => "puppet:///modules/ssh/etc/ssh/sshd_config",
#               backup => ".$backup_date.bak",
                backup => 'main',
                require => Class["ssh::install"],
                notify => Class["ssh::service"],  #等同于class ssh::service中的subscribe
        }
}

2.3 客户端agent3测试
可修改agent3端/etc/rc.d/init.d/sshd进行测试

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                echo "sshd restart stop-start" >>/tmp/sshd_status
                ;;
        reload)
                reload
                echo "sshd reload" >>/tmp/sshd_status
                ;;

测试部分:

[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.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/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378279503'
notice: /File[/etc/ssh/sshd_config]/content:
--- /etc/ssh/sshd_config    2013-09-04 15:27:22.177863699 +0800
+++ /tmp/puppet-file20130904-19622-yy8g9o-0 2013-09-04 15:29:47.791863671 +0800
@@ -5,8 +5,6 @@
 Protocol 2
 #AddressFamily any
                                           
-# HostKey for protocol version 1
-
 # Lifetime and size of ephemeral version 1 server key
 #KeyRegenerationInterval 1h
 #ServerKeyBits 768
info: FileBucket adding {md5}43f5b3f207a1b6fb35e3bd779b83c3f8
info: /File[/etc/ssh/sshd_config]: Filebucketed /etc/ssh/sshd_config to main with sum 43f5b3f207a1b6fb35e3bd779b83c3f8
notice: /File[/etc/ssh/sshd_config]/content: content changed '{md5}43f5b3f207a1b6fb35e3bd779b83c3f8' to '{md5}df197ccd4957217616b62a82b890ed98'
info: /File[/etc/ssh/sshd_config]: Scheduling refresh of Class[Ssh::Service]
info: Class[Ssh::Config]: Scheduling refresh of Service[sshd]
info: Class[Ssh::Service]: Scheduling refresh of Service[sshd]
notice: /Service[sshd]: Triggered 'refresh' from 2 events
notice: Finished catalog run in 0.97 seconds
[root@agent3 ~]# cat /tmp/sshd_status
sshd reload   #可以看到服务是reload而不是restart
[root@agent3 ~]#

欢迎puppet精英加入自动化运维管理群 296934942

你可能感兴趣的:(puppet,service资源,puppet学习,puppet笔记)