RexCommandsFile:
透明的文件处理:
这个模块可以操作文件:
task "read_passwd", group=>"myservers", sub {
my $fh = file_read "/etc/passwd";
for my $line ($fh->read_all) {
print $line;
}
$fh->close;
};
[root@node01 Rex]# rex read_passwd
[2017-05-01 09:56:17] INFO - Running task read_passwd on 192.168.137.3
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaldaemon:x:68:68:HAL daemon:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinsaslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinoprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologinmqm:x:500:500::/home/mqm:/bin/bashmysql:x:501:501::/home/mysql:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinzabbix:x:201:201::/home/zabbix:/bin/bashnginx:x:502:502::/home/nginx:/bin/bash[2017-05-01 09:56:28] INFO - All tasks successful on all hosts
[root@node01 Rex]#
导出的函数:
template($file, @params)
解析一个模板和返回内容:
my $content = template("/files/templates/vhosts.tpl",
name => "test.lan",
webmaster => 'webmaster@test.lan');
文件名指定是指定到 "path_map" 处理作为激励
在file()函数来解决这个物理文件名。
task "mytask3", group=>"myservers", sub {
my $content = template("hosts"
);
say $content;
};
[root@node01 Rex]# rex mytask3
[2017-05-01 10:14:46] WARN - Task mytask3 already exists. Overwriting...
[2017-05-01 10:14:46] INFO - Running task mytask3 on 192.168.137.3
1.1.1.1
2.2.2.2
3.3.3.3
[2017-05-01 10:14:59] INFO - All tasks successful on all hosts
[root@node01 Rex]#
file($file_name, %options)
此功能是安装文件的后继,请使用这个功能来上传文件到你的服务器:
task "file1", group=>"myservers", sub {
file "/root/hosts",
source => "hosts";
};
[root@node01 Rex]# rex file1
[2017-05-01 10:28:43] WARN - Task mytask3 already exists. Overwriting...
[2017-05-01 10:28:43] INFO - Running task file1 on 192.168.137.3
[2017-05-01 10:28:54] INFO - All tasks successful on all hosts
[root@node01 Rex]#
task "file2", group=>"myservers", sub {
file "/root/hosts",
content => template("/etc/hosts"),
owner => "mqm",
group => "mqm",
mode => 400,
on_change => sub { say "Something was changed." };
};
[root@node01 Rex]# rex file2
[2017-05-01 10:30:58] WARN - Task mytask3 already exists. Overwriting...
[2017-05-01 10:30:58] INFO - Running task file2 on 192.168.137.3
Something was changed.
[2017-05-01 10:31:12] INFO - All tasks successful on all hosts
[root@node01 Rex]#
node2:/root#ls -ltr hosts
-r-------- 1 mqm mqm 158 May 8 01:33 hosts
task "file3", group=>"myservers", sub {
file "/root/hosts",
content => 'fortune';
};
[root@node01 Rex]# rex file3
[2017-05-01 10:38:20] WARN - Task mytask3 already exists. Overwriting...
[2017-05-01 10:38:20] INFO - Running task file3 on 192.168.137.3
[2017-05-01 10:38:31] INFO - All tasks successful on all hosts
将'fortune' 内容写入到/root/hosts
task "file4", group=>"myservers", sub {
file "/root/hosts",
content => template("vv"),
no_overwrite => TRUE; # this file will not be overwritten if already exists.
};
[root@node01 Rex]# rex file4
[2017-05-01 10:45:48] WARN - Task mytask3 already exists. Overwriting...
[2017-05-01 10:45:48] INFO - Running task file4 on 192.168.137.3
[2017-05-01 10:46:01] INFO - All tasks successful on all hosts
配置更改自动重启服务:
node2:/etc#ps -ef | grep mysql | grep -v grep
root 22637 1 0 May06 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data01/mysql --pid-file=/data01/mysql/node2.pid
mysql 23029 22637 0 May06 ? 00:00:32 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data01/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/mysqld.log --pid-file=/data01/mysql/node2.pid --socket=/data01/mysql/mysql.sock
node2:/etc#mysql -uroot -p1234567 --socket=/data01/mysql/mysql.sock -e "show variables like '%binlog_format%'";
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
node2:/etc#
task "file4", group=>"myservers", sub {
file "/etc/my.cnf",
source => "my.cnf",
on_change => sub { service mysql => "restart"; }
};
[root@node01 Rex]# rex file4
替换文件 自动重启服务
第一个参数是一个字符串或者一个数组引用。
在后面的情况 函数是被调用用于所有的字符串 因此下面的构造是等价的: