Puppetlabs 文档指出,为了让一个类需要另一个类,您应该使用关系链接语法并在外部节点中声明这两个类。
我有一个 repo 类,它创建每个模块中的许多包所依赖的 yum repo 定义。在每个模块中,我都有一个 Class['repo'] -> Class['modulename'] 语句,并且两个类都在节点中声明。但是,当 puppet 运行时,它并不总是按预期在模块类之前执行 repo 类。为什么不?下面的示例(木偶 2.6.16):
编辑:这个问题似乎有 3 个基本解决方案。
- 使用 before/require 元参数将类依赖项替换为资源依赖项(如图灵机的答案所示)。
- 删除外部类依赖关系并显式声明内部类之间的依赖关系。
- 在 stdlib 模块中使用 Puppetlabs 提供的锚类型来包含一个类,该类允许依赖类使用链接语法创建对外部类的引用。
那么,考虑到 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