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

[经验分享] Learning Puppet — Resource Ordering

[复制链接]

尚未签到

发表于 2015-9-16 10:32:49 | 显示全部楼层 |阅读模式
Learning Puppet — Resource Ordering
  Learn about dependencies and refresh events, manage the relationships between resources, and discover the fundamental Puppet design pattern.
Disorder
  Let’s look back on one of our manifests from the last page:
  [iyunv@centos manifests]# vim site.pp
[iyunv@centos manifests]# cat site.pp
import 'order.pp'
[iyunv@centos manifests]# cat order.pp
file { '/tmp/test1':
ensure => present,
content => "hi.",
}
file { '/tmp/test2':
ensure => directory,
mode => 644,
}
file { '/tmp/test3':
ensure => link,
target => '/tmp/test1',
}
  notify {" iam nofitying you":}
notify {" so am I!":}
  [iyunv@yum01 tmp]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1415344839'
Notice: /Stage[main]/Main/File[/tmp/test1]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/test3]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/test2]/ensure: created
Notice:  so am I!
Notice: /Stage[main]/Main/Notify[ so am I!]/message: defined 'message' as ' so am I!'
Notice:  iam nofitying you
Notice: /Stage[main]/Main/Notify[ iam nofitying you]/message: defined 'message' as ' iam nofitying you'
Notice: Finished catalog run in 0.04 seconds
  When we ran this, the resources weren’t synced in the order we wrote them: it went /tmp/test1,/tmp/test3, /tmp/test2, So am I!, and I'm notifying you.
  Like we mentioned in the last chapter, Puppet combines “check the state” and “fix any problems” into a single declaration for each resource. Since each resource is represented by one atomic statement, ordering within a file matters a lot less than it would for an equivalent script. So when dealing with related resources, Puppet has ways to express those relationships.
Metaparameters, Resource References, and Ordering
  Here’s a notify resource that depends on a file resource:
  [iyunv@centos manifests]# cat file.pp
file {'/tmp/test111':
        ensure => present,
        content => "hi. this is a test 111111 file \n",
}
  notify {'/tmp/test111 has already been synced/':
require => File['/tmp/test111'],
}
  [iyunv@yum01 tmp]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1415345569'
Notice: /Stage[main]/Main/File[/tmp/test111]/ensure: created
Notice: /tmp/test111 has already been synced/
Notice: /Stage[main]/Main/Notify[/tmp/test111 has already been synced/]/message: defined 'message' as '/tmp/test111 has already been synced/'
Notice: Finished catalog run in 0.11 seconds
  Each resource type has its own set of attributes, but there’s another set of attributes, calledmetaparameters, which can be used on any resource
  There are four metaparameters that let you arrange resources in order:

  • before  before is used in the earlier resource
  • require  require is used in the later resource
  • notify   send msg to puppet agent
  • subscribe  subscribe is used in the later resource, if change, also sent refresh
  All of them accept a resource reference (or an array of them) as their value.
Before and Require
  before and require make simple dependency relationships, where one resource must be synced before another. before is used in the earlier resource, and lists resources that depend on it; require is used in the later resource, and lists the resources that it depends on.
  These two metaparameters are just different ways of writing the same relationship — our example above could just as easily be written like this:
  [iyunv@centos manifests]# cat file.pp
file {'/tmp/test1':
      ensure  => present,
      content => "Hi.",
      before  => Notify['/tmp/test1 has already been synced.'],
    }
  notify {'/tmp/test1 has already been synced.':}
  [iyunv@yum01 tmp]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1415345911'
Notice: /Stage[main]/Main/File[/tmp/test1]/ensure: created
Notice: /tmp/test1 has already been synced.
Notice: /Stage[main]/Main/Notify[/tmp/test1 has already been synced.]/message: defined 'message' as '/tmp/test1 has already been synced.'
Notice: Finished catalog run in 0.39 seconds
Notify and Subscribe
  A few resource types (service, exec, and mount) can be “refreshed” — that is, told to react to changes in their environment. For a service, this usually means restarting when a config file has been changed; for anexec resource, this could mean running its payload if any user accounts have been changed
  The notify and subscribe metaparameters make dependency relationships the way before and requiredo, but they also make notification relationships. Not only will the earlier resource in the pair get synced first, but if Puppet makes any changes to that resource, it will send a refresh event to the later resource, which will react accordingly.
  An example of a notification relationship:
  [iyunv@centos manifests]# mkdir -p /etc/puppet/modules/ntp/files
  [iyunv@centos manifests]# cp /etc/ntp.conf /etc/puppet/modules/ntp/files
  [iyunv@centos manifests]# vim file.pp
  file { '/etc/ntp.conf':
      ensure => file,
      mode   => 600,
      source => 'puppet:///modules/ntp/ntp.conf',
    }
    service { 'ntpd':
      ensure    => running,
      enable    => true,
      subscribe => File['/etc/ntp.conf'],
    }
  [iyunv@yum01 tmp]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1415347799'
