How to convert puppet hash to another?

Viewed 18

I have a puppet hash as below

$oldHash
{
  server1 => {port1 => {timeout=3}, port2 => {timeout=2}}, 
  server2 => {port1 => {timeout=2}, port2 => {timeout=2}}
}

Would like to take out the "timeout=x" part, and get a new hash as below

$newHash
{
  server1 => [port1, port2], 
  server2 => [port1,port2]
}

I tried this

$newHash = $oldHash.map |$server, $ports| {
    {$server =>  keys($ports)}
  }

But the newHash becomes

[{server1 => [port1, port2]}, {server2 => [port1, port2]}]

Thanks!

1 Answers

The code you implemented produces an array of hashes. You can convert it to a hash with Hash.new()

This code:

$new_hash = $old_hash.map |$server, $ports| {
  {$server =>  keys($ports)}
}

notice(Hash.new($new_hash))

will output:

Notice: Scope(Class[main]): {{server1 => [port1, port2]} => {server2 => [port1, port2]}}
Related