Friday, March 2, 2012

Stupid Puppet Tricks: Appending to a variable

Want to maybe once in a while append things to a variable and not have to result to 15 snippet files and a exec to cat?  Here's one way that assumes that your classes are under your control and you can declare and use top scope variables. 

This is a kludge, and likely shouldn't work in puppet.  It's certainly against the core design and probably just a bad idea.

node 'foo' {
  include appender
  include speaker
  $bob = ''
  appender::append { 'one':
    $text => 'one',
    before => Appender::Append['two'],
  }
  append::append {'two':
    $text => 'two',
  }
  speaker::speak {'truth':
    $lies => $bob,
    require => Appender::Append['two'],
  }
}

class appender {
  define append($text) {
    $bob += "$text "
  }
}

class speaker {
  define speak($lies) {
    notice("the speaker $lies")
  }
}