zoukankan      html  css  js  c++  java
  • rex 文件操作

    File Manipulation:
    
    文件操作:
    
    Rex::Commands::File 传输文件操作
    
    使用这个模块你可以传输文件
    
    [root@node01 Rex]# cat Rexfile 
    use Rex -feature => ['1.0'];
    no strict;
    
    user "root";
    private_key "/root/.ssh/id_rsa";  ##私钥
    public_key "/root/.ssh/id_rsa.pub"; ##公钥
    key_auth;
    group myservers => "192.168.137.3";
       
     task "mytask", group=>"myservers", sub {
      my $output=run "df";
      say $output;
     };
    
     task "read_passwd", group=> "myservers", sub {
       my $fh = file_read "/etc/passwd";
       for my $line ($fh->read_all) {
         print $line;
       }
       $fh->close;
     };
     task "read_passwd2", "myservers", sub {
           say cat "/etc/passwd";
         };
    
    [root@node01 Rex]#  rex read_passwd | grep nginx
    [2017-04-30 07:37:24] INFO - Running task read_passwd on 192.168.137.3
    [2017-04-30 07:37:35] INFO - All tasks successful on all hosts
    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
    [root@node01 Rex]# 
    
    导出的方法:
    
    template($file, @params)
    
    解析模板并返回内容:
    
    file name 指定是提供给"path_map" 处理作为记录再file()函数来提供一个屋里文件名。
    
     task "task1", sub {
    
        say template("ntpd.conf");
    
     };
    [root@node01 Rex]# rex task1
    [2017-04-30 08:05:58] INFO - Running task task1 on <local>
    aaaaaaaa
    bbbbbbbb
    cccccccc
    111111111
    22222222
    33333333
    
    我们会打印 ntpd.conf文件的内容 如果存在
    
    file($file_name, %options)
    
    这个函数是安装文件的后继,请使用这个函数来上传文件到你的服务器
    
    task "prepare", group=>"myservers", sub {
       file "/tmp/Rexfile",
         source => "Rexfile";
    };
    
    
    上传文件
    
    task "task2", group=>"myservers", sub {
    
      file "/tmp/hosts",
         content => template("hosts"),
         owner  => "mqm",
         group  => "mqm",
         mode   => 700,
         on_change => sub { say "Something was changed." };
    };
    [root@node01 Rex]# rex task2
    [2017-04-30 13:27:47] INFO - Running task task2 on 192.168.137.3
    Something was changed.
    [2017-04-30 13:28:01] INFO - All tasks successful on all hosts
    
    
    
    task "task2", group=>"myservers", sub {
    
      file "/tmp/hosts",
         content => template("hosts"),
         owner  => "mqm",
         group  => "mqm",
         mode   => 700,
         on_change => sub { say "Something was changed." };
    };
    [root@node01 Rex]# rex task2
    [2017-04-30 13:27:47] INFO - Running task task2 on 192.168.137.3
    Something was changed.
    [2017-04-30 13:28:01] INFO - All tasks successful on all hosts
    [root@node01 Rex]# 
    [root@node01 Rex]# 
    [root@node01 Rex]# 
    [root@node01 Rex]# rex task2
    [2017-04-30 13:30:19] INFO - Running task task2 on 192.168.137.3
    
    
    
    task "task2", group=>"myservers", sub {
    
      file "/tmp/hosts",
         content => template("hosts"),
         owner  => "mqm",
         group  => "mqm",
         mode   => 700,
         on_change => sub { say "Something was changed." };
    };
    [root@node01 Rex]# rex task2
    [2017-04-30 13:31:23] INFO - Running task task2 on 192.168.137.3
    Something was changed.
    [2017-04-30 13:31:37] INFO - All tasks successful on all hosts
    
    第一个参数是一个字符串或者一个数组引用,在后面的例子
    
    函数被调用用于所有的字符串在数组里。
    

  • 相关阅读:
    对其他组的评论与意见
    第一阶段结果展示
    团队第一阶段冲刺绩效评估
    团队冲刺第八天站立会议
    团队检查博客
    团队冲刺地七天站立会议
    团队冲刺第六天站立会议
    团队冲刺第五天战略站立会议
    团队冲刺第四天战略会议站立
    No2_2.接口继承多态_Java学习笔记_继承
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349741.html
Copyright © 2011-2022 走看看