我在使用Puppet的Exec类型时遇到了一种非常奇怪的行为。
我在一个类中有两个类似的Exec[]
,都有onlyif
参数,如下所示。问题是始终执行Exec['apt-update']
(即每次Puppet的代理应用其清单时),即使onlyif
条件为假,也不像Exec['install-newrelic-apt-key']
工作正常。
注意:我的Exec[]
路径($ PATH)在此类之外配置,所有命令在命令行中按预期工作
class newrelic::server($license_key) {
file { "/etc/apt/sources.list.d/newrelic.list":
ensure => present,
content => "deb http://apt.newrelic.com/debian/ newrelic non-free",
}
exec { "apt-update":
command => "aptitude update",
onlyif => "test 0 -eq $(dpkg -l | grep -c newrelic-sysmond)",
require => File['/etc/apt/sources.list.d/newrelic.list'],
}
exec { "install-newrelic-apt-key":
command => "apt-key adv --keyserver hkp://subkeys.pgp.net --recv-keys 548C16BF",
onlyif => "test 0 -eq $(apt-key list | grep -c 548C16BF)",
}
package { "newrelic-sysmond":
ensure => latest,
require => [
Exec["install-newrelic-apt-key"],
Exec["apt-update"],
],
}
file { "/etc/newrelic/nrsysmond.cfg":
ensure => present,
content => template("newrelic/nrsysmond.erb"),
owner => "root",
group => "newrelic",
mode => "0640",
notify => Service["newrelic-sysmond"],
}
service { "newrelic-sysmond":
ensure => running,
enable => true,
hasstatus => true,
require => Package["newrelic-sysmond"],
}
}
*以上代码属于公共领域,您可以根据需要随意使用。
答案 0 :(得分:2)
我不相信您的onlyif
命令会经历您期望的shell插值。我会尝试切换到unless
语句:
class newrelic::server($license_key) {
file { "/etc/apt/sources.list.d/newrelic.list":
ensure => present,
content => "deb http://apt.newrelic.com/debian/ newrelic non-free",
}
exec { "apt-update":
command => "aptitude update",
unless => "dpkg -l | grep -c newrelic-sysmond",
require => File['/etc/apt/sources.list.d/newrelic.list'],
}
exec { "install-newrelic-apt-key":
command => "apt-key adv --keyserver hkp://subkeys.pgp.net --recv-keys 548C16BF",
unless => "apt-key list | grep -c 548C16BF",
}
package { "newrelic-sysmond":
ensure => latest,
require => [
Exec["install-newrelic-apt-key"],
Exec["apt-update"],
],
}
file { "/etc/newrelic/nrsysmond.cfg":
ensure => present,
content => template("newrelic/nrsysmond.erb"),
owner => "root",
group => "newrelic",
mode => "0640",
notify => Service["newrelic-sysmond"],
}
答案 1 :(得分:1)
实际上清单上的一切都是正确的,但是在我重新启动木偶代理程序之后,只是按预期开始工作。
听起来像是Puppet中的一个错误。