task($name [, @servers], $funcref)
这个函数用于创建一个新任务.


key_auth;
group myservers => "192.168.137.3";

 task "mytask", sub {
   say run "hostname";
 };
   
[root@node01 Rex]# rex mytask
[2017-05-06 16:45:19] INFO - Running task mytask on <local>
node01

创建指定机器的任务:

 task "mytask","192.168.137.3", sub {
   say run "hostname";
 };
   
[root@node01 Rex]# rex mytask
[2017-05-06 16:47:09] INFO - Running task mytask on 192.168.137.3
node2
[2017-05-06 16:47:21] INFO - All tasks successful on all hosts
[root@node01 Rex]# 


你也可以绑定多个机器,只需要用","分割机器名

创建指定机器组的任务。
可以用group函数定义一个机器组。



 task "mytask","192.168.137.3","192.168.137.2", sub {
   say run "hostname";
 };
   
[root@node01 Rex]# rex mytask
[2017-05-06 16:52:29] INFO - Running task mytask on 192.168.137.3
node2
[2017-05-06 16:52:41] INFO - Running task mytask on 192.168.137.2
node01
[2017-05-06 16:52:52] INFO - All tasks successful on all hosts



创建指定机器组的任务:
group "allserver" => "192.168.137.3","192.168.137.2";

 task "mytask2",group=>"allserver", sub {
   say run "hostname";
 };
   
[root@node01 Rex]# rex mytask2
[2017-05-06 16:54:27] INFO - Running task mytask2 on 192.168.137.3
node2
[2017-05-06 16:54:39] INFO - Running task mytask2 on 192.168.137.2
node01
[2017-05-06 16:54:50] INFO - All tasks successful on all hosts
[root@node01 Rex]# 


desc($description)

设置任务描述。



group($name, @servers)

如果你想一次在一组机器执行任务,可以用group函数定义一个机器组,非常方便:

 
 group "servergroup", "192.168.137.2", "192.168.137.3","192.168.137.4";

 task "mytask3",group=>"servergroup", sub {
   say run "hostname";
 };
[root@node01 Rex]# rex mytask3
[2017-05-06 19:07:15] INFO - Running task mytask3 on 192.168.137.2
node01
[2017-05-06 19:07:27] INFO - Running task mytask3 on 192.168.137.3
node2
[2017-05-06 19:07:38] INFO - Running task mytask3 on 192.168.137.4
[2017-05-06 19:07:47] WARN - Can't authenticate against 192.168.137.4 (unable to establish master SSH connection: master process exited unexpectedly)


batch($name, @tasks)
用 batch 函数可以在批处理中调用任务。


 batch "name", "task1", "task2", "task3";
你也可以在终端中用-b参数. rex -b 批处理名称



 task "mytask3",group=>"servergroup", sub {
   say run "/sbin/ifconfig -a | grep inet";
 };
[root@node01 Rex]# rex mytask3
[2017-05-06 19:10:53] INFO - Running task mytask3 on 192.168.137.2
          inet addr:192.168.137.2  Bcast:192.168.137.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe29:8eac/64 Scope:Link          inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host
[2017-05-06 19:11:04] INFO - Running task mytask3 on 192.168.137.3
          inet addr:192.168.137.3  Bcast:192.168.137.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe14:8fc1/64 Scope:Link          inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host
[2017-05-06 19:11:16] INFO - Running task mytask3 on 192.168.137.4
[2017-05-06 19:11:25] WARN - Can't authenticate against 192.168.137.4 (unable to establish master SSH connection: master process exited unexpectedly)
[2017-05-06 19:11:25] ERROR - 1 out of 3 task(s) failed:
[2017-05-06 19:11:25] ERROR - 	mytask3 failed on 192.168.137.4
[2017-05-06 19:11:25] ERROR - 		Couldn't authenticate against 192.168.137.4. It may be caused by one or more of:
[2017-05-06 19:11:25] ERROR - 		 - wrong username, password, key or passphrase
[2017-05-06 19:11:25] ERROR - 		 - changed remote host key
[2017-05-06 19:11:25] ERROR - 		 - root is not permitted to login over SSH
[2017-05-06 19:11:25] ERROR - 		 at /usr/local/perl/lib/site_perl/5.22.1/Rex/TaskList/Base.pm line 342.
[root@node01 Rex]# 


 group "servergroup", "192.168.137.2", "192.168.137.3","192.168.137.4";

 task "mytask3",group=>"servergroup", sub {
   say run "/sbin/ifconfig -a | grep inet";
 };


batch "batchserver","mytask2","mytask3";

[root@node01 Rex]# rex -b batchserver
[2017-05-06 19:12:39] INFO - Running task mytask2 on 192.168.137.3
Linux node2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[2017-05-06 19:12:50] INFO - Running task mytask2 on 192.168.137.2
Linux node01 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[2017-05-06 19:13:01] INFO - Running task mytask3 on 192.168.137.2
          inet addr:192.168.137.2  Bcast:192.168.137.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe29:8eac/64 Scope:Link          inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host
[2017-05-06 19:13:13] INFO - Running task mytask3 on 192.168.137.3
          inet addr:192.168.137.3  Bcast:192.168.137.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe14:8fc1/64 Scope:Link          inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host
[2017-05-06 19:13:24] INFO - Running task mytask3 on 192.168.137.4
[2017-05-06 19:13:33] WARN - Can't authenticate against 192.168.137.4 (unable to establish master SSH connection: master process exited unexpectedly)
[2017-05-06 19:13:33] ERROR - 1 out of 5 task(s) failed:
[2017-05-06 19:13:33] ERROR - 	mytask3 failed on 192.168.137.4
[2017-05-06 19:13:33] ERROR - 		Couldn't authenticate against 192.168.137.4. It may be caused by one or more of:
[2017-05-06 19:13:33] ERROR - 		 - wrong username, password, key or passphrase
[2017-05-06 19:13:33] ERROR - 		 - changed remote host key
[2017-05-06 19:13:33] ERROR - 		 - root is not permitted to login over SSH
[2017-05-06 19:13:33] ERROR - 		 at /usr/local/perl/lib/site_perl/5.22.1/Rex/TaskList/Base.pm line 342.
[root@node01 Rex]# 


do_task($task)
在其他任务调用这个 $task。具体被执行的服务器就是在这个$task 中定义好的那些。


 task "task1", "server1", sub {
   say "Running on server1";
   do_task "task2";
 };
 
 task "task2", "server2", sub {
   say "Running on server2";
 };

 task "task1", "192.168.137.2", sub {
   say run "hostname";
   do_task "task2";
 };
 
 task "task2", "192.168.137.3", sub {
   say run "hostname";
 };

batch "batchserver","mytask2","mytask3";

[root@node01 Rex]# rex task1
[2017-05-06 19:56:31] INFO - Running task task1 on 192.168.137.2
node01
[2017-05-06 19:56:43] INFO - Running task task2 on 192.168.137.3
node2
[2017-05-06 19:57:04] INFO - All tasks successful on all hosts




parallelism($count)
在指定机器上并行执行任务。 $count 指定线程数。



needs($package [, @tasks])


使用needs 你可以定义任务间的依赖,

被依赖的任务将在和调用自己的任务完全相同的服务器上执行。



依赖 所有的任务在包MyPkg,所有的任务会被请求 在server1