Last active
April 1, 2022 11:14
-
-
Save MikaelSmith/14bfddd1ebe5283defc83f98e3c6b410 to your computer and use it in GitHub Desktop.
Transforming hash in Puppet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$data = [ | |
{ | |
"certname" => "server1", | |
"parameters" => { | |
"port" => 1234, | |
"job" => "job1" | |
} | |
}, | |
{ | |
"certname" => "server2", | |
"parameters" => { | |
"port" => 5678, | |
"job" => "job1" | |
} | |
}, | |
{ | |
"certname" => "server3", | |
"parameters" => { | |
"port" => 987, | |
"job" => "job2" | |
} | |
} | |
] | |
$output = $data.reduce({}) |$memo, $entry| { | |
# Group entries by job | |
$job = $entry['parameters']['job'] | |
$address = "${$entry['certname']}:${$entry['parameters']['port']}" | |
if $memo[$job] { | |
# Have to merge into the old hash because values are immutable in Puppet | |
$memo + { $job => $memo[$job] + [$address] } | |
} else { | |
$memo + { $job => [$address] } | |
} | |
}.map |$job, $entries| { | |
{ | |
'job' => $job, | |
'entries' => $entries | |
} | |
} | |
notice($output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment