rlandster
rlandster

Reputation: 7825

Puppet parameterized classes and changing parameters

I am trying to understand Puppet parameterized classes. I have a parameterized class defined this way:

class defaults(
  $no_samba = 'FALSE'
)
{
  if ($no_samba =~ /TRUE/) {
    notify { "will not install samba": } ;
  } else {
    # install samba here
  }

  # More server install tasks here...
}

Furthermore, I define a basenode as follows:

node basenode
{
  class {'defaults':
    no_samba => 'FALSE',
  }
}

and then I instantiate a server:

node myserver1 inherits basenode {
  Class['defaults'] { no_samba => 'TRUE' }
}

However, this does not work. The myserver1 node does not show the notify message indicating that samba will not be installed.

Upvotes: 2

Views: 13183

Answers (4)

kenorb
kenorb

Reputation: 166329

Here is simple example:

class apache-setup {
  class { 'apache':
    mpm_module => 'prefork',
  }
}

include apache-setup

Or:

class { '::mysql::server':
  config_file => '/etc/my.cnf',
  root_password    => 'root', # Sets MySQL root password.
  override_options => {
    'mysqld' => {
      'max_connections' => '512',
      'max_allowed_packet' => '256M',
      'log' => 'ON',
      'log_slow_queries' => 'ON',
      'general_log' => 'ON',
      'wait_timeout' => '28800',
    }
  }
}

Upvotes: 0

pwan
pwan

Reputation: 2914

Here's my non-typo answer - I think you're running into http://projects.puppetlabs.com/issues/7890

Here's a code sample where I tweaked your code to get the effect you're looking for, based on the rewritten example in the ticket:

class defaults(
  $no_samba = 'FALSE'
)
{

  notify {"no_samba_hack" :
    message => "$no_samba";
  }

  if ($no_samba =~ /TRUE/) {
    notify { "will not install samba": }
  } else {
    # install samba here
  }

  # More server install tasks here...
}

class basenode($no_samba="FALSE") {
  class {defaults: no_samba => $no_samba}
}

node yourserver {

  class { 'basenode' : no_samba => 'TRUE'}

}

When I run that with 'puppet apply sample.pp' with puppet 2.7.11 on Ubuntu 12.04, I get the following output:

notice: will not install samba
notice: /Stage[main]/Defaults/Notify[will not install samba]/message: defined 'message' as 'will not install samba'
notice: TRUE
notice: /Stage[main]/Defaults/Notify[no_samba_hack]/message: defined 'message' as 'TRUE'
notice: Finished catalog run in 0.05 seconds

Upvotes: 1

Mark Roggenkamp
Mark Roggenkamp

Reputation: 144

I believe it has to do with scope. It looks like you're creating the 'default' class in the base node and then setting a resource default for the 'default' class after the fact in something that inherits that basenode.

http://docs.puppetlabs.com/guides/language_guide.html

"Defaults are not global — they only affect the current scope and scopes below the current one."

Upvotes: 0

pwan
pwan

Reputation: 2914

Was samba installed on myserver1, and/or did any of the other server install tasks get triggered ? If only the notify message wasn't printed, then it may really be a problem with notify type versus the notice function.

Notify should look like "notify{"i have curly brackets and a trailing colon":}

Notice is called like a function: notice("i use parenthesis")

Try changing 'notify' to 'notice' and see if it works. You may also want to check the puppet syntax with 'puppet parser validate default.pp' (assuming your default class is in default.pp)

Upvotes: 1

Related Questions