Advertisement
pulser_777

Untitled

Nov 17th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.64 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. use FindBin;
  6. use JSON qw/encode_json/;
  7.  
  8. my $hostinfo = require $FindBin::Bin . '/hosts.dat';
  9.  
  10. my $groups;
  11. foreach my $host (keys %{$hostinfo})    {
  12.     foreach my $role (@{$hostinfo->{$host}{roles}}) {
  13.         $groups->{$role}++;
  14.     }
  15. }
  16.  
  17. my $inventory;
  18.  
  19. foreach my $role (keys %{$groups})  {
  20.     $inventory->{$role}{hosts} = [];
  21.     foreach my $host (keys %{$hostinfo})    {
  22.         push(@{$inventory->{$role}{hosts}}, $host) if grep { $_ eq $role } @{$hostinfo->{$host}{roles}};
  23.     }
  24. }
  25.  
  26. foreach my $host (keys %{$hostinfo})    {
  27.     $inventory->{_meta}{hostvars}{$host} = $hostinfo->{$host}{vars};
  28. }
  29.  
  30. print encode_json($inventory);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement