puppet自动化运维之service资源
作用:
①.管理服务的状态;
②.服务能够在配置文件更改的情况下自动重启。
格式:
1、service资源常用属性
service {'资源标题':
binary
enable
ensure
hasrestart
hasstatus
name
path
pattern
restart
start
status
stop
provider
}
◆enable:指定服务在开机的时候是否启动,可以设置true和false。
◆ensure:是否运行服务,running表示运行,stopped表示停止服务。
◆name:守护进程的名字。
◆path:启动脚本搜索路径。
◆hasrestart:指出管理脚本是否支持restart参数,如果不支持,就用stop和start实现restart效果。
◆hasstatus:指出管理脚本是否支持status参数,puppet用status参数来判断服务是否已经在运行了,如果不支持status参数,puppet利用查找运行进程列表里面是否有服务名来判断服务是否在运行。
◆provider:默认为init。
service {"title":#服务名,通常就是在/etc/init.d/目录下的名字
ensure => {running|stopped},#当前service的状态
enable => {true|false},#service是否开机启动,chkconfig
[status|start|stop|restart] => "cmd", #指定要执行的完整命令,当且仅当,启动脚本不在/etc/init.d/下的
path => "目录",#启动脚本的搜索路径,可以用冒号分割多个路径,或者用数组指定
hasrestart => {true|false},#是否支持restart参数,如果不支持,就用stop和start实现restart效果.
hasstatus => {true|false},#是从命令行status查询还是从进程表(有没有该进程)中,查询service的状态
provider => base|daemontools|init;#默认为init
}
实例:
#vsftpd,启动且开机自起
vi /etc/puppet/manifest/test.pp
service {"vsftpd":
ensure => running,
enable => true;
}
#检查
[root@client~]# /etc/init.d/vsftpd status
vsftpd isstopped
[root@client~]# chkconfig --list vsftpd
vsftpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@client~]#
[root@client~]#puppet agent --test -v --server master.perofu.com
info: Caching catalog for client.perofu.com
info: Applying configuration version '1395069819'
notice: /Stage[main]//Service[vsftpd]/ensure: ensurechanged 'stopped' to 'running'
notice: Finished catalog run in 0.38 seconds
[root@client~]#
[root@client~]# /etc/init.d/vsftpd status
vsftpd (pid 20118) isrunning...
[root@client~]# chkconfig --list vsftpd
vsftpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
#源码
service {"httpd":
ensure => running,
#restart => "/usr/local/apache2/bin/apachectl restart",
hasrestart => "true",
subscribe => File["/etc/httpd/httpd.conf"]
}
subscribe
检测某个资源,当它发生变化时,该资源会重新加载,如:
class nagios {
file { “/etc/nagios/nagios.conf”:
source => “puppet://server/module/nagios.conf”,
alias => nagconf # just to make things easier for me
}
service { nagios:
ensure => running,
subscribe => File["nagconf"]
}
}
当检测到文件nagconf被修改时,服务nagios会相应的更新。需要注意的是,目前支持subscribe的资源只有exec,service和mount。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。