Posts Tagged ‘inline_template’

Puppet Tips&Tricks: Variable variables

Wednesday, September 1st, 2010

Sometimes you want to use variable variables, for instance when you want to iterate over all the ipaddress_* facts that facter found. Using something like ${ipaddress_$if} doesn’t work, though. Inline_template to the rescue! Thanks to Volcane on IRC, this is a possible solution:

$ifs = split($interfaces,",")

define do_this {
	$mule = "ipaddress_${name}"
	$donkey = inline_template("<%= scope.lookupvar(mule) %>")

	notify { "Found interface $donkey":; }

do_this { $ifs:; }

This will output:

$ sudo puppet net.pp 
notice: Found interface
notice: //Do_this[eth0]/Notify[Found interface]/message: defined 'message' as 'Found interface'
notice: Found interface
notice: //Do_this[eth1]/Notify[Found interface]/message: defined 'message' as 'Found interface'

Puppet Tips&Tricks: checking if a value is present in an array

Thursday, June 17th, 2010

I tend to create fairly ubiquitous defines, which allow for a lot of functionality. For that, it would be nice if you could tell puppet “if value X is present in array Y, do this”. I made it a feature request for puppet (#3871), which was luckily accepted and will be implemented in the next version of puppet. However, for the time being I needed this fixed. So I spend some time creating an ugly solution for this in older versions of puppet. Hope it helps someone!

# Setup additional services for this vhost, if any are required.
# Start with a general check to prevent a lot of work when it's
# not needed.
if $allow != "" {
           # FIXME Ideally we want something that simply does
           #  for i in allow do include $http::allow_$i
           # but that doesn't work (yet?).
           # W00t! That's going to be added in 2.6! #3871
           if inline_template("<%= allow.include?('php5') %>") == "true" {
                     include allow_php5
           if inline_template("<%= allow.include?('rewrite') %>") == "true" {
                     include allow_rewrite
           if inline_template("<%= allow.include?('ruby') %>") == "true" {
                     include allow_ruby
           if inline_template("<%= allow.include?('python') %>") == "true" {
                     include allow_python

Puppet Tips&Tricks: Variable variables

Monday, February 15th, 2010

This is the first post in a series of mine, while I’m reinventing our puppet modules. Puppet is the tool we use to maintain most machines in our care. We make it a point to share our modules as much as possible, so others might benefit from them.

Today I was working on our kbp_firewall module, which sets some variables that other modules can use via qualified variables. We differentiate between “protected”, “public” and “management” network interfaces on each host. These are always separate (virtual) interfaces, to make our life a little easier. I wanted to make both the interface and the IP address for these easily available, but I wanted most of it to happen automagically after puppet has determined which interface is connected to which network. I wanted to use the facter fact $ipaddress_(interface name), which is created for each interface, like for example $ipaddress_eth0.

Currently, there’s no way to use something like $ipaddress_${interface}, although I’ve been told on the #puppet IRC channel that this will be implemented in the next major version. So I had to create a more hackish solution. Inline_template to the rescue!

What I currently do:
$protected_ip = inline_template("<%= ipaddress_${protected_if} %>")

This will probably work in future versions too, so I feel comfortable implementing it like this. Once we’ve fully switched to the next major release, I can slowly work through the code to make it all a bit better readable.

Hope this helps someone!