運維效率翻倍:帶你認識 Ansible 最常用的 14 個模塊
Ansible 是一款開源的 自動化運維工具,主要用于配置管理、應用部署、任務自動化和持續交付。Ansible 對于運維工作有多重要性,已經不需要再多言,掌握它的使用如同打開了自動化的大門。
本文將介紹Ansible在運維工作中最常用的14個模塊,帶你實現工作效率的翻倍。
一、基礎連接與測試模塊
1. ping模塊
測試與目標主機的連接性:
ansible all -m ping
示例輸出:
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
2. command模塊(不支持shell特性)
在遠程主機執行命令:
ansible webservers -m command -a "uptime"
常用參數:
- - `chdir`:執行前切換目錄
- - `creates`:如果文件存在則不執行
- - `removes`:如果文件不存在則不執行
示例:
ansible db -m command -a "mysqldump -u root -p password dbname > backup.sql chdir=/backups"
3. shell模塊
在遠程主機通過shell執行命令(支持管道、重定向等):
ansible all -m shell -a "df -h | grep /dev/sda1"
二、文件操作模塊
4. copy模塊
復制本地文件到遠程主機:
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模塊
管理文件屬性或創建文件/目錄/鏈接:
- 創建目錄:
ansible app -m file -a "path=/opt/myapp state=directory mode=0755"
- 創建軟鏈接:
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模塊
獲取文件狀態信息:
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"
四、系統服務管理
9. service模塊
啟動服務:
ansible webservers -m service -a "name=nginx state=started enabled=yes"
重啟服務:
ansible webservers -m service -a "name=nginx state=restarted"
停止服務:
ansible webservers -m service -a "name=nginx state=stopped"
五、用戶與組管理
10. user模塊
創建用戶:
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模塊
創建組:
ansible all -m group -a "name=admin gid=1000 state=present"
刪除組:
ansible all -m group -a "name=admin state=absent"
六、常用高級模塊
12. setup模塊
收集主機系統信息:
ansible all -m setup
過濾特定信息:
ansible all -m setup -a "filter=ansible_distribution*"
13. cron模塊
添加cron任務:
ansible all -m cron -a "name='daily backup' minute=0 hour=2 job='/usr/local/bin/backup.sh'"
刪除cron任務:
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'"
七. 實際使用技巧
(1) 查看模塊幫助文檔:
ansible-doc copy
(2) 限制執行主機:
ansible webservers[0] -m ping # 只對webservers組第一個主機執行
(3) 并行執行控制:
ansible all -m ping -f 10 # 使用10個并行進程
(4) 使用become提權:
ansible all -m yum -a "name=nginx state=present" --become --ask-become-pass
(5) 調試模式:
ansible all -m command -a "ls /nonexistent" --check -vvv
掌握這些常用模塊的命令行用法,可以快速完成日常運維任務,提高工作效率。當然,對于更加復雜的任務,建議還是使用Playbook來實現更結構化的自動化管理。