我开始设置 puppet。我想设置一个依赖项,即在该类尝试安装或启动任何内容之前必须安装邮件传输代理。使用 puppet,设置依赖项的标准方法似乎是require blah
。挑战在于我没有在所有系统上使用相同的 MTA。一些实际的邮件服务器系统我有完整的 MTA(exim),但绝大多数系统都安装了 ssmtp。我想要做的是设置一个要求,以便在处理类之前安装并运行其中一个 MTA foo
。
这是一个配置,可以在一定程度上展示我正在尝试做的事情。
node default {
if $fqdn in ["mail1.example.org",
"mail2.example.org",
"mail3.example.org"] {
include fullmta # mailhub, and so on
} else {
include ssmtp # really basic send-only mta.
}
include foo # class that requries an mta be installed
}
class foo {
require MTA # FIXME, A valid mta is required.
package { foo: ensure => present, }
... # also a service, and some files, and so on...
}
那么在我的 foo 类中,我如何要求其中一个可能的 MTA 类已经被处理?
答案1
如果将 MTA 逻辑拆分为单独的类,则可以在那里处理逻辑 - 并且您的资源可以要求 MTA 类来强制执行依赖关系。
node default {
include mta
include foo # class that requries an mta be installed
}
class mta {
if $fqdn in ["mail1.example.org",
"mail2.example.org",
"mail3.example.org"] {
include fullmta # mailhub, and so on
} else {
include ssmtp # really basic send-only mta.
}
}
class foo {
package { foo: ensure => present,
require => Class['mta'], }
... # also a service, and some files, and so on...
}
答案2
使用别名。例如:
service { "ssmtp":
...
alias => "MTA",
}
service { "fullmta":
...
alias => "MTA",
}
class foo {
package { foo:
ensure => present,
require => Service["mta"],
...
}
...
}
答案3
您可以将require
依赖项指定为数组,在这种情况下,Puppet 将确保所有依赖项都得到满足,然后再继续。在这种情况下,我通常会执行以下操作:
node default {
include mta
include foo # class that requries an mta be installed
}
class mta {
if $fqdn in ["mail1.example.org",
"mail2.example.org",
"mail3.example.org"] {
package { "conflicting-package-A": ensure => present, }
package { "conflicting-package-B": ensure => absent, }
} else {
package { "conflicting-package-A": ensure => absent, }
package { "conflicting-package-B": ensure => present, }
}
}
class foo {
package { foo: ensure => present,
require => [Package["conflicting-package-A",
"conflicting-package-B"], }
... # also a service, and some files, and so on...
}
这样,您不仅可以确保该foo
包明确依赖于其他包,而且还可以进行设置,以便如果您mail*.example.org
将来从列表中删除主机,“冲突包 A”将自动替换为“冲突包 B”。