zoukankan      html  css  js  c++  java
  • ansible 自动化(2)

    模块介绍:

    copy模块

    使用copy模块,可以将本地文件一键复制到远程服务器;
    -a后跟上参数,参数中指定本地文件和远端路径;

    [root@ZABBIX ~]# ansible glq -m copy -a "src=~/fping-3.8.tar.gz dest=~/"
    10.69.69.121 | success >> {
        "changed": true, 
        "dest": "/root/fping-3.8.tar.gz", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "1e36467cc96a4ca959925ac167ea19af", 
        "mode": "0644", 
        "owner": "root", 
        "size": 147448, 
        "src": "/root/.ansible/tmp/ansible-tmp-1482214561.7-273384209097162/source", 
        "state": "file", 
        "uid": 0
    }
    10.107.2.56 | success >> {
        "changed": true, 
        "dest": "/root/fping-3.8.tar.gz", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "1e36467cc96a4ca959925ac167ea19af", 
        "mode": "0644", 
        "owner": "root", 
        "size": 147448, 
        "src": "/root/.ansible/tmp/ansible-tmp-1482214563.87-235871924804761/source", 
        "state": "file", 
        "uid": 0
    }
    10.107.2.44 | success >> {
        "changed": true, 
        "dest": "/root/fping-3.8.tar.gz", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "1e36467cc96a4ca959925ac167ea19af", 
        "mode": "0644", 
        "owner": "root", 
        "size": 147448, 
        "src": "/root/.ansible/tmp/ansible-tmp-1482214563.94-108812671292926/source", 
        "state": "file", 
        "uid": 0
    }
    
    10.107.2.58 | success >> {
        "changed": true, 
        "dest": "/root/fping-3.8.tar.gz", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "1e36467cc96a4ca959925ac167ea19af", 
        "mode": "0644", 
        "owner": "root", 
        "size": 147448, 
        "src": "/root/.ansible/tmp/ansible-tmp-1482214563.92-185412226226660/source", 
        "state": "file", 
        "uid": 0
    }
    
    验证是否copy成功:
    [root@ZABBIX ~]# ansible glq -m command -a 'ls -al /root/fping-3.8.tar.gz'
    10.107.2.56 | success | rc=0 >>
    -rw-r--r-- 1 root root 147448 Dec 20 14:15 /root/fping-3.8.tar.gz
    10.107.2.44 | success | rc=0 >>
    -rw-r--r-- 1 root root 147448 Dec 20 14:15 /root/fping-3.8.tar.gz
    10.107.2.58 | success | rc=0 >>
    -rw-r--r-- 1 root root 147448 Dec 20 14:15 /root/fping-3.8.tar.gz
    10.69.69.121 | success | rc=0 >>
    -rw-r--r-- 1 root root 147448 Dec 20 07:15 /root/fping-3.8.tar.gz
    
    command模块:

    command模块为ansible默认模块,不指定-m参数时,使用的就是command模块;
    comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道;

    缺点是:不支持管道,就没法批量执行命令;

    实例:

    [root@ZABBIX ~]# ansible glq -m command -a 'pwd'
    10.69.69.121 | success | rc=0 >>
    /root
    10.107.2.56 | success | rc=0 >>
    /root
    10.107.2.44 | success | rc=0 >>
    /root
    10.107.2.58 | success | rc=0 >>
    /root
    
    
    shell模块

    使用shell模块,在远程命令通过/bin/sh来执行;所以,我们在终端输入的各种命令方式,都可以使用;
    但是我们自己定义在.bashrc/.bash_profile中的环境变量shell模块由于没有加载,所以无法识别;如果需要使用自定义的环境变量,就需要在最开始,执行加载自定义脚本的语句;

    对shell模块的使用可以分成两块:
    1) 如果待执行的语句少,可以直接写在一句话中:

    [root@ZABBIX ~]# ansible glq -a "ifconfig eth0 | grep -e "^10.";pwd" -m shell
    10.69.69.121 | success | rc=0 >>
    /root
    
    10.107.2.44 | success | rc=0 >>
    /rooteth0: error fetching interface information: Device not found
    
    10.107.2.56 | success | rc=0 >>
    /root
    
    10.107.2.58 | success | rc=0 >>
    /rooteth0: error fetching interface information: Device not found
    

    2) 如果在远程待执行的语句比较多,可写成一个脚本,通过copy模块传到远端,然后再执行;但这样就又涉及到两次ansible调用;对于这种需求,ansible已经为我们考虑到了,script模块就是干这事的;

    scripts模块

    使用scripts模块可以在本地写一个脚本,在远程服务器上执行:

    脚本如下:

    [root@ZABBIX ~]# vim test.sh
    #!/bin/bash
    declare -i SUM=0
    for ((i=1;i<=100;i+=1))
    do
            let SUM+=$i
    done
    echo $SUM
    

    执行脚本:

    [root@ZABBIX ~]# ansible glq -m script -a "/root/test.sh" 
    10.69.69.121 | success >> {
        "changed": true, 
        "rc": 0, 
        "stderr": "", 
        "stdout": "5050
    "
    }
    10.107.2.44 | success >> {
        "changed": true, 
        "rc": 0, 
        "stderr": "", 
        "stdout": "5050
    "
    }
    10.107.2.56 | success >> {
        "changed": true, 
        "rc": 0, 
        "stderr": "", 
        "stdout": "5050
    "
    }
    10.107.2.58 | success >> {
        "changed": true, 
        "rc": 0, 
        "stderr": "", 
        "stdout": "5050
    "
    }
    
  • 相关阅读:
    js数组
    js中严格模式
    js 中bind
    HDU 2846(Trie树)
    HDU 4825(01 Trie树)
    Codeforces 900B (Java高精度或模拟)
    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 I Reversion Count(Java大数)
    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B Goldbach (素数测试,随机化算法)
    HDU 2256(矩阵快速幂)
    HDU 6029(思维)
  • 原文地址:https://www.cnblogs.com/ligao/p/6202594.html
Copyright © 2011-2022 走看看