1.ssh-keygen非交互式創(chuàng)建秘鑰對
具體命令:ssh-keygen -f ~/.ssh/id_rsa -P '' -q
參數(shù):
-P 密碼
-f 輸出的密鑰文件
-q 不輸出信息
-t 指定密鑰類型
ssh-keygen -f ~/.ssh/id_rsa -P'' -q
2.ssh0copy-id 不提示yes/no分發(fā)密鑰
ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.8
-f:強制
-i:指定密鑰文件
-o:指定ssh參數(shù)
StrictHostKeyChecking=no:嚴格公鑰檢查:no
3.sshpass工具:非交互分發(fā)密鑰
yum -y? install sshpass
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.8
參數(shù): -p? password 指定用戶密碼操作
4.一鍵配置實踐
#!/bin/bash
#yum install sshpass -y
ssh-keygen -f ~/.ssh/id_rsa? -P '' -q
for ip in 7 61
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
#test(測試)
ssh 172.16.1.7? "ip a"
ssh 172.16.1.61 "ip a"
-----------------------------------------------------------------------------
ansible
1.功能介紹
ssh密鑰認證+腳本批量管理邮绿。
Ansible通過SSH協(xié)議實現(xiàn)管理節(jié)點與遠程節(jié)點之間的通信。
涉及管理操作:復制文件攀例、安裝服務船逮、服務啟動停止管理、配置管理等等粤铭。
2.架構介紹
1挖胃、連接插件connectior plugins用于連接主機 用來連接被管理端
2、核心模塊 core modules 連接主機實現(xiàn)操作梆惯, 它依賴于具體的模塊來做具體的事情
3酱鸭、自定義模塊 custom modules,根據自己的需求編寫具體的模塊
4垛吗、插件 plugins凹髓,完成模塊功能的補充
5、劇本 playbooks怯屉,ansible的配置文件,將多個任務定義在劇本中蔚舀,由ansible自動執(zhí)行
6饵沧、主機清單 inventor,定義ansible需要操作主機的范圍
最重要的一點是 ansible是模塊化的 它所有的操作都依賴于模塊
3.安裝ansible
yum install epel-release -y
yum install ansible? ? ? -y(管理機)
---------------------
rpm -qa |grep libselinux-python
yum install libselinux-python -y(查看有沒有l(wèi)ibselinux-python,有就不下載了)
4.配置文件
/etc/ansible/hosts主機資產清單文件赌躺,用于定義被管理主機的認證信息狼牺,
/etc/ansible/ansible.cfg(是否嚴格公鑰檢查,設置no后可以使用密碼登錄)
[root@m01 ~]# ansible oldboy -m shell -a "ip a|grep eth1"
5.登錄方法
5.1密鑰登錄
vim /etc/ansible/hosts
[oldboy]
172.16.1.7
ssh-keygen -f ~/.ssh/id_rsa -P '' -q 創(chuàng)建密鑰對
sshpasswd -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$i 分發(fā)密鑰(寫于腳本中)
"-o StrictHostKeyChecking=no" 避免首次使用時輸入yes/no部分礼患。 不進行公鑰確認是钥。
5.2密碼登錄
vim /etc/ansible/ansible.cfg
host_key_checking = False(取消備注,允許密碼登錄)
vim /etc/ansible/hosts
[oldboy_pass]
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456
測試 ansible oldboy_pass -m shell -a "ip a"
ansible oldboy -m copy -a "src=/etc/passwd dest=/tmp/oldgirl owner=oldboy group=oldboy mode=600"
6.ansible命令參數(shù)
-m MODULE_NAME, 模塊名字,默認command
-a MODULE_ARGS, 模塊參數(shù)
-f FORKS ? ? 并發(fā)進程數(shù)缅叠,默認5個咏瑟。
-i INVENTORY(default=/etc/ansible/hosts)指定主機列表文件
===================
ansible? ? ? ? ? ? ? ? ? ? 批量執(zhí)行
ansible-doc? ?
ansible-doc -l? ? ? ? 查找模塊
ansible-doc -s command 查找具體參數(shù)
ansible-playbook? ? ? ? ? 組成多個模塊的容器。
ansible-galaxy? ? ? ? ? ? 從galaxy網站下載角色
===================