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.\r\n", 
    "stderr_lines": [
        "Shared connection to web03 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
web01 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to web01 closed.\r\n", 
    "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

 

posted @ 2020-10-07 13:57  六月OvO  阅读(484)  评论(0编辑  收藏  举报