Notice: /Stage[main]/Main/File[/etc/ntp.conf]/content:
--- /etc/ntp.conf2014-11-07 07:56:08.681423545 +0000
+++ /tmp/puppet-file20141107-15367-s2gp6n-02014-11-07 08:19:27.500485582 +0000
@@ -19,9 +19,8 @@

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
- #server ntpv01
server 192.168.1.20
-# server ntpv02
+server 192.168.1.21
  Info: Computing checksum on file /etc/ntp.conf
Info: /Stage[main]/Main/File[/etc/ntp.conf]: Filebucketed /etc/ntp.conf to puppet with sum 4f6db2d5786a7911745abd3113ce02b4
Notice: /Stage[main]/Main/File[/etc/ntp.conf]/content: content changed '{md5}4f6db2d5786a7911745abd3113ce02b4' to '{md5}86f6b5f3e0cbcb2dd9dfcc49bb16e1a9'
Notice: /Stage[main]/Main/File[/etc/ntp.conf]/mode: mode changed '0644' to '0600'
Info: /Stage[main]/Main/File[/etc/ntp.conf]: Scheduling refresh of Service[ntpd]
Info: /Stage[main]/Main/File[/etc/ntp.conf]: Scheduling refresh of Service[ntpd]
Notice: /Stage[main]/Main/Service[ntpd]: Triggered 'refresh' from 2 events
Notice: Finished catalog run in 4.39 seconds
  In this example, the ntpd service will be restarted if Puppet has to edit its config file. (There are two changed parameters, so trggered refresh from 2 events)
  # fileserver.conf
  # Puppet automatically serves PLUGINS and FILES FROM MODULES: anything in
# <module name>/files/<file name> is available to authenticated nodes at
# puppet:///modules/<module name>/<file name>. You do not need to edit this
# file to enable this.
Chaining Arrows
  There’s one last way to declare relationships: chain resource references with the ordering (->) and notification (~>; note the tilde) arrows. Think of them as representing the flow of time: the resource behind the arrow will be synced before the resource the arrow points at.
  This example causes the same dependency as the similar examples above:
  [iyunv@centos manifests]# cat site.pp |grep order1
import 'order1.pp'
  [iyunv@centos manifests]# cat order1.pp
file {'/tmp/test11':
ensure => present,
content => "hi",
}
notify {'after':
message => '/tmp/test11 has already been synced.',
}
  File['/tmp/test11'] -> Notify['after']
  [iyunv@yum01 ~]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1417158189'
Notice: /Stage[main]/Main/File[/tmp/test11]/ensure: created
Notice: /tmp/test11 has already been synced.
Notice: /Stage[main]/Main/Notify[after]/message: defined 'message' as '/tmp/test11 has already been synced.'
Notice: Finished catalog run in 0.89 seconds
  Chaining arrows can take several things as their operands: this example uses resource references, but they can also take resource declarations and resource collectors.
  Since whitespace is freely adjustable in Puppet, and since chaining arrows can go between resource declarations, it’s easy to make a short run of resources be synced in the order they’re written — just put chaining arrows between them:
  [iyunv@centos manifests]# cat order1.pp
file {'/tmp/test11':
ensure => present,
content => "hi",
}
->
notify {'after':
message => '/tmp/test11 has already been synced.',
}
  [iyunv@yum01 ~]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '1417158371'
Notice: /Stage[main]/Main/File[/tmp/test11]/ensure: created
Notice: /tmp/test11 has already been synced.
Notice: /Stage[main]/Main/Notify[after]/message: defined 'message' as '/tmp/test11 has already been synced.'
Notice: Finished catalog run in 1.03 seconds
  Again, this creates the same relationship we’ve seen previously.
Autorequire
  Some of Puppet’s resource types will notice when an instance is related to other resources, and they’ll set up automatic dependencies. The one you’ll use most often is between files and their parent directories: if a given file and its parent directory are both being managed as resources, Puppet will make sure to sync the parent directory before the file. This never creates new resources; it only adds dependencies to resources that are already being managed.
  Don’t sweat much about the details of autorequiring; it’s fairly conservative and should generally do the right thing without getting in your way. If you forget it’s there and make explicit dependencies, your code will still work. Explicit dependencies will also override autorequires, if they conflict.
  
  refer: https://docs.puppetlabs.com/learning/ordering.html

运维网声明 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-114355-1-1.html 上篇帖子: puppet-dashboard和foreman;puppet 的web管理端 下篇帖子: Advanced Puppet 系列的前言
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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