3

我在使用 Puppet 的 Exec 类型时遇到了一种非常奇怪的行为。

Exec[]在一个类中有两个类似的,都带有onlyif参数,如下所示。问题Exec['apt-update']总是会被执行(即每次 Puppet 的代理应用其清单时),即使onlyif条件为假,Exec['install-newrelic-apt-key']也不会正常工作。

注意: myExec[]的路径 ($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"],
  }
}

* 以上代码为公共领域,请随意使用。

4

2 回答 2

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"],
  }
于 2013-06-07T17:46:58.253 回答
1

实际上,清单中的一切都是正确的,但是在我重新启动 puppet 代理进程后,它才按预期开始工作

对我来说听起来像是 Puppet 中的一个错误。

于 2013-06-11T20:09:42.980 回答