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
On Thursday, August 9, 2012 1:38:14 PM UTC-7, Tim Mooney wrote:
Environment: puppet 2.7.14 on both master and all clients. We're also
using puppetlabs-stdlib and hiera, if that matters.
I know this is really more of a ruby/erb question, but I've been searching
for a couple hours and haven't turned up anything relatively close to what
I'm trying to do, and I'm hoping someone here has had to do this and can
provide a suggestion for how to proceed.
I'm generating a configuration file from a template. The configuration
file will need to have IP addresses in it, but I would like to be able to
use either hostnames or IP adresses in the puppet config. This means
that I need to be able to resolve the hostnames and turn them into IP
addresses, probably in the template itself.
Basically, given something like this in puppet:
class foo::data {
$webfarm = {
http_servers => hiera('webfarm_http_servers', [
{
host => 'foo1.example.com',
port => '80',
},
{
host => 'foo2.example.com',
port => '80',
},
{
host => 'foo3.example.com',
port => '80',
},
]),
https_servers => hiera('webfarm_https_servers', [
{
host => 'foo1.example.com',
port => '443',
},
{
host => 'foo22.example.com',
port => '443',
},
{
host => 'foo99.example.com',
port => '443',
},
]),
}
}
I need my template to iterate over the http_servers and https_servers
arrays and resolve the values for the host key for each element.
Anyone have an example of how to do this in a template?
Thanks,
Tim
--
Tim Mooney tim.moo...@ndsu.edu
Enterprise Computing & Infrastructure 701-231-1076 (Voice)
Room 242-J6, IACC Building 701-231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164
On Thursday, August 9, 2012 1:38:14 PM UTC-7, Tim Mooney wrote:
Environment: puppet 2.7.14 on both master and all clients. We're also
using puppetlabs-stdlib and hiera, if that matters.
I know this is really more of a ruby/erb question, but I've been searching
for a couple hours and haven't turned up anything relatively close to what
I'm trying to do, and I'm hoping someone here has had to do this and can
provide a suggestion for how to proceed.
I'm generating a configuration file from a template. The configuration
file will need to have IP addresses in it, but I would like to be able to
use either hostnames or IP adresses in the puppet config. This means
that I need to be able to resolve the hostnames and turn them into IP
addresses, probably in the template itself.
Basically, given something like this in puppet:
class foo::data {
$webfarm = {
http_servers => hiera('webfarm_http_servers', [
{
host => 'foo1.example.com',
port => '80',
},
{
host => 'foo2.example.com',
port => '80',
},
{
host => 'foo3.example.com',
port => '80',
},
]),
https_servers => hiera('webfarm_https_servers', [
{
host => 'foo1.example.com',
port => '443',
},
{
host => 'foo22.example.com',
port => '443',
},
{
host => 'foo99.example.com',
port => '443',
},
]),
}
}
I need my template to iterate over the http_servers and https_servers
arrays and resolve the values for the host key for each element.
Anyone have an example of how to do this in a template?
Thanks,
Tim
--
Tim Mooney tim.moo...@ndsu.edu
Enterprise Computing & Infrastructure 701-231-1076 (Voice)
Room 242-J6, IACC Building 701-231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164
--
Tim Mooney tim.moo...@ndsu.edu
Enterprise Computing & Infrastructure 701-231-1076 (Voice)
Room 242-J6, IACC Building 701-231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164
--
You received this message because you are subscribed to the Google Groups "Puppet
Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.