Day38 課堂作業(yè)
1畜份、SSH批量管理項(xiàng)目一鍵分發(fā)
1.)ssh-keygen非交互式創(chuàng)建秘鑰對
具體命令:
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
參數(shù)講解:
參數(shù) | 功能 |
---|---|
ssh-keygen | 密鑰對創(chuàng)建工具 |
[-P old_passphrase] | 密碼 |
[-f output_keyfile] | 輸出的秘鑰文件 |
[-q] | 不輸出信息 |
[-t dsa ] | 指定秘鑰類型玄叠。 |
2.)ssh-copy-id不需要提示yes/no分發(fā)秘鑰
具體命令:
ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.8
參數(shù)講解:
ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root172.16.1.8
ssh-copy-id [-f] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
說明:
- -f:force mode 強(qiáng)制
- [-i [identity_file]]:指定秘鑰文件
- [[-o <ssh -o options>] ...]:指定ssh參數(shù)選項(xiàng)。
3.)sshpass工具:指定密碼非人工交互分發(fā)秘鑰
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
[root@web02 ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass [-f|-d|-p|-e] [-hV] command parameters
參數(shù)講解:
-p password Provide password as argument (security unwise) <-->指定用戶密碼操作
4.)一鍵配置實(shí)踐
把web02作為分發(fā)服務(wù)器:
- web02(172.16.1.8)-->m01(172.16.1.61)
- web02(172.16.1.8)-->web01(172.16.1.7)
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
編寫腳本:
#!/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 "ifconfig eth0"
ssh 172.16.1.61 "ifconfig eth0"
2摆马、ansible 自動(dòng)化管理
運(yùn)維發(fā)展歷史:普通運(yùn)維--->自動(dòng)化運(yùn)維--->平臺(tái)化運(yùn)維--->可視化運(yùn)維--->智能化運(yùn)維
1.) ansible介紹
ssh
秘鑰認(rèn)證+腳本批量管理臼闻,特點(diǎn):簡單、實(shí)用囤采,
但是看起來比較LOW
述呐,需要人工寫腳本,類似實(shí)時(shí)復(fù)制的inotify
工具蕉毯。
2013以前這種方式很普遍乓搬。
MySQL
高可用MHA
集群,要求所有機(jī)器互相秘鑰認(rèn)證代虾。
大數(shù)據(jù)集群也需要进肯。
2.批量管理工具歷史
年份 | 工具 |
---|---|
08年以前 | SSH+腳本 |
07-08年 | CFEngine |
10-13年 | Puppet |
14-17年 | saltstack(python開發(fā)) |
16-今至 | ansible(python開發(fā)) |
3.)為什么用ansible
簡單、方便棉磨、容易學(xué)習(xí)江掩、功能同樣強(qiáng)大。
ansible
有配置文件乘瓤,可以多線程直接實(shí)現(xiàn)环形。不需要寫腳本,類似實(shí)時(shí)復(fù)制的sersync
衙傀。
4.)什么是ansible
ansible
是一個(gè)用來遠(yuǎn)程管理服務(wù)器的工具軟件抬吟。
ansible
是一個(gè)用來批量部署遠(yuǎn)程主機(jī)上服務(wù)的工具。這里“遠(yuǎn)程主機(jī)(Remote Host
)”是指任
何可以通過SSH
登錄的主機(jī)统抬,所以它既可以是遠(yuǎn)程虛擬機(jī)或物理機(jī)火本,也可以是本地主機(jī)。
ansible
通過SSH
協(xié)議實(shí)現(xiàn)管理節(jié)點(diǎn)與遠(yuǎn)程節(jié)點(diǎn)之間的通信聪建。理論上來說钙畔,只要能通過SSH
登錄到
遠(yuǎn)程主機(jī)來完成的操作,都可以通過Ansible實(shí)現(xiàn)批量自動(dòng)化操作妆偏。
涉及管理操作:復(fù)制文件、安裝服務(wù)盅弛、服務(wù)啟動(dòng)停止管理钱骂、配置管理等等叔锐。
5.)為什么要用批量管理工具運(yùn)維
提高效率:大型互聯(lián)網(wǎng)公司,如:百度(幾萬臺(tái)服務(wù)器)见秽,阿里(幾十萬臺(tái)服務(wù)器)
6.) Ansible特點(diǎn)
Ansible
基于Python
語言實(shí)現(xiàn)愉烙,由Paramiko
和PyYAML
兩個(gè)關(guān)鍵模塊構(gòu)建。
- 1)安裝部署過程特別簡單解取,學(xué)習(xí)曲線很平坦步责。
- 2)不需要單獨(dú)安裝客戶端,只是利用現(xiàn)有的
SSHD
服務(wù)(協(xié)議)即可禀苦。- 3)不需要服務(wù)端(
no servers
)蔓肯。- 4)
ansible playbook
,采用yaml
配置振乏,提前編排自動(dòng)化任務(wù)蔗包。- 5)
ansible
功能模塊較多,對于自動(dòng)化的場景支持豐富慧邮。
7.Ansible架構(gòu)介紹(見圖說明)
- 1调限、連接插件
connectior plugins
用于連接主機(jī) 用來連接被管理端- 2、核心模塊
core modules
連接主機(jī)實(shí)現(xiàn)操作误澳, 它依賴于具體的模塊來做具體的事情- 3耻矮、自定義模塊
custom modules
,根據(jù)自己的需求編寫具體的模塊- 4忆谓、插件
plugins
裆装,完成模塊功能的補(bǔ)充- 5、劇本
playbooks
陪毡,ansible
的配置文件,將多個(gè)任務(wù)定義在劇本中米母,由ansible
自動(dòng)執(zhí)行- 6、主機(jī)清單
inventor
毡琉,定義ansible
需要操作主機(jī)的范圍
最重要的一點(diǎn)是ansible
是模塊化的 它所有的操作都依賴于模塊
8.)ansible實(shí)踐環(huán)境準(zhǔn)備
- m01(172.16.1.61)====>nfs01(172.16.1.31)
- m01(172.16.1.61)====>backup(172.16.1.41)
9.) 安裝ansible
m01管理機(jī):
yum install epel-release -y
yum install ansible -y
rpm -qa |grep libselinux-python
yum install libselinux-python -y
如果有
libselinux-python
就不執(zhí)行下面的命令了其他所有機(jī)器:
rpm -qa |grep libselinux-python
yum install libselinux-python -y
10.) 主機(jī)列表配置
/etc/ansible/hosts
主機(jī)資產(chǎn)清單文件铁瞒,用于定義被管理主機(jī)的認(rèn)證信息,
例如ssh
登錄用戶名桅滋、密碼以及key
相關(guān)信息慧耍。如何配置Inventory
文件
- 1.主機(jī)支持主機(jī)名通配以及正則表達(dá)式,例如web[1:3].oldboy.com代表三臺(tái)主機(jī)
- 2.主機(jī)支持基于非標(biāo)準(zhǔn)的ssh端口丐谋,例如web1.oldboyedu.com:6666
- 3.主機(jī)支持指定變量芍碧,可對個(gè)別主機(jī)的特殊配置,如登陸用戶\密碼
- 4.主機(jī)組支持指定變量[group_name:vars]号俐,同時(shí)支持嵌套組[game:children]
實(shí)踐:
cp /etc/ansible/hosts{,.ori}
cat >/etc/ansible/hosts<<EOF
[oldboy]
172.16.1.31
172.16.1.41
[oldgirl]
172.16.1.31
172.16.1.41
172.16.1.51
EOF
cat /etc/ansible/hosts
/etc/ansible/ansible.cfg
ansible
的配置文件
11.) 小試牛刀
直接執(zhí)行如下命令或報(bào)錯(cuò)
ansible oldboy -m command -a "free -m"
基于
SSH
秘鑰認(rèn)證的前提下:
如果沒有做SSH秘鑰認(rèn)證泌豆,可以把用戶名密碼寫到
/etc/ansible/hosts
vim /etc/ansible/hosts
[oldboy_pass]
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=123456
ansible oldboy -m command -a "free -m"
要想成功,先解決yes/no
的問題吏饿。
- 1踪危、使用
ssh
連接一遍蔬浙。- 2、使用
ssh -o
參數(shù)
上述命令就是
sshpass
的封裝
ansible
關(guān)閉ssh
首次連接時(shí)yes/no
提示
使用ssh連接時(shí)贞远,可以使用-o
參數(shù)將StrictHostKeyChecking
設(shè)置為no
畴博,
避免使用ssh
連接時(shí)避免首次連接時(shí)讓輸入yes/no
部分的提示。
方法1:修改
/etc/ansible/ansible.cfg
中的374
行
369 [ssh_connection]
370
371 # ssh arguments to use
372 # Leaving off ControlPersist will result in poor performance, so use
373 # paramiko on older platforms rather than removing it, -C controls compression use
374 #ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
374行改為:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
方法2:修改
/etc/ansible/ansible.cfg
中的71
行
70 # uncomment this to disable SSH key host checking
71 #host_key_checking = False
71行的注釋取消:host_key_checking = False
[root@m01 /server/scripts]# > ~/.ssh/known_hosts
[root@m01 /server/scripts]# ssh 172.16.1.31
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.
ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.
Are you sure you want to continue connecting (yes/no)? ^C
[root@m01 /server/scripts]# ansible oldboy -m command -a "free -m"
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 88 592 13 291 708
Swap: 767 0 767
172.16.1.41 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 89 572 13 310 706
Swap: 767 0 767
注意:使用
SSH
連接:
- 密碼認(rèn)證
host
里主機(jī)后面加密碼Paramiko
模塊 (重點(diǎn))- 秘鑰認(rèn)證:提前發(fā)公鑰蓝仲,才能用
ansible
.SSHPASS
工具
特殊端口:
[oldboy]
172.16.1.7 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456
172.16.1.8 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456
基于
SSH
秘鑰認(rèn)證的實(shí)踐一鍵創(chuàng)建及分發(fā)秘鑰:
#!/bin/bash
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 8 41 31
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
[root@m01 ~]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
[root@m01 ~]# ansible oldboy -m command -a "free -m"
12.) ansible命令參數(shù)
參數(shù) | 功能 |
---|---|
-m | MODULE_NAME, 模塊名字俱病,默認(rèn)command |
-a | MODULE_ARGS, 模塊參數(shù) |
-f FORKS | 并發(fā)進(jìn)程數(shù),默認(rèn)5個(gè)袱结。 |
-i INVENTORY(default=/etc/ansible/hosts) | 指定主機(jī)列表文件 |
13.) ansible模塊查看和幫助
查找模塊
ansible-doc -l
查看某個(gè)模塊的具體參數(shù)幫助
ansible-doc -s command
A:command模塊功能說明:
1)功能說明: 執(zhí)行一個(gè)命令在遠(yuǎn)程節(jié)點(diǎn)上
2)操作實(shí)踐:
ansible oldboy -m command -a "free -m"
ansible oldboy -m command -a "df -h"
ansible oldboy -m command -a "ls /root"
ansible oldboy -m command -a "cat redhat-release"
ansible oldboy -m command -a "cat /etc/redhat-release"
最通用的功能亮隙。但不支持一些特殊符號,如:> < | &等$HOME擎勘,替代方案用shell模塊
[root@m01 ~]# ansible oldboy -m command -a "cat /etc/redhat-release"
172.16.1.7 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
172.16.1.31 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
172.16.1.41 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
[root@m01 ~]# cat /server/scripts/cmd.sh
for n in 31 41
do
echo "=====172.16.1.$n======"
ssh 172.16.1.$n "$1"
done
[root@m01 ~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"
=====172.16.1.31======
CentOS Linux release 7.6.1810 (Core)
=====172.16.1.41======
CentOS Linux release 7.6.1810 (Core)
3)常用參數(shù)說明及實(shí)踐
[root@m01 ~]# ansible-doc -s command
- name: Executes a command on a remote node
command:
argv: # Allows the user to provide the command as a list vs. a string. Only the
string or the list form can be provided, not
both. One or the other must be provided.
chdir: # Change into this directory before running the command.
creates: # A filename or (since 2.0) glob pattern. If it already exists, this step
*won't* be run.
free_form: # (required) The command module takes a free form command to run. There is no
parameter actually named 'free form'. See the
examples!
removes: # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
stdin: # Set the stdin of the command directly to the specified value.
warn: # If command_warnings are on in ansible.cfg, do not warn about this particular
line if set to `no'.
- 參數(shù):chdir=/tmp配置相當(dāng)于cd /tmp
[root@m01 ~]# ansible oldboy -m command -a "pwd chdir=/etc"
ansible oldboy -m shell -a "cd /etc/;pwd"
- 參數(shù):creates=/etc 相當(dāng)于條件測試 [ -e /etc ]||pwd 和下面removes相反
[root@m01 ~]# ansible oldboy -m command -a "pwd creates=/etc"
- 參數(shù):removes=/root 相當(dāng)于條件測試 [ -e /root ]&&ls /root
ansible oldboy -m command -a "ls /root removes=/root"
ansible oldboy -m shell -a "[ -d /etc ]||pwd"
[root@m01 ~]# ansible oldboy -m command -a "cat /etc/hosts removes=/etc/hosts"
- 參數(shù):warn=False 忽略警告
[root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"