運(yùn)維效率翻倍:帶你認(rèn)識 Ansible 最常用的 14 個模塊
Ansible 是一款開源的 自動化運(yùn)維工具,主要用于配置管理、應(yīng)用部署、任務(wù)自動化和持續(xù)交付。Ansible 對于運(yùn)維工作有多重要性,已經(jīng)不需要再多言,掌握它的使用如同打開了自動化的大門。
本文將介紹Ansible在運(yùn)維工作中最常用的14個模塊,帶你實(shí)現(xiàn)工作效率的翻倍。
一、基礎(chǔ)連接與測試模塊
1. ping模塊
測試與目標(biāo)主機(jī)的連接性:
ansible all -m ping
示例輸出:
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
2. command模塊(不支持shell特性)
在遠(yuǎn)程主機(jī)執(zhí)行命令:
ansible webservers -m command -a "uptime"
常用參數(shù):
- - `chdir`:執(zhí)行前切換目錄
- - `creates`:如果文件存在則不執(zhí)行
- - `removes`:如果文件不存在則不執(zhí)行
示例:
ansible db -m command -a "mysqldump -u root -p password dbname > backup.sql chdir=/backups"
3. shell模塊
在遠(yuǎn)程主機(jī)通過shell執(zhí)行命令(支持管道、重定向等):
ansible all -m shell -a "df -h | grep /dev/sda1"
二、文件操作模塊
4. copy模塊
復(fù)制本地文件到遠(yuǎn)程主機(jī):
ansible webservers -m copy -a "src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf owner=root group=root mode=0644 backup=yes"
5. file模塊
管理文件屬性或創(chuàng)建文件/目錄/鏈接:
- 創(chuàng)建目錄:
ansible app -m file -a "path=/opt/myapp state=directory mode=0755"
- 創(chuàng)建軟鏈接:
ansible all -m file -a "src=/etc/nginx/nginx.conf dest=/tmp/nginx.conf state=link"
- 刪除文件:
ansible all -m file -a "path=/tmp/testfile state=absent"
6. stat模塊
獲取文件狀態(tài)信息:
ansible webservers -m stat -a "path=/etc/nginx/nginx.conf"
輸出示例:
{
"changed": false,
"stat": {
"exists": true,
"gid": 0,
"group": "root",
"mode": "0644",
"mtime": 1634567890.1234567,
"path": "/etc/nginx/nginx.conf",
"size": 1024,
"uid": 0,
"owner": "root"
}
}
三、軟件包管理模塊
7. yum模塊 (RHEL/CentOS)
安裝包:
ansible centos_servers -m yum -a "name=nginx state=present"
更新所有包:
ansible centos_servers -m yum -a "name=* state=latest"
刪除包:
ansible centos_servers -m yum -a "name=nginx state=absent"
8. apt模塊 (Debian/Ubuntu)
安裝包:
ansible ubuntu_servers -m apt -a "name=nginx state=present update_cache=yes"
刪除包:
ansible ubuntu_servers -m apt -a "name=nginx state=absent"
四、系統(tǒng)服務(wù)管理
9. service模塊
啟動服務(wù):
ansible webservers -m service -a "name=nginx state=started enabled=yes"
重啟服務(wù):
ansible webservers -m service -a "name=nginx state=restarted"
停止服務(wù):
ansible webservers -m service -a "name=nginx state=stopped"
五、用戶與組管理
10. user模塊
創(chuàng)建用戶:
ansible all -m user -a "name=testuser uid=1000 group=admin create_home=yes shell=/bin/bash"
刪除用戶:
ansible all -m user -a "name=testuser state=absent remove=yes"
11. group模塊
創(chuàng)建組:
ansible all -m group -a "name=admin gid=1000 state=present"
刪除組:
ansible all -m group -a "name=admin state=absent"
六、常用高級模塊
12. setup模塊
收集主機(jī)系統(tǒng)信息:
ansible all -m setup
過濾特定信息:
ansible all -m setup -a "filter=ansible_distribution*"
13. cron模塊
添加cron任務(wù):
ansible all -m cron -a "name='daily backup' minute=0 hour=2 job='/usr/local/bin/backup.sh'"
刪除cron任務(wù):
ansible all -m cron -a "name='daily backup' state=absent"
14. lineinfile模塊
確保某行存在:
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config line='PermitRootLogin no' regexp='^PermitRootLogin'"
刪除某行:
ansible all -m lineinfile -a "path=/etc/hosts state=absent line='127.0.0.1 badhost'"
七. 實(shí)際使用技巧
(1) 查看模塊幫助文檔:
ansible-doc copy
(2) 限制執(zhí)行主機(jī):
ansible webservers[0] -m ping # 只對webservers組第一個主機(jī)執(zhí)行
(3) 并行執(zhí)行控制:
ansible all -m ping -f 10 # 使用10個并行進(jìn)程
(4) 使用become提權(quán):
ansible all -m yum -a "name=nginx state=present" --become --ask-become-pass
(5) 調(diào)試模式:
ansible all -m command -a "ls /nonexistent" --check -vvv
掌握這些常用模塊的命令行用法,可以快速完成日常運(yùn)維任務(wù),提高工作效率。當(dāng)然,對于更加復(fù)雜的任務(wù),建議還是使用Playbook來實(shí)現(xiàn)更結(jié)構(gòu)化的自動化管理。