1. 程式人生 > 實用技巧 >Ansible命令模組(command模組 shell模組 script模組 )

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