Learning Puppet — Resource Ordering

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 => 644,
}
file { ‘/tmp/test3‘:
ensure => link,
target => ‘/tmp/test1‘,
}

notify {" iam nofitying you":}
notify {" 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 ‘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:

[[email protected] 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‘],
}

[[email protected] 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
  • 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:

[[email protected] 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.‘:}

[[email protected] 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:

[[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 => 600,
source => ‘puppet:///modules/ntp/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 ‘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.

# 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.

refer: https://docs.puppetlabs.com/learning/ordering.html

时间: 2024-09-29 06:25:45

Learning Puppet — Resource Ordering的相关文章

Learning Puppet — Resources and the RAL

Learning Puppet — Resources and the RAL Welcome to Learning Puppet! This series covers the basics of writing Puppet code Begin Log into the Learning Puppet VM as root, and run puppet resource service. This command will return something like the follo

Puppet resource命令参数介绍(七)

Puppet resource是资源抽象层的shell,通过它可以将当前系统状态转换为puppet的代码,并且还具有将当前系统状态改变为Puppet RAL状态等功能. [[email protected] ~]# puppet resource -h puppet-resource(8) -- The resource abstraction layer shell ======== SYNOPSIS -------- Uses the Puppet RAL to directly inter

Learning Puppet — Manifests

Begin In a text editor — vim, emacs, or nano — create a file with the following contents and filename: written and applied your first Puppet manifest. [[email protected] ~]# useradd testuser [[email protected] ~]# cat /etc/passwd |grep testtestuser:x

Learning Puppet — Variables, Conditionals, and Facts

Begin $my_variable = "A bunch of text" notify {$my_variable:} Yup, that’s a variable, all right. refer: https://docs.puppetlabs.com/learning/variables.html

puppet知识简记

Puppet 开始 安装 aptitude install puppet puppetmaster 工作机制 puppet是通过ssl方式进行安全通信的,在正常同步前,需要进行证书的获取和认证 puppet运行机制大概是这样: 客户端向服务端发送facts信息,请求返回catalog, 服务端检查类文件等的关于客户端的信息打包catalog返回给客户端, 客户端按照catalog进行一系列检查同步操作. puppet资源类型及属性 3.1 资源 资源是puppet处理主机而分解的基本元素,资源的

puppet基础学习(二)

puppet基础学习(二) 六.ResourceOrdering(资源定序) 使用变化参数before , require , notify , subscribe catalog是对一个给定的系统的所有资源及关系的编译,在编译catalog时,除非我们指定资源的执行顺序,不然puppet是以其自己的顺序管理,大多数时候puppet指定适当的方式,例如puppet管理用户gigabyte应该存在和文件夹/home/gigabyte/bin应该存在并属于用户gigabyte时,puppet会自动指

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

本文有感于<精通Puppet配置管理工具>在豆瓣上的某些差评而顺手写的书评. 半路出家   故事要从12年初说起.  某天,部门老大让我所在team的老大调研一下当下业界的配置管理工具.于是我的老大给我分配了一个棘手的任务,要求我转型去做devops,并尝试在本季度内使用Puppet来管理现有的IAAS内部平台上的所有业务,工作成果计入KPI.    于是,我半路出家从dev转成了ops.    我花了几天的时间把learning Puppet动手练习了一遍,在会使用几个基础的resource

Puppet学习--基础安装和配置

0. 安装环境 客户端IP puppet_client.example.net(192.168.1.10) 服务端IP puppet_server.example.net(192.168.1.11) OS版本 CentOS release 6.6 x86_64 puppet版本 3.7.5 1.预安装配置 需要在服务端和客户端进行一些必要的预安装配置,因此本节下面的命令需要在客户端和服务端均要执行. (1) yum install ruby #安装ruby (2) 修改/etc/hosts,写入

通过MCollective实现puppet向windows的推送

puppet在比较老的版本的时候是通过kick进行推送实现配置及时更新,由于kick的效率问题,在比较新的版本中开始采用第三方工具MCollective来实现,网上介绍如何部署MCollective文章也不少,但大都是linux平台下的部署,windows下的几乎没有,我在研究的时候也是一路坎坷,差点放弃,不过看到有一个老外自己成功部署后,有点不甘心,在坚持下终于配置成功,现在整理一下分享出来. 如果你还一点都不了解MCollective,建议看看这篇文章,他讲的是在linux下部署MColle