0

During the task of writing a module, I found out that I have a huge amount of configurable parts. To better organize the module (for the sake of read- and maintainability), I have chosen to split template files up. As those template parts/fragments already sum up groups of configurable variables, I thought it might come handy to use hashes that represent the contents of the template fragments

(hash) $db -> template( config.db.erb )
(hash) $*** -> template( config.***.erb )

For some reason, I got something wrong with the Puppet/Ruby foo that I have set up, as only the thrown in hash values get used and the rest stays undef.

# Calling the module
class { 'wppuppet':
  location => '/var/www/wp',
  db       => {
    prefix => 'foo',
    user   => 'usr',
    pass   => '^[:D',
  },
}

The actual init class inherits from the params defaults class

# init.pp
class wppuppet(
  $location  = $wppuppet::params::location,
  $db        = $wppuppet::params::db
) inherits wppuppet::params {

  validate_hash( $db )
  validate_string( $db['name'] )
  # ...
  validate_bool( str2bool( $db['repair'] ) )

  class { 'wppuppet::config':
    db        => {
      prefix  => $db['prefix'],
      name    => $db['name'],
      user    => $db['user'],
      pass    => $db['pass'],
      host    => $db['host'],
      repair  => $db['repair'],
    }
  }

# The params.pp defaults class
class wppuppet::params {
  $db = {
    prefix  => 'wp',
    name    => 'wordpress',
    user    => 'root',
    pass    => 'root',
    host    => 'localhost',
    charset => 'utf8',
    repair  => true,
  }
}

Then there's the config.pp file containing the wppuppet::config class that generates the file from the concatenated templates.

  # The actual config.php file
  file { "${location}/config.php":
    ensure  => file,
    content => template(
      'wppuppet/config.db.erb',
      'wppuppet/config.debug.erb'
      # ...
    ),
  }

And finally there's the config.database.erb file

define( 'DB_NAME',     '<%= @db['name'] %>' );
define( 'DB_USER',     '<%= @db['user'] %>' );
define( 'DB_PASSWORD', '<%= @db['pass'] %>' );
define( 'DB_HOST',     '<%= @db['host'] %>' );

Edit Can someone explain me where I lost track and why I can't get the default values? When I place notice('params loaded') or simply dump the $db hash, I can see it being set in each class on the CLI, but not in the config.pp file / config class.

  • Are config filenames right? Is it "config.db.erb" or "config.database.erb" ? – R.Sicart May 3 '14 at 9:58
  • That was just me renaming it for simplicity. So yes, they are right :) I tested things through and files are loaded, but I can't seem to parse defaults vs input arguments. – kaiser May 3 '14 at 16:56
  • perhaps vars are not well defined in the current scope and using a method like "scope.lookupvar" could help – R.Sicart May 3 '14 at 17:55
1

Default values for classes are either used (in whole) or replaced during declaration of the class. Puppet has no means of somehow merging the hashes together.

You may wish to switch to a Hiera based approach. By putting the default and the specific values in your hierarchy, you can take advantage of Hiera's ability to perform the deep merge you apparently require.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.