This message was deleted.
# puppet
s
This message was deleted.
s
I might be misunderstanding what you're trying to do, but I think
$found_services = lookup('servicelist').values
would do the trick
If you're worried about duplicate values, then tack on
.unique
r
so services_installed is really a fact $facts['service_installed'] with the above output. but what I am trying to do is loop through all the services_installed, and return the category of service it is, based on a defined servicelist in hieradata.
and handle it via regex matching
sorta like $services_installed.each |$service| { pattern match servicelist create array }
n
@rismoney What do you want to result to be? An array of service categories?
r
yes
s
Maybe something like
Copy code
$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...
n
ooh, with the way the data is structured, it's a good fit for the
in
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:
Copy code
$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:
Copy code
$service_categories = lookup('servicelist', Hash, {}).filter |$svc_regex, $_svc_category| {
  Regexp($svc_regex) in $facts['services_installed']
}.values.unique
🙌🏼 1
r
will give it a try and report back
tyvm!
this is great! With this, I am able to craftily tag my package resource used for monitoring, by building a category table in hiera. This subsequently gets picked up by service discovery, and completes a really nice solution. Kudos to y'all for the tips.
@natemccurdy this works fantastic. thank you so much!
🍻 1
thank you skylar as well!
🎊 1