semente
semente

Reputation: 7513

Puppet's Exec[] ignoring onlyif?

I'm experiencing a very strange behavior with Puppet's Exec type.

I have two similar Exec[] in a class, both with the onlyif parameter, as you can see below. The problem is Exec['apt-update'] is always performed (i.e. every time Puppet's agent apply its manifests), even when the onlyif condition is false, unlike Exec['install-newrelic-apt-key'] that works fine.

Note: my Exec[]'s path ($PATH) is configured outside this class and all commands works as expected in command line

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"],
  }
}

* The code above is public domain, feel free to use it as you want.

Upvotes: 3

Views: 4978

Answers (2)

semente
semente

Reputation: 7513

Actually everything is right with the manifest, but it just started work as expected after I've restarted the puppet agent process.

Sounds like a bug in Puppet for me.

Upvotes: 1

gabrtv
gabrtv

Reputation: 3588

I don't believe your onlyif commands will undergo the shell interpolation you're expecting. I would try switching to an unless statement as so:

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"],
  }

Upvotes: 2

Related Questions