Ethan Brouwer
Ethan Brouwer

Reputation: 1005

Puppet advanced default params

Basically I have a puppet class cassandra_manager::params. I have a ton of params set like so:

# etc...
$cluster_name = undef,
$num_tokens = 256,
$initial_token = undef,
$hinted_handoff_enabled = true,
# etc...

Now most of these ones set undef, I just handle in my template just commenting it out if it's undef.

But there are a few that I need to use some previous params, and facts to figure out the defaults. Here is one main example:

$memtable_flush_writers = undef,

I then try to set it later like this:

$min_from_cores_and_disks = min([ $::processorcount, size(split($::hard_drives, ',')) ])
if !$memtable_flush_writers {
  if 0 + (0 + $min_from_cores_and_disks[0]) > 8 or (0 + $min_from_cores_and_disks[0]) < 2 {
    if (0 + $min_from_cores_and_disks[0]) < 2 {
      $memtable_flush_writers = 2
    }
    elsif (0 + $min_from_cores_and_disks[0]) > 8 {
      $memtable_flush_writers = 8
    }
  } else {
    $memtable_flush_writers = (0 + $min_from_cores_and_disks[0])
  }
}

Then puppet tells me I can't set $memtable_flush_writers because all variables are immutable in puppet.

So then I changed checking for whether the variable was "false" and didn't set the variable up above, but that just told me the obvious, that $memtable_flush_writers wasn't set.

What's the best way that I can still get the functionality from the if statements above without having these errors pop up. Thanks in advance.

Upvotes: 1

Views: 168

Answers (2)

Anshu Prateek
Anshu Prateek

Reputation: 3055

You have to create a new variable which you use only for declaration and setting any default value in the class definition.

class cassandra_manager::params (
        $memtable_flush_writers_count = undef
      ){
          if !$memtable_flush_writers_count {
              # Whatever you want to do
                 $memtable_flush_writers = 'your_value'
             } else {
                 $memtable_flush_writers = $memtable_flus_writers_count
          }

       }

Upvotes: 1

Felix Frank
Felix Frank

Reputation: 8223

The variables in cassandra_manager::params are typically used as default values for parameters of class cassandra_manager.

class cassandra_manager(
    $cluster_name = $cassandra_manager::params::cluster_name,
    $num_tokens = $cassandra_manager::params::num_tokens,
    ...) inherits cassandra_manager::params {

}

You override the defaults by passing such parameters.

class { 
  'cassandra_manager':
    memtable_flush_writers => <value for this node>
}

Upvotes: 0

Related Questions