[轉] ansible批量執行命令展示


【From】 https://blog.csdn.net/zhydream77/article/details/81223805

 

ansible命令基礎


• ansible <host-pattern> [options]
host-pattern 主機戒定義的分組
-M 指定模塊路徑
-m 使用模塊,默認 command 模塊
-a or --args 模塊參數
-i inventory 文件路徑,戒可執行腳本
-k 使用交虧式登彔密碼
-e 定義變量
-v 詳紳信息,-vvvv 開吭 debug 模式


• 列出要執行的主機,不執行任何操作
ansible all --list-hosts


• 批量檢測主機
ansible all -m ping


• 批量執行命令
ansible all -m command -a 'id' -k


批量部署證書文件
• 每次交虧輸入密碼比較麻煩
• 密碼寫入配置文件安全性很差
• 不同主機不同密碼,配置文件要上天
• 使用 key 方式認證,是一個不錯的選擇
• 給所有主機部署公鑰

ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/authorized_keys)'" -k -v

 

舉例說明:

批量檢測主機
[root@ansible ~]# ansible web  -m ping

批量執行命令
[root@ansible ~]# ansible all -m command -a 'uptime'
[root@ansible ~]# ansible web  -m command -a 'uptime'
[root@ansible ~]# ansible web  -m command -a 'uptime' -k
SSH password: 
web1 | SUCCESS | rc=0 >>
 14:37:12 up  5:12,  3 users,  load average: 0.01, 0.03, 0.02

web2 | SUCCESS | rc=0 >>
 14:37:12 up  5:03,  2 users,  load average: 0.00, 0.01, 0.03

批量部署證書文件
[root@ansible ooxx]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/id_rsa.pub)'" -k  

注意:

/root/.ssh/id_rsa.pub是通過命令生成的ssh-keygen -t rsa並且在/root/.ssh/目錄下可以看到的
---------------------
作者:zhy-dream
來源:CSDN
原文:https://blog.csdn.net/zhydream77/article/details/81223805
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM