## 1.什么是自動(dòng)化批量管理
重復(fù)性工作與內(nèi)容: 思考如何自動(dòng)化完成.
部署環(huán)境,批量查看信息,批量檢查:自動(dòng)化
一般步驟:
1.如何手動(dòng)實(shí)現(xiàn)
2.如何自動(dòng)化管理工具,批量實(shí)現(xiàn)
3.注意事項(xiàng):想要自動(dòng)化一定要先標(biāo)準(zhǔn)化(所有環(huán)境,軟件,目錄一致)
4.注意事項(xiàng):盡量進(jìn)行分組(比如:所有web服務(wù)器是1組)
2.自動(dòng)化工具選擇
自動(dòng)化批量管理工具 | ||
---|---|---|
Ansible | 基于python語(yǔ)言編寫(xiě),使用極其簡(jiǎn)單,不需要客戶(hù)端 | |
Saltstack | 基于python語(yǔ)言編寫(xiě),需要安裝客戶(hù)端 | |
TereForm | 批量管理平臺(tái),批量創(chuàng)建阿里云服務(wù)器,批量創(chuàng)建aws服務(wù)器 | |
Fabric | python使用它 | |
Chef | 了解即可 | |
puppet | 古老一些的批量管理工具 | |
… |
3.Ansible架構(gòu)
4.Ansible極速使用指南
4.1 環(huán)境準(zhǔn)備
ansible環(huán)境 | ||
---|---|---|
ansible管理端 | m01,10.0.0.210 | |
被管理機(jī)器 | web01 10.0.0.7 | |
被管理機(jī)器 | backup 10.0.0.41 | |
被管理機(jī)器 | nfs01 10.0.0.31 | |
被管理機(jī)器 | db01 10.0.0.51 |
sh /server/scripts/ssh_check.sh hostname
4.2 ansible部署
m01
yum install -y ansible
#epel源中的軟件包
4.3 初步配置主機(jī)清單(inventory)
root@m01 /etc/ansible]# cat hosts
[hbinz]
172.16.1.7
172.16.1.31
172.16.1.41
172.16.1.51
4.4與ansible的第一次接觸
ansible hbinz -m ping
#第一次會(huì)提示yes/no
4.5 小結(jié)
1.密鑰認(rèn)證
2.環(huán)境準(zhǔn)備,部署ansible
3.修改ansible 主機(jī)清單
4.測(cè)試,ansible hbinz -m ping
ansible命令格式 | |||
---|---|---|---|
ansible | 主機(jī)分組 | -m指定模塊 | |
ansible | 主機(jī)分組 | -m指定模塊 | -a 指定模塊中的選項(xiàng) |
module | action |
5.ansible配置文件
ansible.cfg #ansible配置文件
#71:host_key_checking = False 主機(jī)校驗(yàn)關(guān)掉
hosts #ansible默認(rèn)的主機(jī)清單
6.ansible主機(jī)清單
6.1 基本格式
[root@m01 /etc/ansible]# cat hosts
[hbinz] #[組的名字]
172.16.1.7
172.16.1.31
172.16.1.41
172.16.1.51
[web]
172.16.1.7
[db]
172.16.1.51
[nfs]
172.16.1.31
[backup]
172.16.1.41
6.2指定用戶(hù)名,密碼,端口
主機(jī)清單中指定信息 | ||
---|---|---|
連接的ssh端口 | ansible_ssh_root=22 | |
連接的ssh端口 | ansible_ssh_user=root | |
連接的ssh端口 | ansible_ssh_pass=‘thinker’ |
[web]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
[db]
172.16.1.51
[nfs]
172.16.1.31
[backup]
172.16.1.41
6.3 子組
我想把多個(gè)主機(jī)分組進(jìn)行合并,比如把db,nfs,backup合并成一個(gè)組叫data組.
[web]
172.16.1.7
[db]
172.16.1.51
[nfs]
172.16.1.31
[backup]
172.16.1.41
[data:children] #data:children表示data是創(chuàng)建的子組,組里面包含 db,nfs,backup3個(gè)組.
db
nfs
backup
6.4主機(jī)清單案例
1)案例1:查看與使用指定的子組 dataansible data -m command -a 'hostname'
2)案例2:查看指定的組dbansible db -m command -a 'hostname'
3)案例3:查看所有主機(jī)情況ansible all -m command -a 'hostname'
4)案例4:查看某一機(jī)器ansible 172.16.1.7 -m command -a 'hostname'
7.ansible核心模塊
7.1模塊概述
模塊分類(lèi) | |
---|---|
命令和腳本模塊 | command模塊,默認(rèn)的模塊,執(zhí)行簡(jiǎn)單命令,不支持特殊符號(hào) |
shell模塊,執(zhí)行命令,支持特殊符號(hào) | |
script模塊,分發(fā)腳本并執(zhí)行 | |
文件 | file,創(chuàng)建目錄,文件,軟鏈接 |
copy,遠(yuǎn)程分發(fā)文件,修改權(quán)限,所有者,備份 | |
服務(wù) | systemd服務(wù)管理 |
service服務(wù)管理(了解) | |
軟件包 | yum源,yum repository |
yum命令 | |
get_url下載軟件 | |
系統(tǒng)管理 | mount模塊,掛載 |
cron模塊,定時(shí)任務(wù) | |
用戶(hù)管理 | group模塊,管理用戶(hù)組 |
user模塊,管理用戶(hù) | |
其他可以研究 | 壓縮解壓(unarchive),rsync模塊(synchronize),數(shù)據(jù)庫(kù)模塊(mysql_db,mysql_user)… |
其他 | ansible管理docker k8s zabbix grafana… |
用于調(diào)試模塊 | ping 模塊檢查,ansible與其他節(jié)點(diǎn)連通性 |
debug模塊,檢查/顯示變量 |
7.2如何查詢(xún)幫助
通過(guò)命令: ansible-doc -s command
查看官網(wǎng):
傳送門(mén)
7.3ansible命令與腳本類(lèi)模塊
1)command模塊
僅支持簡(jiǎn)單命令,不支持特殊符號(hào),管道....
? 這個(gè)模塊是默認(rèn)模塊,ansible不加上模塊,默認(rèn)就使用這個(gè)模塊.
ansible all -m command -a '命令'
ansible all -a 'hostname' #相當(dāng)于省略 -m command
2)shell模塊
與command類(lèi)似,shell模塊支持特殊符號(hào),執(zhí)行腳本...
ansible all -m shell -a 'ip a s eth0|sed -n 3p' #command不支持特殊符號(hào)
- ansible顏色
- 綠色表示正常
- 黃色表示執(zhí)行正常,狀態(tài)變化
- 紅色表示錯(cuò)誤,輸出錯(cuò)誤信息
- 紫色表示警告,建議
3)script模塊-傳輸腳本到被管理端并執(zhí)行腳本
傳輸腳本
執(zhí)行腳本
安裝ipvsadm
cat /server/scripts/yum.sh
yum install -y ipvsadm
#安裝
ansible db -m script -a '/server/scripts/yum.sh'
#檢查
ansible db -m shell -a 'rpm -qa|grep ipvsadm'
[root@m01 /etc/ansible]# ansible db -m shell -a 'rpm -qa|grep ipvsadm'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task
or set 'command_warnings=False' in ansible.cfg to get rid of this message.
172.16.1.51 | CHANGED | rc=0 >>
ipvsadm-1.27-8.el7.x86_64
4)命令與腳本模塊
模塊 | 含義 | 應(yīng)用 |
---|---|---|
command模塊 | 執(zhí)行命令 | 簡(jiǎn)單命令,不含特殊符號(hào),默認(rèn)的模塊ansible all -a 'hostname'
|
shell模塊 | 執(zhí)行命令或腳本 | 執(zhí)行含有特殊符號(hào):管道,反引號(hào),{}的命令,運(yùn)行腳本(腳本在管理端). |
script模塊 | 先傳輸腳本,然后運(yùn)行腳本 | 一般用于執(zhí)行腳本 |
7.4ansible-文件與目錄管理模塊
- file 創(chuàng)建文件,目錄
- copy 遠(yuǎn)程傳輸文件,目錄,類(lèi)似于scp
1)file模塊管理文件或目錄,軟鏈接
file模塊中的選項(xiàng) | |
---|---|
path | 路徑(目錄,文件)必須要寫(xiě) |
src(source源) | 源文件一般用于link(創(chuàng)建軟鏈接模式)用于指定源文件 |
state | (狀態(tài))(模式)state=directory 創(chuàng)建目錄state=file (默認(rèn))更新文件,如果文件不存在也不創(chuàng)建state=link 創(chuàng)建軟鏈接state=touch 創(chuàng)建文件state=absent 刪除 |
案例01-創(chuàng)建目錄/hbinz/目錄
ansible all -m file -a 'path=/hbinz state=directory'
案例02-創(chuàng)建文件/hbinz/hbinz.txt文件
ansible all -m file -a 'path=/hbinz/hbinz.txt state=touch'
ansible all -a 'ls -l /hbinz'
案例03-創(chuàng)建軟鏈接 /hbinz/hbinz.txt到/tmp/hbinz.txt.soft
ansible all -m file -a 'src=/hbinz/hbinz.txt path=/tmp/hbinz.txt.soft state=link'
ansible all -a 'ls -l /tmp/hbinz.txt.soft'
案例04-刪除文件、目錄、軟鏈接
ansible all -m file -a 'path=/hbinz/hbinz.txt state=absent'
ansible all -m file -a 'path=/hbinz state=absent'
ansible all -m file -a 'path=/tmp/hbinz.txt.soft state=absent'
案例05-創(chuàng)建文件/tmp/hbinz.txt,所有者root,用戶(hù)組root,權(quán)限755
ansible all -m file -a 'path=/tmp/hbinz.txt owner=root group=root mode=755 state=touch'
ansible all -a 'ls -l /tmp/oldboy.txt'
2)copy遠(yuǎn)程傳輸模塊
copy模塊 | |
---|---|
src | source源文件 |
dest | destination目標(biāo) |
backup | backup=yes 則會(huì)在覆蓋前備份 |
mode | 修改權(quán)限 |
owner | 修改為指定所有者 |
group | 修改為指定用戶(hù)組 |
案例01-傳輸/etc/hosts文件到/etc/hosts
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts'
案例02–傳輸/etc/hosts文件到/etc/hosts-先備份然后修改
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'
3)文件傳輸小結(jié)
文件管理與傳輸 | 含義 |
---|---|
file | 創(chuàng)建/刪除 文件/目錄,軟鏈接 |
copy | 遠(yuǎn)程分發(fā)文件/目錄,軟件包,壓縮包… |
7.5ansible-服務(wù)管理模塊
就是用了systemctl 命令
啟動(dòng)/關(guān)閉/重啟服務(wù)
開(kāi)機(jī)自啟動(dòng)/開(kāi)機(jī)不自啟動(dòng)
1)systemd
systemd | |
---|---|
name | 用于指定服務(wù)名稱(chēng) |
enabled | 控制服務(wù)的開(kāi)機(jī)自啟動(dòng) enabled=yes /enable=no |
state | 表示服務(wù)開(kāi),關(guān),重啟…state=started 開(kāi)啟state=stopped 關(guān)閉state=reloaded 重讀配置文件(服務(wù)支持的情況下sshd nfs nginx)state=restarted 重啟(關(guān)閉再開(kāi)啟) |
daemon-reload | yes是否重新加載對(duì)應(yīng)的服務(wù)的管理配置文件(講解了systemctl配置文件.) |
案例01-關(guān)閉firewalld |
ansible all -m systemd -a 'name=firewalld enabled=no state=stopped'
ansible all -a 'systemctl state firewalld'
案例02-開(kāi)啟sshd服務(wù)
ansible all -m systemd -a 'name=sshd enabled=yes stat=started'
ansible all -a 'systemctl state sshd'
案例03-重啟backup這臺(tái)機(jī)器上面的rsync服務(wù)
ansible backup -m systemd -a 'name=rsyncd state=restarted'
2)service文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-836846.html
服務(wù)管理模塊 | systemd模塊 | service模塊 |
---|---|---|
開(kāi)機(jī)自啟動(dòng) | enabled | enabled |
服務(wù)名稱(chēng) | name | name |
服務(wù)開(kāi)關(guān)重啟 | state | state |
運(yùn)行級(jí)別 | 無(wú) | runlevel(運(yùn)行級(jí)別) |
重啟加載systemctl配置 | daemon_reload=yes systemctl daemon-reload
|
無(wú) |
3)服務(wù)管理模塊小結(jié)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-836846.html
模塊 | ||
---|---|---|
systemd | 管理linux 紅帽7以上推薦使用,ubuntu… | |
service | 6及一下 |
到了這里,關(guān)于運(yùn)維SRE-15 自動(dòng)化批量管理-ansible1的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!