command模块
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[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模块 |
shell模块
1 2 3 4 5 6 7 8 9 |
#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 |
script模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[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 |