Sometimes, it's relevant to iterate through all ip adrresses on a host to emit configuration for each one. Facter has a useful bunch of facts for this:

  • interfaces => lo,eth0,eth0_0,eth1
  • ipaddress_lo =>
  • ipaddress_eth0 =>
  • ipaddress_...
  • ... and so on

This is nice, but how can you use this in an ERB template?

Answer: use scope.lookupvar()

Here's a snippet for emitting one "Allow" line for each of the server's IP addresses in an Apache vhost:

<%- interfaces.split(',').each do |int|
  if has_variable?("ipaddress_#{int}") then %>
               Allow from <%= scope.lookupvar("ipaddress_#{int}") %>
<%- end
end %>

if you're wondering why I'm using has_variable?(), too, that's because sometimes interfaces are not assigned any IP address. In such cases, there is no corresponding $ipaddress_* fact, so it would be an error to lookup a value for that.