I found a very ugly solution:
Step 1: create custom puppet fact on Icinga2 Master with a dirty perl script:
#!/usr/bin/env perl
use strict;
use warnings;
my $role_hostgroups = qx(curl -s -X GET http://PUPPETDBHOST:8080/pdb/query/v4/facts/role --data-urlencode 'query=["extract",["value"],["group_by","value"]]' | jq -r '.[]|.value');
my @role_hostgroups = sort map {"role_$_" } grep { $_ ne 'not_set' } split(/\n/,$role_hostgroups);
my $application_hostgroups = qx(curl -s -X GET http://PUPPETDBHOST:8080/pdb/query/v4/facts/application --data-urlencode 'query=["extract",["value"],["group_by","value"]]' | jq -r '.[]|.value');
my @application_hostgroups = sort map {"application_$_" } grep { $_ ne 'not_set' } split(/\n/,$application_hostgroups);
open (my $fact, ">", "/etc/facter/facts.d/icinga_role_application_hostgroups.yaml") or die "Kann /etc/facter/facts.d/icinga_role_application_hostgroups.yaml nicht schreiben: $!";
print $fact "icinga_role_application_hostgroups: [".join(",",@role_hostgroups,@application_hostgroups)."]\n";
close $fact;
That creates a fact:
[ "role_ROLE1", "role_ROLE2", ... "application_APP1", "application_APP2", ... ]
This script is startet by cron every 5 minutes, so new “roles” or “applications” are available a few minutes later.
Step 2: Add some code to our puppet manifests for the icing2 master:
icinga2_master::hostgroups { $::icinga_role_application_hostgroups: }
and in “icinga2_master/hostgroups.pp”:
define icinga2_master::hostgroups (
$hostgroup = $name,
)
{
icinga2::object::hostgroup { "${hostgroup}":
target => '/etc/icinga2/conf.d/hostgroups.conf',
assign => [
"${hostgroup} == role_ + host.vars.puppet_role",
"${hostgroup} == application_ + host.vars.puppet_application",
],
}
}
With the next puppet run it creates the hostgroups and assigns the hosts to it.
Conclusion: in german I would call it: “Von hinten durch die Brust ins Auge”, but at least it works…
I will try, if the fileshipper module can read CSV and create Hostgroups from that, so my perl script could just write the CSV. Or the “curl” could write it directly. I will try this…