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:

[root@centos manifests]# vim site.pp
[root@centos manifests]# cat site.pp
import 'order.pp'
[root@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!":}

[root@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/test2So 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:

[root@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'],
}

[root@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:

[root@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.':}

[root@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 (serviceexec, 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:

[root@centos manifests]# mkdir -p /etc/puppet/modules/ntp/files

[root@centos manifests]# cp /etc/ntp.conf /etc/puppet/modules/ntp/files

[root@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'],
}

[root@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.conf 2014-11-07 07:56:08.681423545 +0000
+++ /tmp/puppet-file20141107-15367-s2gp6n-0 2014-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:

[root@centos manifests]# cat site.pp |grep order1
import 'order1.pp'

[root@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']

[root@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:

[root@centos manifests]# cat order1.pp
file {'/tmp/test11':
ensure => present,
content => "hi",
}
->
notify {'after':
message => '/tmp/test11 has already been synced.',
}

[root@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

Learning Puppet — Resource Ordering的更多相关文章

  1. Learning Puppet — Resources and the RAL

    Learning Puppet — Resources and the RAL Welcome to Learning Puppet! This series covers the basics of ...

  2. Learning Puppet — Manifests

    Begin In a text editor — vim, emacs, or nano — create a file with the following contents and filenam ...

  3. Learning Puppet — Variables, Conditionals, and Facts

    Begin $my_variable = "A bunch of text" notify {$my_variable:} Yup, that’s a variable, all ...

  4. 从入门到精通Puppet的实践之路

    本文有感于<精通Puppet配置管理工具>在豆瓣上的某些差评而顺手写的书评. 半路出家   故事要从12年初说起.  某天,部门老大让我所在team的老大调研一下当下业界的配置管理工具.于 ...

  5. Puppet简易入门

    一.查看官方提供的下载源 https://docs.puppet.com/guides/puppetlabs_package_repositories.html 二. 选择对应系统的下载源 因为本机是 ...

  6. Puppet权威指南

    <Puppet权威指南>基本信息作者: 王冬生 丛书名: Linux/Unix技术丛书出版社:机械工业出版社ISBN:9787111485988上架时间:2014-12-25出版日期:20 ...

  7. 烂泥:puppet添加带密码的用户

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前一篇文章,我们介绍了有关puppet3.7的安装与配置,这篇文章我们再来介绍下如何利用puppet添加带密码的用户. 要通过puppet添加带密码的用 ...

  8. Puppet's Commands 3.7

    Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...

  9. puppet重申证书

    直接上步骤,由于测试用的是PE3.X版本,在网上搜的命令几乎与PE相关的puppet命令不同了, 1.在PE-Client操作,停止pe-puppet,pe-mcollective资源; puppet ...

随机推荐

  1. CUDA 程序中的同步

    前言 在并发,多线程环境下,同步是一个很重要的环节.同步即是指进程/线程之间的执行顺序约定. 本文将介绍如何通过共享内存机制实现块内多线程之间的同步. 至于块之间的同步,需要使用到 global me ...

  2. Java线程的生命周期

    线程的生命周期包括:新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead)5种状态.线程状态转换图如下: 1.新建状态(New) 当程序使用new关 ...

  3. 无法远程访问虚拟机中的EM (Oracle Enterprise Manager)

    今天安装EM,因为文件系统采用ASM来控制,后面需要输入一些ASM相关的信息.这个ASMSNMP用户是新建,所以设个密码自己记下就行. Enter the following information: ...

  4. CoreOS 835.12.0 稳定版安装

    导读 CoreOS是一个基于Docker的轻量级容器化Linux发行版,为Docker而生,CoreOS作为Docker生态圈中的重要一员,日益得到各大云服务商的重视,发展风头正劲. CoreOS宣称 ...

  5. 安装CDH4 (Cloudera Distribution Hadoop)步骤

    安装流程 机器和系统 3台服务器,安装centos 6.4 64bit系统,内存8G,磁盘60G,cpu单核 已配置好静态ip,并配置好/etc/hosts 下载cdh4版本 https://www. ...

  6. Apache CXF 101 Win Eclipse开发环境搭建

    前言 博客草稿中“SOA生态系统初探”一文一直没有进展,感觉要将SOA.Web Service(WS).REST等概念阐述清楚还需要一些酝酿. 顶天须得立地,这里记录一些“下里巴人”的实践,主要考察A ...

  7. ZOJ 1107 FatMouse and Cheese

    原题链接 题目大意:FM在一个街道n*n街道的(0,0)点,在每个网格里放着cheese,他要尽可能多的吃这些cheese.有两个规则:1)他跑的总距离不能超过k步:2)下一个节点的cheese的块数 ...

  8. 故障模块名称: mso.dll

    本人今天早上打开word文档的时候打不开了,反复试了n次也不成,一想八成儿要重新装了,结果我点开详细信息看了一下,看到了“故障模块名称: mso.dll”这个提示,结果我就放到了百度上找了一下,都是只 ...

  9. 20145220 实验五 Java网络编程

    20145220 实验五 Java网络编程 实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务 ...

  10. hdu3231 拓扑序

    题意:在空间内有多个长方体,有一系列关系,分别是 A 的所有点的 x 坐标都比 B 的所有点的 x 坐标小, A 的所有点的 y 坐标都比 B 的所有点的 y 坐标小, A 的所有点的 z 坐标都比 ...