puppet学习(二)

参考内容来源于朱双印 https://www.zsythink.net/archives/420

资源间的依赖关系以及资源的引用

在一个清单中配置多个资源时,这些资源之间往往存在着依赖关系,在服务器上安装nginx并且想要在nginx安装完成后启动nginx服务,那么我们将会使用到两个资源, package资源与service资源,
而且package资源应该在service资源之前被执行,因为如果想要启动nginx那么则必须先安装了nginx才行,同样,当我们修改了nginx的配置文件时,对应nginx服务的配置应该被重载.

before/require  这两个元属性可以定义资源之间的依赖关系。
notify/subscribe 这两个元属性可以定义资源之间的通知关系。

例子1 require

定义了一个user类型的资源,用户名称为zsylinux,我们的目标是创建这个用户,并且指定了用户的gid为888的组。但目前还没有gid为888的组。 所以可这样写:

user{"zsylinux":
        ensure => present,
        gid => 888,
        managehome => true,
        require => Group[linuxgroup],
}

group{"linuxgroup":
        ensure => present,
        gid => 888,
}

在这里插入图片描述
在这里插入图片描述

例子2 before

user{"zsylinux":
        ensure => present,
        gid => 888,
        managehome => true,
}

group{"linuxgroup":
        ensure => present,
        gid => 888,
        before => User['zsylinux'],
}

在这里插入图片描述

例子3 subscribe

通过 notify 与 subscribe可以定义资源之间的通知机制。
需求:修改nginx的配置文件以后,需要通知nginx服务进行重载操作
在这里插入图片描述

file{"nginxconf":
        path => '/etc/nginx/conf.d/default.conf',
        ensure => present,
        source => '/template/default.conf',
}

exec{
        command => "/usr/sbin/nginx -s reload",
        subscribe => File["nginxconf"],
        refreshonly => true,
}

例子4 notify

在这里插入图片描述

file{"nginxconf":
        path => '/etc/nginx/conf.d/default.conf',
        ensure => present,
        source => '/template/default.conf',
        notify => Exec['nginxreload'],
}

exec{"nginxreload":
        command => "/usr/sbin/nginx -s reload",
        refreshonly => true,

}

例子5 ”通知链”通过 “~>”连接通知链

在这条”通知链”中,我们定义了由”nginxconf”资源通知”nginxreload”资源。 当使用”通知链”时,我们则不需要使用notify 与 subscribe这两个元属性了。

file{"nginxconf":
        path => '/etc/nginx/conf.d/default.conf',
        ensure => present,
        source => '/template/default.conf',
}

exec{"nginxreload":
        command => "/usr/sbin/nginx -s reload",
        refreshonly => true,
}

File['nginxconf'] ~> Exec['nginxreload']

例子6 ”依赖链”或者称为”次序链” 。 通过”->”符号连接次序链

我们可以使用”依赖链”或者称为”次序链”,来代替require 与 before 两个属性

user{"zsylinux":
        ensure => present,
        gid => 888,
        managehome => true,
}

group{"linuxgroup":
        ensure => present,
        gid => 888,
}

Group['linuxgroup'] -> User['zsylinux']

在这里插入图片描述

这里仅是入门了小部分,还有很多坑没填,比如puppetmaster 对 puppetclient生成CA证书。对其批量操作等。。。

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值