Include class if package exists

89 views
Skip to first unread message

Pascal Robert

unread,
Feb 10, 2014, 5:20:06 PM2/10/14
to puppet...@googlegroups.com
Hi,

I'm using Puppet for the first time, and for my tests, I'm recreating our Nagios setup into a Puppet modules. It works at 95%, but I do have a problem: my module include files but I also need to modify a couple of Nagios configuration files that comes with the Nagios RPM package. Example:

commandes.pp:

class nagios::commandes {

nagios_command { 'process-service-perfdata':
command_name => 'process-service-perfdata',
ensure => present,
command_line => '/usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl',
target => '/etc/nagios/objects/commands.cfg',
}

}

init.pp:

class nagios {

  include nagios::commandes
  
  package { 'nagios':
    ensure => present,
  }

  package { 'nagios-plugins-all':
    ensure => present,
  }

  package { 'nrpe':
    ensure => present,
  }

  service { 'nagios':
    ensure => running,
    hasstatus => true,
    hasrestart => true,
    enable => true,
    require => Package["nagios"],
  }
}


It works fine when Nagios is already installed, but if not, the include of the class in init.pp is done before the package is installed so it fails.

Error: Puppet::Util::FileType::FileTypeFlat could not write /etc/nagios/objects/commands.cfg: No such file or directory - /etc/nagios/objects/commands.cfg
Error: /Stage[main]/Nagios::Commandes/Nagios_command[process-service-perfdata]: Could not evaluate: Puppet::Util::FileType::FileTypeFlat could not write /etc/nagios/objects/commands.cfg: No such file or directory - /etc/nagios/objects/commands.cfg

If there a way to include the class only if the package is installed?

Daniele Sluijters

unread,
Feb 11, 2014, 2:56:02 AM2/11/14
to puppet...@googlegroups.com
Hi,

What you're actually running into is that you want the nagios::commandes class to be executed after the nagios package has been installed.

In that case, use the before metaparameter:

package { 'nagios':
    ensure => present,
    before => Class['nagios::commandes'],
}

This should fix your ordering and ensure everything works as you expect. Generally when you run into an issue with Puppet where you want to conditionally order something al you need are the require/before/notify/subscribe parameters.

Pascal Robert

unread,
Feb 11, 2014, 9:25:08 AM2/11/14
to puppet...@googlegroups.com
Works fine, thanks!
Reply all
Reply to author
Forward
0 new messages