常见问题:
这里我们将维护一个常见问题列表和答案:
为什么run命令不格式化输出:
run命令在数组上下文例将返回一个数组:
如果i只想打印标准输出的内容到终端,你可以强制在标量上下文中调用run命令。
[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" => "stop";
};
desc "df -h";
task "df -h",group =>"myservers", sub{
my @output = run "df -h";
say @output;
};
task "upload_file", group => "myservers", sub {
file "/etc/ntp.conf",
source => "ntp.conf"
};
[root@node01 my-first-rex-project]# rex "df -h"
[2017-04-25 14:14:18] WARN - Please use only the following characters for task names:
[2017-04-25 14:14:18] WARN - A-Z, a-z, 0-9 and _
[2017-04-25 14:14:18] WARN - Also the task should start with A-Z or a-z
[2017-04-25 14:14:18] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-04-25 14:14:18] INFO - Running task df -h on 192.168.137.3
Filesystem Size Used Avail Use% Mounted on/dev/sda3 19G 11G 7.1G 60% /tmpfs 497M 0 497M 0% /dev/shm/dev/sda1 194M 29M 155M 16% /boot
[2017-04-25 14:14:30] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]#
怎么获取当前任务运行在哪台服务器上?
[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" => "stop";
};
desc "df -h";
task "df -h",group =>"myservers", sub{
my @output = run "df -h";
say @output;
my $current_server = connection->server;
print "\$current_server=$current_server\n";
};
task "upload_file", group => "myservers", sub {
file "/etc/ntp.conf",
source => "ntp.conf"
};
[root@node01 my-first-rex-project]# rex "df -h"
[2017-04-25 14:17:30] WARN - Please use only the following characters for task names:
[2017-04-25 14:17:30] WARN - A-Z, a-z, 0-9 and _
[2017-04-25 14:17:30] WARN - Also the task should start with A-Z or a-z
[2017-04-25 14:17:30] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-04-25 14:17:30] INFO - Running task df -h on 192.168.137.3
Filesystem Size Used Avail Use% Mounted on/dev/sda3 19G 11G 7.1G 60% /tmpfs 497M 0 497M 0% /dev/shm/dev/sda1 194M 29M 155M 16% /boot
$current_server=192.168.137.3
[2017-04-25 14:17:41] INFO - All tasks successful on all hosts
怎么给任务传递参数?
rex
-H Execute a task on the given hosts (space delimited)
执行一个任务在一个给定的hosts(空格分割)
[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" => "stop";
};
desc "df -h";
task "df -h",group =>"myservers", sub{
my $parameters = shift;
my $parameter1_value = $parameters->{parameter1};
my $parameter2_value = $parameters->{parameter2};
my @output = run "ps -ef | grep $parameter1_value | grep $parameter2_value";
say @output;
my $current_server = connection->server;
print "\$current_server=$current_server\n";
};
task "upload_file", group => "myservers", sub {
file "/etc/ntp.conf",
source => "ntp.conf"
};
[root@node01 my-first-rex-project]# rex "df -h" --parameter1=root --parameter2=sshd
[2017-04-25 14:28:08] WARN - Please use only the following characters for task names:
[2017-04-25 14:28:08] WARN - A-Z, a-z, 0-9 and _
[2017-04-25 14:28:08] WARN - Also the task should start with A-Z or a-z
[2017-04-25 14:28:08] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-04-25 14:28:08] INFO - Running task df -h on 192.168.137.3
root 1211 1 0 02:05 ? 00:00:00 /usr/sbin/sshdroot 2884 1211 0 02:29 ? 00:00:00 sshd: root@pts/0 root 5629 1211 0 04:25 ? 00:00:00 sshd: root@pts/1 root 7903 1211 0 05:29 ? 00:00:00 sshd: root@notty root 7978 7903 0 05:29 ? 00:00:00 bash -c LC_ALL=C ; export LC_ALL; PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/pkg/bin:/usr/pkg/sbin; export PATH; ps -ef | grep root | grep sshd root 7987 7978 0 05:29 ? 00:00:00 grep sshd
$current_server=192.168.137.3
[2017-04-25 14:28:19] INFO - All tasks successful on all hosts
rex 传参
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:ssh 认证
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Vue3 路由传参
Vue3 路由传参
Vue路由传参 query params