RexCommandsFile

RexCommandsFile - Transparent File Manipulation

传输文件模块:

 task "read-passwd2", "192.168.137.3", sub {
   say cat "/etc/passwd";
 };
[root@node01 Rex]# rex "read-passwd2"
[2017-05-10 07:37:45] WARN - Please use only the following characters for task names:
[2017-05-10 07:37:45] WARN -   A-Z, a-z, 0-9 and _
[2017-05-10 07:37:45] WARN - Also the task should start with A-Z or a-z
[2017-05-10 07:37:45] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-05-10 07:37:45] INFO - Running task read-passwd2 on 192.168.137.3
root:x:0:0:root:/root:/bin/bash


导出函数:

template($file, @params)

解析模板然后返回内容
 task "mytask4",sub {
   my $content = template("/root/vhosts.tpl",
              name => "test.lan",
              webmaster => 'webmaster@test.lan');
  say $content;
};
[root@node01 Rex]# cat /root/vhosts.tpl 
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
cccccccccccccc
[root@node01 Rex]# rex mytask4
[2017-05-10 09:54:27] WARN - Please use only the following characters for task names:
[2017-05-10 09:54:27] WARN -   A-Z, a-z, 0-9 and _
[2017-05-10 09:54:27] WARN - Also the task should start with A-Z or a-z
[2017-05-10 09:54:27] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-05-10 09:54:27] INFO - Running task mytask4 on <local>
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
cccccccccccccc

[2017-05-10 09:54:38] INFO - All tasks successful on all hosts
[root@node01 Rex]# 




[root@node01 Rex]# vim /usr/local/perl/lib/site_perl/5.22.1/Rex/Commands/File.pm


sub template {
   print "11111111111112222334414
";
  my ( $file, @params ) = @_;
  my $param;


file($file_name, %options)
这个函数是 install file 命令的缩写。请使用这个函数来上传文件。


  task "prepare", "192.168.137.3",  sub {
   file "/tmp/machine",
     source => "/root/machine";
};
[root@node01 Rex]# rex prepare
[2017-05-10 10:40:44] WARN - Please use only the following characters for task names:
[2017-05-10 10:40:44] WARN -   A-Z, a-z, 0-9 and _
[2017-05-10 10:40:44] WARN - Also the task should start with A-Z or a-z
[2017-05-10 10:40:44] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-05-10 10:40:44] INFO - Running task prepare on 192.168.137.3
[2017-05-10 10:40:55] INFO - All tasks successful on all hosts


node2:/tmp#cat machine
cat: machine: No such file or directory
node2:/tmp#cat machine
13`
dADA1
131qqe

 task "mytask5",sub {
     file "/etc/hdf",
     content => template("/root/machine"),
     owner  => "mqm",
     group  => "mqm",
     mode   => 700,
     on_change => sub { say "Something was changed." };
};
[root@node01 Rex]# rex mytask5
[2017-05-10 10:46:52] WARN - Please use only the following characters for task names:
[2017-05-10 10:46:52] WARN -   A-Z, a-z, 0-9 and _
[2017-05-10 10:46:52] WARN - Also the task should start with A-Z or a-z
[2017-05-10 10:46:52] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-05-10 10:46:52] INFO - Running task mytask5 on <local>
11111111111112222334414
Something was changed.
[2017-05-10 10:47:02] INFO - All tasks successful on all hosts


[root@node01 Rex]# ls -ltr /etc/hdf
-rwx------ 1 mqm mqm 52 May 10 10:47 /etc/hdf


如果 source 是相对路径,它会自动从你的 Rexfile 位置开始搜索。如果你还用了 Perl 包,搜索路径也就包括 这些 .pm 文件的路径。

这个函数支持一下 hooks:


原文地址:https://www.cnblogs.com/hzcya1995/p/13349719.html