Running command in server

124 views
Skip to first unread message

pablo.f...@cscs.ch

unread,
Mar 5, 2012, 5:35:40 AM3/5/12
to Puppet Users
Dear Puppet users,

I'm quite new to Puppet, and I know less about ruby. That's probably
why I have this question, hopefully you could answer it. I have
google'd quite a lot about it, without success:

I install nodes on a private subnet, and they get their FQDN from a
local DHCP server, so the $hostname variable is correct. But then I
want to make puppet configure the machine's interface with the public
IP address, and that's known by the production DNS server (not
accessible from the client on the private subnet).

Puppet Master has access to the production DNS, so I thought I could
run a template (they run in the master, right?) to get the right IP,
and pass it on to the puppet client. So I specify this inside the node
{}:

$new_ip=inline_template("<%= system(\"host $hostname|awk '{print \
$4}'\") -%>")
notify { "New IP: $new_ip": }

But I get:
notice: New IP: true

The weird thing is that the command is actually running well. If I run
puppetmasterd by hand, with --no-daemonize, I can see the IP being
written to stdout on the masterd. So, it seems the problem is that the
template is not capturing the output of the system command.

Do you know what am I doing wrong? How do I capture the system()
output?

Thanks!
Pablo

Stig Sandbeck Mathisen

unread,
Mar 5, 2012, 8:49:00 AM3/5/12
to puppet...@googlegroups.com
"pablo.f...@cscs.ch" <pablo.f...@cscs.ch> writes:

> Do you know what am I doing wrong? How do I capture the system()
> output?

Try using %x{} instead of system. The system() function returns the
return code of the process, not the output.

Oh, and "chomp" the extra whitespace off the output. If you're new to
ruby, you can test ruby code in "irb", interactive ruby.

irb> %x{hostname --fqdn}.chomp
=> "dagon.fnord.no"

--
Stig Sandbeck Mathisen
Any sufficiently advanced incompetence
is indistinguishable from malice

Pablo Fernandez

unread,
Mar 5, 2012, 9:22:29 AM3/5/12
to puppet...@googlegroups.com

Hi,

 

> Try using %x{} instead of system. The system() function returns the

> return code of the process, not the output.

 

> irb> %x{hostname --fqdn}.chomp

> => "dagon.fnord.no"

 

That worked, thanks!

 

For the record, this is how it looks like:

 

$new_ip=inline_template("<%= %x{host $hostname|awk '{print \$4}'}.chomp %>")

notify { "New IP: $new_ip": }

 

Kind regards,

Pablo

zed_0xff

unread,
Dec 19, 2013, 9:39:25 AM12/19/13
to puppet...@googlegroups.com
Better way (without executing a process):

$new_ip=inline_template("<%= Resolv::DNS.new.getaddress('www.ru').to_s -%>")

or may be simple $ipaddress fact is suitable for you?
Reply all
Reply to author
Forward
0 new messages