Puppet公有资源属性(二十九)


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执行.

相关文章
相关标签/搜索