- 木偶教程
- 木偶 - 主页
- 基础木偶
- 木偶 - 概述
- 木偶 - 建筑
- 傀儡 - 安装
- 木偶 - 配置
- Puppet - 环境会议
- 木偶大师
- Puppet - 代理设置
- Puppet - SSL 签名证书设置
- 安装和配置 r10K
- Puppet - 验证设置
- Puppet - 编码风格
- Puppet - 清单文件
- 木偶 - 模块
- Puppet - 文件服务器
- 木偶 - 事实与事实
- 高级傀儡
- 木偶 - 资源
- Puppet - 资源抽象层
- 木偶 - 模板
- 木偶 - 课程
- 木偶 - 功能
- Puppet - 自定义函数
- 木偶 - 环境
- 木偶 - 类型和提供者
- Puppet - RESTful API
- 木偶 - 现场项目
- 木偶有用资源
- 木偶 - 快速指南
- 木偶 - 有用的资源
- 木偶 - 讨论
木偶 - 课程
Puppet 类被定义为资源的集合,这些资源被分组在一起以便使目标节点或机器处于所需的状态。这些类在位于 Puppet 模块内的 Puppet 清单文件中定义。使用类的主要目的是减少任何清单文件或任何其他 Puppet 代码中的相同代码重复。
以下是 Puppet 类的示例。
[root@puppetmaster manifests]# cat site.pp
class f3backup (
$backup_home = '/backup',
$backup_server = 'default',
$myname = $::fqdn,
$ensure = 'directory',
) {
include '::f3backup::common'
if ( $myname == '' or $myname == undef ) {
fail('myname must not be empty')
}
@@file { "${backup_home}/f3backup/${myname}":
# To support 'absent', though force will be needed
ensure => $ensure,
owner => 'backup',
group => 'backup',
mode => '0644',
tag => "f3backup-${backup_server}",
}
}
在上面的示例中,我们有两个需要用户存在的客户端。可以注意到,我们已经重复了相同的资源两次。组合两个节点时不执行相同任务的一种方法。
[root@puppetmaster manifests]# cat site.pp
node 'Brcleprod001','Brcleprod002' {
user { 'vipin':
ensure => present,
uid => '101',
shell => '/bin/bash',
home => '/home/homer',
}
}
以这种方式合并节点来执行配置并不是一个好的做法。这可以通过创建一个类并将创建的类包含在节点中来简单地实现,如下所示。
class vipin_g01063908 {
user { 'g01063908':
ensure => present,
uid => '101',
shell => '/bin/bash',
home => '/home/g01063908',
}
}
node 'Brcleprod001' {
class {vipin_g01063908:}
}
node 'Brcleprod002' {
class {vipin_g01063908:}
}
需要注意的一点是类结构的样子以及我们如何使用 class 关键字添加新资源。Puppet 中的每种语法都有自己的特点。因此,选择的语法取决于条件。
参数化类
如上面的示例所示,我们已经了解了如何创建类并将其包含在节点中。现在有些情况我们需要在每个节点上有不同的配置,例如需要在每个节点上有不同的用户使用相同的类。Puppet 中使用参数化类提供此功能。新类的配置将如以下示例所示。
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp
class user_account ($username){
user { $username:
ensure => present,
uid => '101',
shell => '/bin/bash',
home => "/home/$username",
}
}
node 'Brcleprod002' {
class { user_account:
username => "G01063908",
}
}
node 'Brcleprod002' {
class {user_account:
username => "G01063909",
}
}
当我们在节点上应用上述 site.pp 清单时,每个节点的输出将如下所示。
Brcleprod001
[root@puppetagent1 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent1.testing.dyndns.org Info: Applying configuration version '1419452655' Notice: /Stage[main]/User_account/User[homer]/ensure: created Notice: Finished catalog run in 0.15 seconds [root@brcleprod001 ~]# cat /etc/passwd | grep "vipin" G01063908:x:101:501::/home/G01063909:/bin/bash
Brcleprod002
[root@Brcleprod002 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent2.testing.dyndns.org Info: Applying configuration version '1419452725' Notice: /Stage[main]/User_account/User[bart]/ensure: created Notice: Finished catalog run in 0.19 seconds [root@puppetagent2 ~]# cat /etc/passwd | grep "varsha" G01063909:x:101:501::/home/G01063909:/bin/bash
还可以设置类参数的默认值,如以下代码所示。
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp
class user_account ($username = ‘g01063908'){
user { $username:
ensure => present,
uid => '101',
shell => '/bin/bash',
home => "/home/$username",
}
}
node 'Brcleprod001' {
class {user_account:}
}
node 'Brcleprod002' {
class {user_account:
username => "g01063909",
}
}