Ansible 批量管理linux機(jī)器

一、準(zhǔn)備環(huán)境

機(jī)器:centos6.9

1、安裝ansible

[root@localhost ~]# yum -y install ansible
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.btte.net
 * updates: mirrors.tuna.tsinghua.edu.cn
Package ansible-2.3.1.0-1.el6.noarch already installed and latest version
Nothing to do

本機(jī)已經(jīng)安裝完ansible

2、批量配置免登錄。

#!/usr/bin/expect
for p in $(cat /data/shell/list)
do
password=$(echo "$p"|cut -f1 -d":")
ip=$(echo "$p"|cut -f2 -d":")
expect -c "
spawn ssh-copy-id -i  root@$ip  
expect {  
\"*yes/no*\" {send \"yes\r\"; exp_continue}  
\"*password*\" {send \"$password\r\"; exp_continue}  
\"*Password*\" {send \"$password\r\";}  
}  
"  
done 

需要一個(gè)list文件,里面記錄了需要更新的主機(jī)以及對(duì)應(yīng)密碼,格式如下:

123456:192.168.1.20  
345676:192.168.2.20

密碼:主機(jī)名 ,以分號(hào)分隔。
expect命令如果沒(méi)有的話(huà),需要提前安裝

運(yùn)行腳本后,顯示如下:

[root@localhost shell]# sh expect.sh 
spawn ssh-copy-id -i root@192.168.6.189
spawn ssh-copy-id -i root@192.168.6.89
The authenticity of host '192.168.6.89 (192.168.6.89)' can't be established.
RSA key fingerprint is 04:46:a5:a6:93:74:e1:d2:58:a1:3d:65:63:43:72:d8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.6.89' (RSA) to the list of known hosts.
root@192.168.6.89's password: 
Now try logging into the machine, with "ssh 'root@192.168.6.89'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

spawn ssh-copy-id -i root@10.18.99.62
The authenticity of host '10.18.99.62 (10.18.99.62)' can't be established.
RSA key fingerprint is 04:46:a5:a6:93:74:e1:d2:58:a1:3d:65:63:43:72:d8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.18.99.62' (RSA) to the list of known hosts.
root@10.18.99.62's password: 
Now try logging into the machine, with "ssh 'root@10.18.99.62'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.  

配置完畢。

二、配置ansible

1、編輯配置文件、添加用戶(hù)組。

編輯 /etc/ansible/hosts
[all]
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11

2、執(zhí)行簡(jiǎn)單命令。

ansible all -m shell -a 'uptime'

192.168.1.3 | SUCCESS | rc=0 >>
 14:08:24 up 1 day, 22:26,  1 user,  load average: 0.06, 0.12, 0.13

192.168.1.2 | SUCCESS | rc=0 >>
 14:10:02 up 3 days,  3:36,  2 users,  load average: 0.04, 0.04, 0.05

3、成功執(zhí)行命令并返回。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • root@softwareluke-Lenovo:/home/softwareluke# virt-manager...
    燕京博士閱讀 813評(píng)論 0 0
  • ansible 系統(tǒng)架構(gòu) ansible簡(jiǎn)介ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,ansible是一個(gè)配置管理和...
    運(yùn)維阿文閱讀 9,841評(píng)論 1 53
  • 陽(yáng)光打在窗玻璃上 聲勢(shì)浩大卻沒(méi)聲息 留下的痕跡很溫暖 它闖過(guò)無(wú)垠虛空后 只為了與你的肌膚 來(lái)一次激情的碰觸 無(wú)怨無(wú)...
    老爺爺不是我閱讀 217評(píng)論 0 1
  • 糖月陽(yáng)閱讀 241評(píng)論 0 0

友情鏈接更多精彩內(nèi)容