Merge pull request #201 from rhansen/group_hosts_by

`group_hosts_by` improvements
This commit is contained in:
Sandro 2020-06-29 09:40:11 +02:00 committed by GitHub
commit bfdf9454f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2369,13 +2369,11 @@ sub extract_ipv6 {
###################################################################### ######################################################################
sub group_hosts_by { sub group_hosts_by {
my ($hosts, $attributes) = @_; my ($hosts, $attributes) = @_;
my %attrs = (map({ ($_ => 1) } @$attributes), 'wantip' => 1);
my @attrs = sort(keys(%attrs));
my %groups = (); my %groups = ();
foreach my $h (@$hosts) { foreach my $h (@$hosts) {
my @keys = (@$attributes, 'wantip'); my $sig = join(',', map({ sprintf("%s=%s", $_, $config{$h}{$_} // '') } @attrs));
map { $config{$h}{$_} = '' unless exists $config{$h}{$_} } @keys;
my $sig = join(',', map { "$_=$config{$h}{$_}" } @keys);
push @{$groups{$sig}}, $h; push @{$groups{$sig}}, $h;
} }
return %groups; return %groups;