• 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
  • 相关阅读:
    xunjian.sh
    192.168.50.235配置
    自动备份并删除旧日志
    bg和fg命令
    linux之sed用法
    正则表示第二行,第二列
    linux下redis安装
    Hma梳理
    linux 系统监控、诊断工具之 lsof 用法简介
    java的基本数据类型有八种
  • 原文地址:https://www.cnblogs.com/chenlifan/p/13777173.html
Copyright © 2020-2023  润新知