Thanks to Tomas!  His answer got me onto a useful track, shared here.

In my case Gethostbyname was returning a bad ip, but I could 
use Socket.getaddrinfo, like this:
<% ipStr = Socket.getaddrinfo('INST-STAGE-HAPROXY-A',nil)[0][3] %><%= ipStr 
%>

or with the environment parameterised;
<% ipStr = 
Socket.getaddrinfo('INST-'+scope.lookupvar('::config::uc_env')+'-HAPROXY-A',nil)[0][3]
 
%><%= ipStr %>

Other useful pointers;

You can test the ruby functions
(yum install irb)
$ irb

2.1.2 :001 > require 'socket'

 => true 

2.1.2 :002 > Socket.getaddrinfo('localhost',nil)

 => [["AF_INET6", 0, "::1", "::1", 30, 2, 17], ["AF_INET6", 0, "::1", 
"::1", 30, 1, 6], ["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 2, 17], 
["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 1, 6], ["AF_INET", 0, 
"127.0.0.1", "127.0.0.1", 2, 2, 17], ["AF_INET", 0, "127.0.0.1", 
"127.0.0.1", 2, 1, 6]] 

2.1.2 :004 > exit

(yum remove irb)


More ruby functions here: 
http://ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html#method-c-gethostbyname

On Friday, June 21, 2013 at 6:29:25 PM UTC+10, Tomas wrote:
>
> It is not so complicated as others wrote here.
>
> Here is an example how I use it:
>
> allowed_hosts=<% for nagios_server in nagios_servers %><% ipInt = 
> Socket.gethostbyname(nagios_server)[3] %><%= "%d.%d.%d.%d" % [ipInt[0], 
> ipInt[1], ipInt[2], ipInt[3]] %>,<% end %>
>
>
>
> Dne čtvrtek, 9. srpna 2012 22:38:14 UTC+2 Tim Mooney napsal(a):
>>
>>
>> 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.m...@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 unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/00523c03-1b86-48d9-8ec9-80fcfa39b958%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to