- command模塊
command模塊可以在遠程主機上執行命令,在遠程主機中執行命令時,不會經過遠程主機的shell處理,如果需要執行的命令中含有重定向、管道符等操作,這些符號也會失效,比如"<", ">", "|", ";" 和 "&" ,如果遠程節點是windows操作系統,則需要使用win_command模塊。
模塊參數
參數 | 說明 |
---|---|
free_form | 必須參數,指定需要遠程執行的命令,"free_form"並不是一個"實際存在"的參數名,如,在遠程主機上執行ls命令,並不需要寫成"free_form=ls" ,直接寫成ls即可,這就是free_form參數的含義,因爲command模塊的作用是執行命令,任何一個可以在遠程主機上執行的命令都可以被稱爲free_form。 |
chdir | 此參數的作用就是指定一個目錄,在執行對應的命令之前,會先進入到chdir參數指定的目錄中。 |
creates | 當指定的文件存在時,就不執行對應命令,比如,如果/testdir/test文件存在,就不執行我們指定的命令。 |
removes | 與creates參數的作用正好相反,它的作用是當指定的文件不存在時,就不執行對應命令,此參數並不會幫助我們刪除文件 |
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "chdir=/app/ansible ls"
10.15.43.15 | SUCCESS | rc=0 >>
chdir
zabbix_discovery_port.sh
zabbix_discovery_port.sh.3933.2018-06-27@06:39:26~
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "pwd"
10.15.43.15 | SUCCESS | rc=0 >>
/home/ywbz
[root@Super ~]#
默認進入的是資源清單中ansible_ssh_user指定的用戶家目錄,通過chdir參數在執行命令之前,會先進入到指定的目錄中,再自行後面的命令
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "chdir=/app/ansible ls"
10.15.43.15 | SUCCESS | rc=0 >>
chdir
zabbix_discovery_port.sh
zabbix_discovery_port.sh.3933.2018-06-27@06:39:26~
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "creates=/app/ansible/zabbix_discovery_port.sh pwd"
10.15.43.15 | SUCCESS | rc=0 >>
skipped, since /app/ansible/zabbix_discovery_port.sh exists
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "creates=/app/ansible/zabbix_discovery_port.sh1 pwd"
10.15.43.15 | SUCCESS | rc=0 >>
/home/ywbz
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "creates=/app/ansible/ pwd"
10.15.43.15 | SUCCESS | rc=0 >>
skipped, since /app/ansible/ exists
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "creates=/app/ansible1/ pwd"
10.15.43.15 | SUCCESS | rc=0 >>
/home/ywbz
[root@Super ~]#
從上面結果可知,當creates參數指定的文件(目錄)存在時,就跳過後面的命令,文件(目錄)不存在時就執行後面的命令
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "removes=/app/ansible/zabbix_discovery_port.sh pwd"
10.15.43.15 | SUCCESS | rc=0 >>
/home/ywbz
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "removes=/app/ansible/zabbix_discovery_port1.sh pwd"
10.15.43.15 | SUCCESS | rc=0 >>
skipped, since /app/ansible/zabbix_discovery_port1.sh does not exist
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "removes=/app/ansible/ pwd"
10.15.43.15 | SUCCESS | rc=0 >>
/home/ywbz
[root@Super ~]# ansible 10.15.43.15 -S -R root -m command -a "removes=/app/ansible1/ pwd"
10.15.43.15 | SUCCESS | rc=0 >>
skipped, since /app/ansible1/ does not exist
[root@Super ~]#
removes參數和creates參數完全相反。
- shell模塊
shell模塊在遠程主機上執行命令,與command模塊不同的是,shell模塊在遠程主機中執行命令時,會經過遠程主機上的/bin/sh程序處理。
模塊參數
參數 | 說明 |
---|---|
free_form | 須參數,指定需要遠程執行的命令,但是並沒有具體的一個參數名叫free_form,和command模塊一樣。 |
chdir | 此參數的作用就是指定一個目錄,在執行對應的命令之前,會先進入到chdir參數指定的目錄中。 |
creates | 使用此參數指定一個文件,當指定的文件存在時,就不執行對應命令,可參考command模塊中的解釋。 |
removes | 使用此參數指定一個文件,當指定的文件不存在時,就不執行對應命令,可參考command模塊中的解釋。 |
executable | 默認情況下,shell模塊會調用遠程主機中的/bin/sh去執行對應的命令,通常遠程主機中的默認shell是bash,如果使用其他類型的shell執行命令,則可以使用此參數指定某種類型的shell去執行對應的命令,指定shell文件時,需要使用絕對路徑。 |
shell模塊中chdir、creates、removes參數的作用與command模塊中的作用都是相同的。
[root@Super ~]# ansible 10.15.43.15 -S -R root -m shell -a "chdir=/app/ansible ls -l >> test"
10.15.43.15 | SUCCESS | rc=0 >>
[root@Super ~]#
使用shell模塊可以在遠程服務器上執行命令,它支持管道與重定向等符號,執行的命令都是在遠程主機上
- script模塊
script模塊可以在遠程主機上執行ansible主機上的腳本,不需要手動拷貝到遠程主機後再執行。
模塊參數
參數 | 說明 |
---|---|
free_form | 必須參數,指定需要執行的腳本,腳本位於ansible主機本地,並沒有具體的一個參數名叫free_form,具體解釋參考command模塊。 |
chdir | 此參數的作用就是指定一個遠程主機中的目錄,在執行對應的腳本之前,會先進入到chdir參數指定的目錄中。 |
creates | 指定一個遠程主機中的文件,當指定的文件存在時,就不執行對應腳本,可參考command模塊中的解釋。 |
removes | 指定一個遠程主機中的文件,當指定的文件不存在時,就不執行對應腳本,可參考command模塊中的解釋。 |
[root@Super ~]# ansible 10.15.43.15 -S -R root -m script -a "chdir=/app/ansible /app/download/system-time.sh"
進入遠程主機的/app/ansible目錄後,再執行ansible主機上/app/download/system-time.sh腳本。
[root@Super ~]# ansible 10.15.43.15 -S -R root -m script -a "creates=/app/ansible /app/download/system-time.sh"
10.15.43.15 | SKIPPED
[root@Super ~]# ansible 10.15.43.15 -S -R root -m script -a "removes=/app/ansible /app/download/system-time.sh"
10.15.43.15 | SUCCESS => {
"changed": true,
creates參數指定遠程主機上存在/app/ansible目錄就不執行/app/download/system-time.sh腳本,removes參數執行遠程主機目錄不存在就不執行腳本,存在就執行腳本。