In regard to: [Puppet Users] Re: how to resolve hostnames to IP addresses...:
> There's no way to do DNS lookups in a template with stock Puppet,
Thanks for confirming what my futile research seemed to be implying. :-)
> but you can pretty easily write a custom function to do that for you. By
> default, Resolv will use the settings in /etc/resolv.conf, so as long as
> your nameservers are set up correctly on the puppetmaster, you shouldn't
> run into any problems.
>
> Try plopping something like this into lib/puppet/parser/functions/get_ip_addr.rb in your module's directory:
> <a href="
https://gist.github.com/3308273">
https://gist.github.com/3308273</a>
>
> require 'resolv'
>
> module Puppet::Parser::Functions
> newfunction(:get_ip_addr, :type => :rvalue) do |args|
> # Super sexy regex to match valid IPs
> ip_addr_re = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
> hostname = args[0].strip
> if hostname =~ ip_addr_re then return hostname end
> begin
> Resolv::DNS.open { |dns| return dns.getaddress hostname }
> rescue Resolv::ResolvError
> return ''
> end
> end
> end
>
>
> Then you can call it in your template file as ```scope.function_get_ip_addr``` and it will either return the IP address for a hostname, the unchanged IP address for a valid IP address, and an empty string otherwise.
>
> I'm not sure what your hiera() calls are supposed to return, but assuming $webfarm ends up as a hash with keys http_servers and https_servers containing an array of hashes with keys host and port, you could do something like this:
>
> <% webfarm = scope.lookupvar 'foo::data::webfarm' %>
> <% webfarm['http_servers'].each do |server| %>
> <%= scope.function_get_ip_addr server['host'] %>:<%= server['port'] %>
> <% end %>
Thank you for the excellent example! There are several things here that
I'm going to have to ponder for a while. I've seen scope.lookupvar before
but never personally had to use it, but the scope.function_<functionname>
is new to me.
Time to do some more reading and research, but what you've provided really
helps point me in the right direction.
Tim