two puppet tricks: combining arrays and local tests
Joining Arrays
I found myself wanting to join a bunch of arrays in my puppet manifests. I had 3 lists of ip addresses, but wanted to join all 3 lists together into a single list to provide all ips to a template. I found some good tricks for flattening nested arrays in an erb --http://weblog.etherized.com/posts/175 -- but I found those solutions to be too "magic" and hard to read.I ended up settling on using an inline_template and split, like so:
$all_ips = split(inline_template("<%= (worker_ips+entry_point_ips+master_ips).join(',') %>"),',')
Testing Puppet Locally
To debug things like this, I like to use puppet locally to test a configuration by writing a test.pp file and passing that into puppet, e.g.:File: arrays-test.pp
define tell_me() { notify{$name:}}
$worker_ips = [ "192.168.0.1",
"192.168.0.2",
]
$entry_point_ips = ["10.0.0.1",
"10.0.0.2",
]
$master_ips = [ "192.168.15.1",
]
$all_ips = split(inline_template("<%= (worker_ips+entry_point_ips+master_ips).join(',') %>"),',')
tell_me{$all_ips:}
Running puppet:
$ puppet arrays-test.pp
notice: 192.168.0.2
notice: /Stage[main]//Tellme[192.168.0.2]/Notify[192.168.0.2]/message: defined 'message' as '192.168.0.2'
notice: 192.168.15.1
notice: /Stage[main]//Tellme[192.168.15.1]/Notify[192.168.15.1]/message: defined 'message' as '192.168.15.1'
notice: 192.168.0.1
notice: /Stage[main]//Tellme[192.168.0.1]/Notify[192.168.0.1]/message: defined 'message' as '192.168.0.1'
notice: 10.0.0.1
notice: /Stage[main]//Tellme[10.0.0.1]/Notify[10.0.0.1]/message: defined 'message' as '10.0.0.1'
notice: 10.0.0.2
notice: /Stage[main]//Tell_me[10.0.0.2]/Notify[10.0.0.2]/message: defined 'message' as '10.0.0.2'
Note that for these kinds of tests, I like to use notify
rather than building files.