I only want to pass the datadir argument to postgresql::globals if it contains a value.
You should not worry about not passing datadir
if it does not contain a value. Just pass the paramater to the postgresql::globals
along with your other paramaters. If the datadir
has a value the class will use it, and if it does not then postgresql::globals
will handle it just fine and use its own defaults. You need to modify the way your passing the paramater though, this will not work: datadir => "${datadir}"
, you must pass the it like this datadir => $datadir,
. Your define could be as simple as this:
define setup_postgresql_globals (
$datadir = undef,
) {
class { 'postgresql::globals':
datadir => $datadir,
needs_initdb => true,
version => '9.3',
}
}
The reason your getting Error: Duplicate declaration: Class[Postgresql::Globals] is already declared
, is becuase you are instantiating your define multipule times. By nature a define can be instantiated mutluple time so it can be problematic to do a class like include within one since you can only include a class like this once. There are a couple of ways you can resolve this issue.
1) Evaluate your need for this define at all. It seams like you created this define just to handle the datadir
paramater being a undef
value. As I have shown obove you might not need this define since your logic is not needed.
2) You should only do basic includes of classes within a define. You can include a class like this multuple times saftly. You would have to handle setting datadir
via the automatic paramater lookup system from hiera. For example:
define setup_postgresql_globals (
$datadir = undef,
) {
include 'postgresql::globals'
}
Again you should probably remove this define and handle including postgresql::globals
from where your instantiating setup_postgresql_globals
.