CentOS Stream 8 自动化运维工具与落地实践
一 工具全景与选型建议
二 快速上手 Ansible
sudo dnf install -y epel-releasesudo dnf install -y ansibleansible --version/etc/ansible/hosts[web]
192.168.10.11
192.168.10.12
[db]
db01.example.com
ansible all -m pinginstall_nginx.yml---
- name: Ensure Nginx is installed and enabled
hosts: web
become: yes
tasks:
- name: Install EPEL repository
ansible.builtin.dnf:
name: epel-release
state: present
- name: Install Nginx
ansible.builtin.dnf:
name: nginx
state: present
- name: Enable and start Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: yes
ansible-playbook -i /etc/ansible/hosts install_nginx.ymlansible-galaxy role install geerlingguy.nginx三 监控与告警工具组合
top/htop、vmstat、free -hdf -h、iostat(需 sysstat)ss、iftop(需 EPEL)、dstatsar(需 sysstat)四 自动化巡检与定时任务
check_sys.sh)#!/usr/bin/env bash
echo "=== $(date) ==="
echo "Uptime: $(uptime -p)"
echo "CPU Load: $(cat /proc/loadavg | awk '{print $1}')"
echo "Memory: $(free -h | awk '/^Mem/{printf "%.2f%% used\n", $3/$2*100}')"
echo "Disk: $(df -h / | awk 'NR==2{gsub(/%/,""); print $5"%"}')"
echo "Top 5 CPU processes:"
ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | head -6
chmod +x check_sys.sh0 9 * * * /usr/bin/bash /root/check_sys.sh | mail -s "Daily System Check $(date +\%F)" ops@example.com
logrotate)与告警通道(邮件、企业微信、钉钉、Webhook)形成闭环。五 实践建议