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

Ansible直接執行遠程命令,不用ssh登錄交互執行。shell

以下:網絡

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

參數解釋:ssh

  • -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

使用主機文件,自動登陸後,批量執行命令:.net

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

這個仍是很方便的。code

參考:blog

相關文章
相關標籤/搜索