puppet公有资源的用途:
puppet将资源完成任务与否的结果看为状态,正是这种特性让我们可以通过资源公有属性来建立与资源的关系,当建立了资源关联关系后,前者资源状态未成功时,后者资源可以通过资源共有属性来确认其最终执行结果,这就是公有资源属性的用途.
常用的公有资源:
before/require:定义资源的先后顺序,除此之外还有"->".
require:资源依赖,自身资源依赖哪个资源。
before:自身资源在哪个资源之前使用.
notify/subscribe:通知订阅.
notify:通知,自身发生改变通知remote资源
subcribe:订阅,当远程remote资源改变,自身做刷新操作
示例一:
以安装、启动httpd服务为例,puppet代码如下:
1
2
3
4
5
6
7
8
9
|
[root@sh-web1 ~] # cat httpd.pp
package { "httpd" :
ensure => present,
provider => 'yum' ,
} service { "httpd" :
ensure => running,
enable => true ,
} |
注释:希望的结果是先安装httpd软件包,再启动httpd服务.
运行结果如下:
1
2
3
4
5
6
7
8
9
10
|
[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代码:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~] # cat httpd.pp
package { "httpd" :
ensure => present,
provider => 'yum' ,
} service { "httpd" :
ensure => running,
enable => true ,
require => Package[ 'httpd' ],
} |
运行结果如下:
1
2
3
4
5
6
7
8
9
|
[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代码:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~] # cat httpd.pp
package { "httpd" :
ensure => present,
provider => 'yum' ,
before => Service[ 'httpd' ],
} service { "httpd" :
ensure => running,
enable => true ,
} |
运行的结果如下:
1
2
3
4
5
6
7
8
9
|
[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订阅功能的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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' ,
}
} |
运行结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[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代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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' ,
}
} |
运行结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[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执行.