Puppet Tips&Tricks: Variable variables

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!

Tags: , , , , , ,


3 Responses to “Puppet Tips&Tricks: Variable variables”

  1. Sam Bashton says:

    &lt%= instance_variable_get(“@ipaddress_#{protected_if}”) %>

  2. Sam Bashton says:

    is a slightly less hackish way of achieving the same result.

  3. colinho says:

    Awesome! Just the hack I was looking for, cheers mate.

Kumina designs, builds, operates and supports Kubernetes solutions that help companies thrive online. As Certified Kubernetes Service Partner, we know how to build real solutions.