Skip to content

Instantly share code, notes, and snippets.

@krimdomu
Last active December 16, 2015 08:29
Show Gist options
  • Save krimdomu/5406176 to your computer and use it in GitHub Desktop.
Save krimdomu/5406176 to your computer and use it in GitHub Desktop.
authentication different per server
use Rex -feature => '0.40';
use String::Escape 'string2hash';
user "root";
group mygroup => "srv02", "srv01", "srv03";
configure_auth("server.lst");
task "test", group => "mygroup", sub {
my $output = run "uptime";
say $output;
};
sub configure_auth {
my ($file) = @_;
open(my $fh, "<", $file) or die($!);
while(my $line = <$fh>) {
chomp $line;
($line =~ /^#|^;|^\s*$/) && (next);
my ($srv, $option_string) = split(/ /, $line, 2);
if($option_string) {
my %option = string2hash($option_string);
my %groups = Rex::Group->get_groups;
for my $group_name (keys %groups) {
my $group = $groups{$group_name};
map {
if($_ eq $srv) {
$_->set_auth(%option);
}
} @{ $group };
}
}
}
close($fh);
}
1;
# -----------------------------------------------------------------------------------
# server.lst file:
srv01 user=foo password=abcd
srv02 user=bar password=dcsx
srv03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment