备忘单:
执行一个命令解析它的输出:
run($command, %options)
运行一个远程命令返回它的输出
task "test01", sub {
my $out = run "pwd",
cwd=>'/home/mqm/perl/';
say $out;
};
cmd -- 命令的工作目录
only_if - Executes the command only if the condition completes successfully.
执行命令只有条件成功,才会执行run
[root@node01 my-first-rex-project]# rex -H 192.168.137.2 test02
[2019-06-18 12:55:02] INFO - Running task test02 on 192.168.137.2
$VAR1 = {
'auto_die' => 0
};
idd: Command not found.error-------------------
127
-------------------
[2019-06-18 12:55:02] INFO - All tasks successful on all hosts
unless - Executes the command only unless the condition completes successfully.
[root@node01 my-first-rex-project]# rex -H 192.168.137.2 test02
[2019-06-18 12:57:21] INFO - Running task test02 on 192.168.137.2
$VAR1 = {
'auto_die' => 0
};
idd: Command not found.error$VAR1 = {
'auto_die' => undef,
'unless' => 'idd'
};
/root
[2019-06-18 12:57:22] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]#
task "task03", sub {
run "extract-something",
command => "pwd",
only_notified => TRUE;
};
If you only want to run a command in special cases, you can queue the command
and notify it when you want to run it.
task "prepare", sub {
run "extract-something",
command => "tar -C /foo -xzf /tmp/foo.tgz",
only_notified => TRUE;
# some code ...
notify "run", "extract-something"; # now the command gets executed
};