Question

Puppet Version: 3.2.4 (Puppet Enterprise 3.0.1)

In order to better support nagios cfg_dir and cfg_file directives in the config file, I've created the following class(es), one for each option:

# Class to add a cfg_dir to the nagios configuration file
class nagios::server::cfg_dir (
  $config_dir,
  $nagios_user,
  $nagios_group,
  $nagios_config_file = '/etc/nagios3/nagios.cfg',
)
{
  # Build the config dir
  file {$config_dir:
    ensure => directory,
    owner => $nagios_user,
    group => $nagios_group,
    mode => '0750',
  }

  # Append cfg_dir=$config_dir path to nagios.cfg file
  augeas { "cfg_dir=$config_dir in $nagios_config_file":
    incl => "$nagios_config_file",
    lens => 'NagiosCfg.lns',
    changes => "set cfg_dir/[last()+1] ${config_dir}",
    require => File[$nagios_config_file],
  }
}

Trying to use this construct inside nagios::server, I have this:

# Set up config directories
each($cfg_dir) |$x| {
  class { 'nagios::server::cfg_dir':
    config_dir => $x,
    nagios_user => $nagios_user,
    nagios_group => $nagios_group,
    nagios_config_file => $nagios_config_file,
  }
}

Which should, in theory, execute the class instantiation for each path passed in to the nagios::server class like so:

class{'::nagios::server': cfg_dir => ['/etc/nagios.d','/etc/nagios/objects'] }

However, I run into this issue:

Error: Could not match |$x| at /tmp/vagrant-puppet-1/modules-2/nagios/manifests/server.pp:181 on node localhost.localdomain

Can someone provide a working example of each in use? Am I expecting too much from this built-in puppet function?

Was it helpful?

Solution 2

The answer to my follow-up question is to use defined types:

http://docs.puppetlabs.com/learning/definedtypes.html

Simply changing the above code from a class to a define and assign $config_dir the value from $target,

define nagios::server::cfg_dir (
  $config_dir = $target,
  $nagios_user,
  $nagios_group,
  $nagios_config_file = '/etc/nagios3/nagios.cfg',
){...

you can use constructs such as:

nagios::server::cfg_dir { '/etc/nagios.d/', '/etc/nagios/objects':
    nagios_user => 'nagios',
    nagios_group => 'nagios'
}

This solves the issue for me.

OTHER TIPS

Aside from a few of the code grammar issues above, I've found that this construct is only evaluated in the future parser:

puppet apply --parser=future --modulepath ...

http://docs.puppetlabs.com/puppet/latest/reference/experiments_lambdas.html

Still getting past other dependency issues. What pattern would I use to support this with the current parser instead of future? A custom function?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top