1. 程式人生 > >Ansible批量在遠端主機執行命令

Ansible批量在遠端主機執行命令

浪費了“黃金五年”的Java程式設計師,還有救嗎? >>>   

Ansible直接執行遠端命令,不用ssh登陸互動執行。

如下:

ansible all -i 192.168.199.180, -m shell -a "ifconfig" -u supermap

引數解釋:

  • -i 連線到遠端主機“192.168.199.180,”,注意這裡的“,”直接指定主機名列表,沒有“,”該引數則被解釋為主機名的列表檔案。
  • -m 為ansible模組名稱,執行shell命令。
  • -a 遠端shell命令,為ifconfig(獲取網路配置引數)。
  • -u 指定執行的遠端主機使用者名稱。
  • -k 可以指定是否互動輸入引數,如果沒有,則需要在本機有預存的訪問金鑰。
    • 可以先使用ssh登入,選擇儲存,即可自動儲存下來。

主機檔案格式,樣例參考( host-list):

[local]
192.168.199.173 ansible_ssh_port=22 ansible_ssh_host=192.168.199.173 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
192.168.199.180 ansible_ssh_port=22 ansible_ssh_host=192.168.199.180 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
192.168.199.249 ansible_ssh_port=22 ansible_ssh_host=192.168.199.249 ansible_ssh_user=openswitch ansible_ssh_pass=SuperMap

使用主機檔案,自動登入後,批量執行命令:

ansible all -i host-list -m shell -a "ifconfig" -u my-username

這個還是很方便的。

參考: