一挚赊、秘鑰認(rèn)證部署流程
1藻三、非交互式
創(chuàng)建秘鑰對
ssh-keygen -t ~/.ssh/id_dsa -P ''4
發(fā)送秘鑰
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no 172.16.1.41
epel給ssh相關(guān)客戶端提供密碼
2洪橘、使腳本一鍵創(chuàng)建、分發(fā)棵帽、測試秘鑰
1)編寫腳本:
[root@m01 ~]# vim /server/scripts/sshpass.sh
#!/bin/bash
#ssh-keygen-id
rm -rf ~/.ssh/*
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P '' &>/dev/null
for ip in `cat /server/scripts/ip.txt`
do
echo "++++++++++++++++Connect Start $ip++++++++++++++++"
sshpass -p 123456 ssh-copy-id -i /root/.ssh/id_dsa.pub $ip -o StrictHostKeyChecking=no
&>/dev/null
ssh $ip hostname
if (($?==0))
then
echo "++++++++++++++++$ip connect successed++++++++++++++++"
else
echo "++++++++++++++++$ip connect failed++++++++++++++++"
fi
echo "++++++++++++++++Connect END $ip++++++++++++++++"
done
~
2)執(zhí)行腳本:
[root@m01 ~]# sh /server/scripts/sshpass.sh
++++++++++++++++Connect Start 172.16.1.7++++++++++++++++
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '172.16.1.7'"
and check to make sure that only the key(s) you wanted were added.
web01
++++++++++++++++172.16.1.7 connect successed++++++++++++++++
++++++++++++++++Connect END 172.16.1.7++++++++++++++++
++++++++++++++++Connect Start 172.16.1.8++++++++++++++++
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: ERROR: ssh: connect to host 172.16.1.8 port 22: No route to host
ssh: connect to host 172.16.1.8 port 22: No route to host
++++++++++++++++172.16.1.8 connect failed++++++++++++++++
++++++++++++++++Connect END 172.16.1.8++++++++++++++++
++++++++++++++++Connect Start 172.16.1.31++++++++++++++++
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '172.16.1.31'"
and check to make sure that only the key(s) you wanted were added.
nfs01
++++++++++++++++172.16.1.31 connect successed++++++++++++++++
++++++++++++++++Connect END 172.16.1.31++++++++++++++++
++++++++++++++++Connect Start 172.16.1.41++++++++++++++++
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.
backup
++++++++++++++++172.16.1.41 connect successed++++++++++++++++
++++++++++++++++Connect END 172.16.1.41++++++++++++++++
二熄求、ansible 批量管理 批量配置 批量分發(fā)
使用前:在管理機上生成秘鑰并分發(fā)
pssh(pssh prsync)
ansible 和 salstack
對比
共同點
批量管理
基于Python
區(qū)別
ansible不需要安裝客戶端
saltstack 需要安裝客戶端
puppet
1、使用指南
在管理配置好 秘鑰認(rèn)證
1)ansible文件
/etc/ansible
/etc/ansible/ansible.cfg 配置文件
/etc/ansible/hosts 主機清單(被管理的服務(wù)器列表)
/etc/ansible/roles
2)ansible
格式 :ansible oldboy -m command -a 'hostname'
參數(shù)
-m 指定模塊
ping 檢查 客戶端是否存活
command 命令模式(只能執(zhí)行簡單命令 不支持 特殊符號) 默認(rèn)的模塊
-a 模塊中命令或參數(shù)
3)顏色
綠色 ok 對方?jīng)]有發(fā)送修改
黃色 修改內(nèi)容(修改成功)
紅色 報錯
紫色 warning 警告
4逗概、Ansible inventory 主機清單
/etc/ansible/hosts
5什黑、AD-Hoc 批量執(zhí)行命令
1)單詞
src source 源
dest 目標(biāo)
content 內(nèi)容 文件內(nèi)容
state 狀態(tài)
2)模塊
(1)command 命令模塊
默認(rèn)模塊死嗦,用于執(zhí)行命令,不支持特殊符號
(2)shell 支持特殊模塊 執(zhí)行腳本
(3)copy 復(fù)制模塊
src= 源
dest= 目的
owner=所有者
group= 用戶組
mode= 權(quán)限
backup 是否備份
(4)script 腳本模塊
Runs a local script on a remote node after transferring it
先把腳本傳送到遠(yuǎn)端 然后執(zhí)行
(5)yum
name=軟件包名
state
installed,present 安裝軟件包
removed菊匿,absent 移除安裝包
latest 安裝最新安裝包(更新)
file
path 路徑或文件名
recure 遞歸授權(quán)
state
directory 目錄
touch 文件
absent 刪除文件或目錄
service
開啟 重啟 關(guān)閉
name 要啟動的服務(wù)名稱
state
started 啟動服務(wù)
stopped 停止服務(wù)
restarted 重啟服務(wù)
reloaded 重載服務(wù)
enable 開機自啟動
group 創(chuàng)建用戶組
name 指定創(chuàng)建的組名
gid 指定組的gid
state
absent 移除遠(yuǎn)端主機的組
present 創(chuàng)建遠(yuǎn)端主機的組
user 添加用戶
uid 指定用戶的uid
group 指定用戶組的名稱
shell 指定用戶是否能登錄
create_home 是否創(chuàng)建家目錄
state
absent 刪除用戶