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:
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.
Comments
Olafur Osvaldsson (not verified)
Fri, 03/30/2012 - 06:54
Permalink
Single interface hosts
When I was implementing this I ran into errors when the template was applied to hosts with only one interface, the checking for comma in the string worked to resolve the issue:
gabster
Sun, 04/01/2012 - 01:14
Permalink
thanks
oh, thanks for pointing this out. I must admit that I rarely encounter hosts with only one interface, and I was expecting split() to just build a list of one element if there were no commas present..
I'll have to test this out with a VM.
Sadly, though, the ruby documentation doesn't describe what happens in the case where the pattern is not found in the string: http://ruby-doc.org/core-1.9.3/String.html#split-method
gabster
Sun, 04/01/2012 - 01:21
Permalink
hum hum ..
well this is weird. I just tested out string.split() with iRuby, and it behaved exactly as I had expected. I'm using ruby 1.8.7 :
Olafur, can you reply by pasting the error you get on single-interface machines?
Olafur Osvaldsson (not verified)
Mon, 04/02/2012 - 05:26
Permalink
I got no error in the erb
I got no error in the erb template, it just did not execute the steps within the do loop, but when I tried this again today I got no error and it returned a single value as expected.
I really don't know what I might be doing different since before the weekend, maybe I just needed the extra rest? ;)
So, you can just ignore that original comment since it seems to work just fine.
/Oli
P.S. This comment system really needs an email or some notification method...
gabster
Sat, 04/07/2012 - 01:16
Permalink
oh, good then
ah! then I'm glad you got this to work. :)
re: to p.s.: yess! I know, it's annoying not to have e-mail notifications! I've asked colleagues about how to implement this with drupal and they told me about some options. So it's in my todo list.
Add new comment