Guest User

Untitled

a guest
May 19th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use YAML qw( Dump );
  4. use DBI;
  5.  
  6. my $hostname = shift || die "No hostname passed";
  7.  
  8. $hostname =~ /^(\w+)\.(\w+)\.(\w{3})$/
  9. or die "Invalid hostname: $hostname";
  10.  
  11. my ( $host, $domain, $net ) = ( $1, $2, $3 );
  12.  
  13. # MySQL Configuration
  14. my $data_source = "dbi:mysql:database=puppet;host=localhost";
  15. my $username = "puppet";
  16. my $password = "password";
  17.  
  18. # Connect to the server
  19. my $dbh = DBI->connect($data_source, $username, $password)
  20. or die $DBI::errstr;
  21.  
  22. # Build the query
  23. my $sth = $dbh->prepare( qq{SELECT class FROM nodes WHERE node = '$hostname'})
  24. or die "Can't prepare statement: $DBI::errstr";
  25.  
  26. # Execute the query
  27. my $rc = $sth->execute
  28. or die "Can't execute statement: $DBI::errstr";
  29.  
  30. # Set parameters
  31. my %parameters = (
  32. puppet_server => "puppet.$domain.$net"
  33. );
  34.  
  35. # Set classes
  36. my @class;
  37. while (my @row=$sth->fetchrow_array)
  38. { push(@class,@row) }
  39.  
  40. # Check for problems
  41. die $sth->errstr if $sth->err;
  42.  
  43. # Disconnect from database
  44. $dbh->disconnect;
  45.  
  46. # Print the YAML
  47. print Dump( {
  48. classes => \@class,
  49. parameters => \%parameters,
  50. } );
Add Comment
Please, Sign In to add comment