Accessing data in a hash

202 views
Skip to first unread message

Chip

unread,
Feb 4, 2011, 9:26:52 AM2/4/11
to Puppet Users
I have a hash with network information structured as such:

$networkinfo = {
'machine1.domain.local' => { eth0 => { ip => '192.168.1.50',
mask => '255.255.255.0', gw => '192.168.1.254' },
eth1 => { ip => '192.168.2.68', mask =>
'255.255.255.0' } },
'machine2.domain.local' => { eth0 => { ip => '192.168.1.51',
mask => '255.255.255.0', gw => '192.168.1.254' },
eth1 => { ip => '192.168.2.74', mask =>
'255.255.255.0' } }
}

In a function I need to access data in this hash without hard coding
everything.

$ipaddress = $networkinfo["$fqdn"]"[$iface"]['ip']

this fails with syntax errors unless specify everything such as

$ipaddress = $networkinfo['machine1.domain.local']['eth2']['ip']

What is the proper syntax to access data within the hash?



Felix Frank

unread,
Feb 8, 2011, 4:28:14 AM2/8/11
to puppet...@googlegroups.com

Interesting - in a short test, I don't even get your final example
working. (This is 2.6.2.)

However, this seems to be a problem with the syntax of nested hashes, as
this Works For Me:

$all = $networkinfo[$nodename][$iface]
$address = $all[ip]
notify { "check": message => "retrieved address $address" }

You may wish to raise a possible bug.

HTH,
Felix

Reply all
Reply to author
Forward
0 new messages