zoukankan      html  css  js  c++  java
  • Ansible命令模块(command模块 shell模块 script模块 )

    1.command模块

    [root@m01 ~]# ansible 'web01' -m command -a 'free -m'
    web01 | CHANGED | rc=0 >>
                  total        used        free      shared  buff/cache   available
    Mem:            972         110         460          19         401         667
    Swap:          2047           0        2047
    
    #command命令不支持特殊符号
    [root@m01 ~]# ansible 'web01' -m command -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
    web01 | FAILED | rc=1 >>
    |: Unknown host
    ifconfig: `--help' gives usage information.non-zero return code
    
    #当ansible命令没指定模块时,默认使用command模块

    2.shell模块

    #shell模块识别特殊符号,但是不支持 $符
    [root@m01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
    web01 | CHANGED | rc=0 >>
            inet 10.0.0.7  netmask 255.255.255.0  broadcast 10.0.0.255
    
    #可以使用 撬棍  转义 $符,就可以识别了
    [root@m01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
    web01 | CHANGED | rc=0 >>
    10.0.0.7

    3.script模块

    [root@m01 ~]# ansible 'web_group' -m script -a '/root/mkdir.sh'
    web03 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to web03 closed.
    ", 
        "stderr_lines": [
            "Shared connection to web03 closed."
        ], 
        "stdout": "", 
        "stdout_lines": []
    }
    web01 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to web01 closed.
    ", 
        "stderr_lines": [
            "Shared connection to web01 closed."
        ], 
        "stdout": "", 
        "stdout_lines": []
    }
    
    #验证文件
    [root@m01 ~]# ansible 'web_group' -m shell -a 'ls -ld /123'
    web01 | CHANGED | rc=0 >>
    drwxr-xr-x 2 root root 6 Sep 17 17:26 /123
    web03 | CHANGED | rc=0 >>
    drwxr-xr-x 2 root root 6 Sep 17 17:26 /123
  • 相关阅读:
    STM32中断优先级理解
    STM32按键控制程序
    STM32的LED驱动程序
    嵌入式程序员应知道的0x10个C语言Tips[转]
    【Unity】使用RenderTexture为物体生成快照
    对装饰模式(Decorator)的解读
    设计模式之初:理解面向对象设计
    windows RT系统下解决网络丢包问题
    IOS推出测试平台
    小米路由试用心得3——关于数据备份及客户端软件
  • 原文地址:https://www.cnblogs.com/chenlifan/p/13777173.html
Copyright © 2011-2022 走看看