puppet公有資源的用途:linux
puppet將資源完成任務與否的結果看爲狀態,正是這種特性讓咱們能夠經過資源公有屬性來創建與資源的關係,當創建了資源關聯關係後,前者資源狀態未成功時,後者資源能夠經過資源共有屬性來確認其最終執行結果,這就是公有資源屬性的用途.web
經常使用的公有資源:centos
before/require:定義資源的前後順序,除此以外還有"->".bash
require:資源依賴,自身資源依賴哪一個資源。app
before:自身資源在哪一個資源以前使用.dom
notify/subscribe:通知訂閱.ide
notify:通知,自身發生改變通知remote資源ui
subcribe:訂閱,當遠程remote資源改變,自身作刷新操做spa
示例一:rest
以安裝、啓動httpd服務爲例,puppet代碼以下:
[root@sh-web1 ~]# cat httpd.pp package {"httpd": ensure => present, provider => 'yum', } service {"httpd": ensure => running, enable => true, }
註釋:但願的結果是先安裝httpd軟件包,再啓動httpd服務.
運行結果以下:
[root@sh-web1 ~]# puppet apply httpd.pp Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.04 seconds Error: Could not start Service[httpd]: Execution of '/sbin/service httpd start' returned 1: httpd: unrecognized service Error: /Stage[main]/Main/Service[httpd]/ensure: change from stopped to running failed: Could not start Service[httpd]: Execution of '/sbin/service httpd start' returned 1: httpd: unrecognized service Notice: /Stage[main]/Main/Package[httpd]/ensure: created Notice: Finished catalog run in 3.71 seconds [root@sh-web1 ~]# rpm -qa httpd httpd-2.2.15-60.el6.centos.6.x86_64 [root@sh-web1 ~]# /etc/init.d/httpd status httpd is stopped
註釋:能夠看到puppet代碼運行過程當中先啓動了httpd服務,而後再安裝的httpd包,順序並不是所但願的那樣運行.
---------------------------------------------------------------------------------------------------
示例二:
引入資源的公有屬性require和before屬性.
引入require的puppet代碼:
[root@sh-web1 ~]# cat httpd.pp package {"httpd": ensure => present, provider => 'yum', } service {"httpd": ensure => running, enable => true, require => Package['httpd'], }
運行結果以下:
[root@sh-web1 ~]# puppet apply httpd.pp Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.04 seconds Notice: /Stage[main]/Main/Package[httpd]/ensure: created Notice: /Stage[main]/Main/Service[httpd]/ensure: ensure changed 'stopped' to 'running' Notice: Finished catalog run in 1.83 seconds [root@sh-web1 ~]# rpm -qa httpd httpd-2.2.15-60.el6.centos.6.x86_64 [root@sh-web1 ~]# /etc/init.d/httpd status httpd (pid 14413) is running...
---------------------------------------------------------------------------------------------
引入before資源的puppet代碼:
[root@sh-web1 ~]# cat httpd.pp package {"httpd": ensure => present, provider => 'yum', before => Service['httpd'], } service {"httpd": ensure => running, enable => true, }
運行的結果以下:
[root@sh-web1 ~]# puppet apply httpd.pp Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.05 seconds Notice: /Stage[main]/Main/Package[httpd]/ensure: created Notice: /Stage[main]/Main/Service[httpd]/ensure: ensure changed 'stopped' to 'running' Notice: Finished catalog run in 2.14 seconds [root@sh-web1 ~]# rpm -qa httpd httpd-2.2.15-60.el6.centos.6.x86_64 [root@sh-web1 ~]# /etc/init.d/httpd status httpd (pid 14665) is running...
-------------------------------------------------------------------------------------
示例三:
以前的文檔寫過exec示例時有subcribe參數,恰好拿來使用.
引入notify/subscribe公有資源:
改變haproxy的timeout的值,使用puppet的subcribe訂閱功能的代碼以下:
class haproxy::service { file {'/etc/haproxy/haproxy.cfg': ensure => present, source => 'puppet:///modules/haproxy/haproxy.cfg', } exec {"/etc/init.d/haproxy restart": path => ['/sbin','/bin',], command => 'service haproxy restart', subscribe => File['/etc/haproxy/haproxy.cfg'], timeout => '5', refreshonly => true, tries => '2', try_sleep => '3', } }
運行結果以下:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1509023682' Notice: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]/content: --- /etc/haproxy/haproxy.cfg2017-10-19 00:26:55.535720143 +0800 +++ /tmp/puppet-file20171026-15120-3hpwyh-02017-10-26 14:04:57.471321465 +0800 @@ -50,7 +50,7 @@ retries 3 timeout http-request 10s timeout queue 1m - timeout connect 10s + timeout connect 20s timeout client 1m timeout server 1m timeout http-keep-alive 10s Info: Computing checksum on file /etc/haproxy/haproxy.cfg Info: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]: Filebucketed /etc/haproxy/haproxy.cfg to puppet with sum 034aa86fec81774e5f81c691df0d92a3 Notice: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]/content: content changed '{md5}034aa86fec81774e5f81c691df0d92a3' to '{md5}d94dac291f5d754ea13c2799f0c065c7' Info: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]: Scheduling refresh of Exec[/etc/init.d/haproxy restart] Notice: /Stage[main]/Haproxy::Service/Exec[/etc/init.d/haproxy restart]: Triggered 'refresh' from 1 events Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: Finished catalog run in 0.57 seconds
註釋:當haprxoy的/etc/haproxy/haproxy.cfg發生改變是就會重啓服務.
---------------------------------------------------------------------------------------------
引入puppet公有資源屬性notify主動通知功能的puppet代碼:
class haproxy::service { file {'/etc/haproxy/haproxy.cfg': ensure => present, source => 'puppet:///modules/haproxy/haproxy.cfg', notify => Exec['/etc/init.d/haproxy restart'], } exec {"/etc/init.d/haproxy restart": path => ['/sbin','/bin',], command => 'service haproxy restart', timeout => '5', refreshonly => true, tries => '2', try_sleep => '3', } }
運行結果以下:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1509027044' Notice: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]/content: --- /etc/haproxy/haproxy.cfg2017-10-26 14:04:57.538321466 +0800 +++ /tmp/puppet-file20171026-15376-1fnh0f2-02017-10-26 14:10:44.544847207 +0800 @@ -50,7 +50,7 @@ retries 3 timeout http-request 10s timeout queue 1m - timeout connect 20s + timeout connect 30s timeout client 1m timeout server 1m timeout http-keep-alive 10s Info: Computing checksum on file /etc/haproxy/haproxy.cfg Info: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]: Filebucketed /etc/haproxy/haproxy.cfg to puppet with sum d94dac291f5d754ea13c2799f0c065c7 Notice: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]/content: content changed '{md5}d94dac291f5d754ea13c2799f0c065c7' to '{md5}d5baea01fcfcb635dec5737a4ae67a3f' Info: /Stage[main]/Haproxy::Service/File[/etc/haproxy/haproxy.cfg]: Scheduling refresh of Exec[/etc/init.d/haproxy restart] Notice: /Stage[main]/Haproxy::Service/Exec[/etc/init.d/haproxy restart]: Triggered 'refresh' from 1 events Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: Finished catalog run in 0.65 seconds
註釋:/etc/haproxy/haproxy.cfg文件發生變化,puppet主動去推的exec執行.