设为首页 收藏本站
查看: 912|回复: 0

[经验分享] Puppet公有资源属性(二十九)

[复制链接]

尚未签到

发表于 2017-10-31 16:20:47 | 显示全部楼层 |阅读模式
puppet公有资源的用途:
        puppet将资源完成任务与否的结果看为状态,正是这种特性让我们可以通过资源公有属性来建立与资源的关系,当建立了资源关联关系后,前者资源状态未成功时,后者资源可以通过资源共有属性来确认其最终执行结果,这就是公有资源属性的用途.

常用的公有资源:
before/require:定义资源的先后顺序,除此之外还有"->".
        require:资源依赖,自身资源依赖哪个资源。
        before:自身资源在哪个资源之前使用.
notify/subscribe:通知订阅.
        notify:通知,自身发生改变通知remote资源
        subcribe:订阅,当远程remote资源改变,自身做刷新操作

示例一:
以安装、启动httpd服务为例,puppet代码如下:
1
2
3
4
5
6
7
8
9
[iyunv@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
[iyunv@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
[iyunv@sh-web1 ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.6.x86_64
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@sh-web1 ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.6.x86_64
[iyunv@sh-web1 ~]# /etc/init.d/httpd status
httpd (pid  14413) is running...



---------------------------------------------------------------------------------------------
引入before资源的puppet代码:
1
2
3
4
5
6
7
8
9
10
[iyunv@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
[iyunv@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
[iyunv@sh-web1 ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.6.x86_64
[iyunv@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
[iyunv@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
[iyunv@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执行.




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406348-1-1.html 上篇帖子: Puppet notify资源参数(二十八) 下篇帖子: puppet权威指南(不用金币)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表