Puppet service資源介紹(二十五)
service資源
service資源主要對服務做啟動、重啟、關閉,監控進程的狀態等.
service的參數:
service { ‘resource title‘: name => # (namevar) The name of the service to run. This name is... ensure => # Whether a service should be running. Valid... binary => # The path to the daemon. This is only used for... control => # The control variable used to manage services... enable => # Whether a service should be enabled to start at... flags => # Specify a string of flags to pass to the startup hasrestart => # Specify that an init script has a `restart... hasstatus => # Declare whether the service‘s init script has a... manifest => # Specify a command to config a service, or a path path => # The search path for finding init scripts.... pattern => # The pattern to search for in the process table... provider => # The specific backend to use for this `service... restart => # Specify a *restart* command manually. If left... start => # Specify a *start* command manually. Most... status => # Specify a *status* command manually. This... stop => # Specify a *stop* command... # ...plus any applicable metaparameters. }
binary:指定二進制程序的系統路徑,用於那些不支持init的操作系統.如果守護進程沒有自啟動腳本,可以通過此屬性啟動服務.
enable:指定服務在開機的時候是否啟動,可以設置true和false值.
ensure:是否運行服務,running表示運行,stopped表示停止服務.
hasrestart:指出管理腳本是否支持restart值,如果不支持,就用stop值和start值實現restart效果.可以設置為true和false.
hasstatus:指出管理腳本是否支持status值.puppet用status值來判斷服務是否已經在運行,如果系統不支持status值,puppet可以利用查找運行進程列表裏面是否有服務名來判斷服務是否在運行.可以設置為true和false.
name:守護進程的名字,如果記得不準確可以在/etc/init.d目錄下面查找.
path:啟動腳本的搜索路徑,可以用冒號分割多個路徑,或者用數組指定.
pattern:設置匹配進程的字符串,當服務停止時,通過進程列表來判斷服務的狀態,主要用於不支持init腳本的系統.
restart:指定用於重啟服務的腳本,否則只能手動先停止該服務再啟動該服務.
start:指定啟動服務的命令,通常init模式的管理腳本都支持,不需要手工指定.
status:指定status命令,如果不指定,就從近乎才呢過列表查詢該服務.
stop:指定停止服務的腳本.
provider:操作系統支持的init模式的管理腳本,支持base|daemontools|init,默認為init.
示例一:
啟動httpd服務,設置開機自啟動.
註意:啟動服務的前提是httpd軟件包已經安裝.
service {"httpd": ensure => running, enable => true, }
[root@sh-web1 ~]# /etc/init.d/httpd status httpd (pid 121592) is running...
[root@sh-web1 ~]# chkconfig --list | grep httpd httpd 0:off1:off2:on3:on4:on5:on6:off
示例二:
之前文章寫過怎麽將源碼包封裝成rpm包,下面是封裝好的mysql rpm包,測試puppet代碼.
[root@sh-web1 ~]# rpm -ivh mysql-5.6.29-1.x86_64.rpm Preparing... ########################################### [100%] 1:mysql ########################################### [100%] `/usr/include/mysql/include‘ -> `/data/mysql/include‘ [root@sh-web1 ~]# source /etc/profile
安裝目錄在/data目錄下.
[root@sh-web1 ~]# ls /data/ mysql mysqldata
啟動腳本是/data的腳本cp一份放到/etc/init.d目錄下.
下面是puppet啟動mysql的代碼.
[root@sh-web1 ~]# cat mysqld.pp service {"mysqld": ensure => running, enable => true, hasrestart => true, hasstatus => true, provider => init, path => "/etc/init.d", restart => "/etc/init.d/mysqld reload", start => "/etc/init.d/mysqld start", stop => "/etc/init.d/mysqld stop", }
運行結果:
[root@sh-web1 ~]# puppet apply mysqld.pp Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.06 seconds Notice: /Stage[main]/Main/Service[mysqld]/ensure: ensure changed ‘stopped‘ to ‘running‘ Notice: Finished catalog run in 2.44 seconds
查看結果是否啟動.
[root@sh-web1 ~]# /etc/init.d/mysqld status MySQL running (123039) [ OK ]
本文出自 “青衫解衣” 博客,請務必保留此出處http://215687833.blog.51cto.com/6724358/1973746
Puppet service資源介紹(二十五)