Search code examples
manifestpuppet

Changing hash value doesnt work


My hash is as follow:

$hash = {
  'somekey' => {
     online => true,
     date   => today
  }
}

I do some random check to change the value of the online key. If something's offline, I want to change the value to offline.

I tried the following code:

$::hash[somekey][online] = false

but that doesn't seem to work. Is there any way I can change the value of online? :(


Solution

  • If at all possible, restructure your manifest.

    In most programming languages, you will use constructs like

    $variable = default-value
    if ( condition ) {
        $variable = another-value
    }
    

    The following structure lends itself much better to Puppet's programming paradigm, however.

    if ( condition ) {
        $variable = another-value
    }
    else {
        $variable = default-value
    }
    

    As for complexer hashes, you might get away with selector expressions.

    $hash = {
      'somekey' => {
        'status' => $condition ? {
          'special-case' => 'offline',
          default        => 'online',
        'date'   => 'today'
      }
    }
    

    It's either that, or storing the dynamic value in a variable that is used in the declaration of the hash value.