Slackbot
05/30/2023, 8:47 PMSkylar Thompson
05/30/2023, 8:49 PM$found_services = lookup('servicelist').values
would do the trickSkylar Thompson
05/30/2023, 8:49 PM.unique
rismoney
05/30/2023, 9:02 PMrismoney
05/30/2023, 9:02 PMrismoney
05/30/2023, 9:04 PMnatemccurdy
05/30/2023, 9:29 PMrismoney
05/30/2023, 9:30 PMSkylar Thompson
05/30/2023, 9:38 PM$found_services = $services_installed.reduce([]) | $acc,$service | {
$mapped_services = $servicelist.filter | $service_category | {
$service_category[0] =~ $service[0] }
$acc + pick_default($mapped_services.values[0],[])
}
Though one thing to be aware of is you could have multiple matches from the servicelist
mapping, or potentially none.
Haven't tested it so probably should treat more like pseudo-code...natemccurdy
05/30/2023, 9:59 PMin
operator's ability to match regexs to hash keys.
https://www.puppet.com/docs/puppet/7/lang_expressions.html#lang_exp_comparison_operators-comparison-in
So an optimization of my first example from above could be:
$servicelist = lookup('servicelist', Hash, {})
$service_categories = $facts['servicelist'.map |$svc_regex, $svc_category| {
if Regexp($svc_regex) in $facts['services_installed'] {
$svc_category
}
}.unique.filter |$val| {$val =~ NotUndef}
But if I flip the iteration around a bit, I can simplify that down even more into just:
$service_categories = lookup('servicelist', Hash, {}).filter |$svc_regex, $_svc_category| {
Regexp($svc_regex) in $facts['services_installed']
}.values.unique
rismoney
05/30/2023, 10:01 PMrismoney
05/30/2023, 10:01 PMrismoney
05/31/2023, 3:15 AMrismoney
06/02/2023, 9:01 PMrismoney
06/02/2023, 9:01 PM