1. 程式人生 > >Puppet公有資源屬性(二十九)

Puppet公有資源屬性(二十九)

公有 puppet 資源屬性


puppet公有資源的用途:

puppet將資源完成任務與否的結果看為狀態,正是這種特性讓我們可以通過資源公有屬性來建立與資源的關系,當建立了資源關聯關系後,前者資源狀態未成功時,後者資源可以通過資源共有屬性來確認其最終執行結果,這就是公有資源屬性的用途.


常用的公有資源:

before/require:定義資源的先後順序,除此之外還有"->".

require:資源依賴,自身資源依賴哪個資源。

before:自身資源在哪個資源之前使用.

notify/subscribe:通知訂閱.

notify:通知,自身發生改變通知remote資源

subcribe:訂閱,當遠程remote資源改變,自身做刷新操作


示例一:

以安裝、啟動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執行.




本文出自 “青衫解衣” 博客,請務必保留此出處http://215687833.blog.51cto.com/6724358/1977355

Puppet公有資源屬性(二十九)