认证:
前面的示例中我们演示了怎么通过用户名密码登陆,其实也可以用秘钥认证
使用密钥认证只需要在Rexfile里定义你的公钥和私钥既即可:
user "my-user";
private_key "/home/user/.ssh/id_rsa"; ##私钥
public_key "/home/user/.ssh/id_rsa.pub"; ##公钥
key_auth;
[root@node01 my-first-rex-project]# cat Rexfile
use Rex -feature => ['1.0'];
user "root";
private_key "/root/.ssh/id_rsa"; ##私钥
public_key "/root/.ssh/id_rsa.pub"; ##公钥
key_auth;
group myservers => "192.168.137.3";
desc "Get the uptime of all servers";
task "uptime", group => "myservers", sub {
my $output = run "uptime";
say $output;
};
desc "Start Mysql Service";
task "start_mysql", group => "myservers", sub {
service "mysql" => "start";
};
[root@node01 my-first-rex-project]# rex uptime
[2017-04-29 10:16:24] INFO - Running task uptime on 192.168.137.3
01:18:43 up 21:46, 1 user, load average: 0.00, 0.00, 0.00
[2017-04-29 10:16:35] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]#