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:
[[Email protected] manifests] # vim site. pp
[[Email protected] manifests] # cat site. pp
Import 'order. PP'
[[Email protected] manifests] # cat order. pp
File {'/tmp/test1 ':
Ensure => present,
Content => "hi .",
}
File {'/tmp/test2 ':
Ensure => directory,
Mode = & gt; 644,
}
File {'/tmp/test3 ':
Ensure => link,
Target => '/tmp/test1 ',
}
Policy {"iam nofitying you ":}
Policy {"so am I! ":}
[[Email protected] tmp] # puppet agent -- test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '20140901'
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/policy [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!
, AndI'm using Ying 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 wowould 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 snapshot y resource that depends on a file resource:
[[Email protected] manifests] # cat file. pp
File {'/tmp/test111 ':
Ensure => present,
Content => "hi. this is a test 111111 file \ n ",
}
Using Y {'/tmp/test111 has already been synced /':
Require => File ['/tmp/test111'],
}
[[Email protected] tmp] # puppet agent -- test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '20140901'
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'/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:
BeforeBefore
Is used in the earlier resource
RequireRequire
Is used in the later resource
Notify
Subscribe is used in the later resource, if change, also sent refresh
All of them acceptResource reference(Or an array of them) as their value.
Before and Require
Before
AndRequire
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 cocould just as easily be written like this:
[[Email protected] manifests] # cat file. pp
File {'/tmp/test1 ':
Ensure => present,
Content => "Hi .",
Before => Policy ['/tmp/test1 has already been synced.'],
}
Using Y {'/tmp/test1 has already been synced .':}
[[Email protected] tmp] # puppet agent -- test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '20140901'
Notice:/Stage [main]/Main/File [/tmp/test1]/ensure: created
Notice:/tmp/test1 has already been synced.
Notice:/Stage [main]/Main/policy [/tmp/test1 has already been synced.] /message: defined 'message' as '/tmp/test1 has already been synced.'
Notice: Finished catalog run in 0.39 seconds
Between y and Subscribe
A few resource types (Service
, Exec
, AndMount
) 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; forExec
Resource, this cocould mean running its payload if any user accounts have been changed
TheNotify
AndSubscribe
Metaparameters make dependency relationships the wayBefore
AndRequire
Do, but they also makeNotification 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:
[[Email protected] manifests] # mkdir-p/etc/puppet/modules/ntp/files
[[Email protected] manifests] # cp/etc/ntp. conf/etc/puppet/modules/ntp/files
[[Email protected] manifests] # vim file. pp
File {'/etc/ntp. Conf ':
Ensure => file,
Mode = & gt; 600,
Source => 'puppet: // modules/ntp. Conf ',
}
Service {'ntpd ':
Ensure => running,
Enable => true,
Subscribe => File ['/etc/ntp. Conf'],
}
[[Email protected] tmp] # puppet agent -- test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for yum01.test.com
Info: Applying configuration version '20140901'
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} comment'
Notice:/Stage [main]/Main/File [/etc/ntp. conf]/mode: mode changed '000000' to '000000'
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,NtpdService will be restarted if Puppet has to edit its config file.
# Fileserver. conf
# Puppet automatically serves PLUGINS and files from modules: anything in
# <Module name>/files/<file name> is available to authenticated nodes
# 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 syncedBeforeThe resource the arrow points.
Refer: https://docs.puppetlabs.com/learning/ordering.html
Learning Puppet-Resource Ordering