79 lines
2.1 KiB
Perl
79 lines
2.1 KiB
Perl
|
#!/usr/bin/perl -w
|
||
|
|
||
|
package esmith;
|
||
|
|
||
|
use strict;
|
||
|
use Errno;
|
||
|
use esmith::DB::db;
|
||
|
use IO::File;
|
||
|
use English;
|
||
|
use JSON;
|
||
|
|
||
|
my $db_hosts = esmith::DB::db->open('hosts')
|
||
|
|| die( "Could not open e-smith db (" . esmith::DB::db->error . ")\n" );
|
||
|
my $db_configuration = esmith::DB::db->open('configuration')
|
||
|
|| die( "Could not open e-smith db (" . esmith::DB::db->error . ")\n" );
|
||
|
|
||
|
my @hostnames = ();
|
||
|
my %vars = ();
|
||
|
my %hostvars = ();
|
||
|
|
||
|
my $default_user = "amgmt";
|
||
|
my $ansible_ssh_private_key_file =
|
||
|
$db_configuration->get_prop( 'client-manager', 'ManagementPrivateKeyFile' )
|
||
|
|| '/home/amgmt/.ssh/id_rsa';
|
||
|
|
||
|
$vars{'configuration_timestamp'} =
|
||
|
qx/find \/etc\/ansible\/roles -type f -printf '%T@\n' | sort -r | head -n 1/;
|
||
|
chomp( $vars{'configuration_timestamp'} );
|
||
|
$vars{"ansible_ssh_private_key_file"} =
|
||
|
$db_configuration->get_prop( 'client-manager', 'ManagementPrivateKeyFile' );
|
||
|
|
||
|
my $domainname = $db_configuration->get_prop( 'DomainName', 'type' );
|
||
|
|
||
|
my @items = $db_hosts->get_all_by_prop('type' => 'local');
|
||
|
foreach my $item (@items) {
|
||
|
push @hostnames, $item->key . '.' . $domainname;
|
||
|
my $ansible_user = $item->prop('sshUser') || $default_user;
|
||
|
my %vars = (
|
||
|
'ansible_ssh_private_key_file' => $ansible_ssh_private_key_file,
|
||
|
'ansible_user' => $ansible_user
|
||
|
);
|
||
|
$hostvars{ $item->key . '.' . $domainname } = {%vars};
|
||
|
}
|
||
|
|
||
|
my %output = (
|
||
|
'all' => {
|
||
|
'hosts' => [@hostnames],
|
||
|
'vars' => {%vars},
|
||
|
'children' => ['clients','ungrouped'],
|
||
|
},
|
||
|
'_meta' => {
|
||
|
'hostvars' => {%hostvars}
|
||
|
},
|
||
|
'ungrouped' => [],
|
||
|
'clients' => [@hostnames]
|
||
|
);
|
||
|
|
||
|
my @groups = $db_hosts->get_all_by_prop('type' => 'group');
|
||
|
foreach my $group (@groups) {
|
||
|
my $members_str = $group->prop('members') || '';
|
||
|
my @members = split ' ', $members_str;
|
||
|
for my $member (@members) {
|
||
|
$member = $member . '.' . $domainname;
|
||
|
}
|
||
|
|
||
|
my %props = $group->props;
|
||
|
delete( $props{'members'} );
|
||
|
delete( $props{'type'} );
|
||
|
|
||
|
$output{ $group->key } = { 'hosts' => [@members], 'vars' => {%props} };
|
||
|
push $output{'all'}{'children'}, $group->key;
|
||
|
|
||
|
}
|
||
|
|
||
|
my $json = encode_json \%output;
|
||
|
print "$json\n";
|
||
|
|
||
|
exit 0;
|