【发布时间】:2012-07-24 17:01:00
【问题描述】:
Puppetlabs 文档指出,为了让一个类需要另一个类,您应该使用关系链语法并在外部节点中声明这两个类。
我有一个 repo 类,它创建每个模块中的许多包所依赖的 yum repo 定义。在每个模块中,我都有一个 Class['repo'] -> Class['modulename'] 语句,并且两个类都在节点中声明。但是,当 puppet 运行时,它并不总是像预期的那样在模块类之前执行 repo 类。为什么不?下面的例子(puppet 2.6.16):
编辑:似乎有 3 个基本解决方案可以解决这个问题。
- 使用资源依赖替换类依赖 before/require 元参数(如图灵机的答案所示)。
- 移除外部类依赖并显式声明依赖 内部类之间。
- 使用 Puppetlabs 在 stdlib 模块中提供的锚类型 包含一个允许依赖类创建引用的类 使用链接语法到外部类。
那么,考虑到 Puppet v3 以及未来将重构保持在最低限度的愿望,这些方法中哪一种最好?
清单puppettest.pp:
class { 'repo': }
class { 'maradns': }
class repo {
class { 'repo::custom': }
}
class repo::custom {
yumrepo {'custom':
enabled => 1,
gpgcheck => 0,
descr => "Local respository - ${::architecture}",
baseurl => 'http://repo.nike.local/CentOS/\$releasever/\$basearch';
}
}
class maradns {
Class['repo'] -> Class['maradns::install']
Class['maradns::install'] -> Class['maradns::config']
Class['maradns::config'] ~> Class['maradns::service']
class { 'maradns::install': }
class { 'maradns::config': }
class { 'maradns::service': }
}
class maradns::install {
package { 'maradns':
ensure => present,
}
}
class maradns::config {
file { 'mararc':
ensure => present,
path => '/etc/mararc',
mode => '0644',
owner => root,
group => root,
}
}
class maradns::service {
service { 'maradns':
ensure => running,
enable => true,
hasrestart => true,
}
}
输出:
puppet apply puppettest.pp
err: /Stage[main]/Maradns::Install/Package[maradns]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install maradns' returned 1: Error: Nothing to do
notice: /Stage[main]/Maradns::Config/File[mararc]: Dependency Package[maradns] has failures: true
warning: /Stage[main]/Maradns::Config/File[mararc]: Skipping because of failed dependencies
notice: /Stage[main]/Maradns::Service/Service[maradns]: Dependency Package[maradns] has failures: true
warning: /Stage[main]/Maradns::Service/Service[maradns]: Skipping because of failed dependencies
notice: /Stage[main]/Repo::Custom/Yumrepo[custom]/descr: descr changed '' to 'Local respository - x86_64'
notice: /Stage[main]/Repo::Custom/Yumrepo[custom]/baseurl: baseurl changed '' to 'http://repo.test.com/CentOS/\$releasever/\$basearch'
notice: /Stage[main]/Repo::Custom/Yumrepo[custom]/enabled: enabled changed '' to '1'
notice: /Stage[main]/Repo::Custom/Yumrepo[custom]/gpgcheck: gpgcheck changed '' to '0'
notice: Finished catalog run in 2.15 seconds
【问题讨论】:
-
根据你的旗帜,@Michelle,我已经删除了赏金评论。不幸的是,它无法更改。然而,即使没有通知,悬赏问题也会引起注意。
-
我喜欢你的想法。我认为它在 3.2.* 中解决了吗?
标签: puppet