101 lines
2.7 KiB
Markdown
101 lines
2.7 KiB
Markdown
|
|
# Ansible 日志管理自动化
|
||
|
|
|
||
|
|
## 目录结构
|
||
|
|
|
||
|
|
```
|
||
|
|
ansible/
|
||
|
|
├── inventory/
|
||
|
|
│ └── hosts.yaml # 服务器清单
|
||
|
|
├── playbooks/
|
||
|
|
│ └── compress_logs.yaml # 日志压缩 playbook
|
||
|
|
├── scripts/
|
||
|
|
│ └── compress_logs.sh # 快捷执行脚本
|
||
|
|
└── README.md
|
||
|
|
```
|
||
|
|
|
||
|
|
## 快速开始
|
||
|
|
|
||
|
|
### 基本用法
|
||
|
|
|
||
|
|
```bash
|
||
|
|
# 必须指定年月日参数
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
-e "year=2025 month=11 start_day=21 end_day=30"
|
||
|
|
|
||
|
|
# 使用便捷脚本
|
||
|
|
./scripts/compress_logs.sh -y 2025 -m 11 -s 21 -e 30
|
||
|
|
|
||
|
|
# 指定单个服务器
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
--limit jhlog -e "year=2025 month=11 start_day=21 end_day=30"
|
||
|
|
```
|
||
|
|
|
||
|
|
**默认行为**:
|
||
|
|
- 压缩日志到 tar.gz
|
||
|
|
- 下载到 `/mnt/d/logs/<host_name>/<year>/<month>/`
|
||
|
|
- 删除原始文件夹和远程压缩包
|
||
|
|
- 支持断点续传
|
||
|
|
|
||
|
|
## 可配置参数
|
||
|
|
|
||
|
|
| 参数 | 默认值 | 说明 |
|
||
|
|
|------|--------|------|
|
||
|
|
| `year` | **必填** | 年份 |
|
||
|
|
| `month` | **必填** | 月份 |
|
||
|
|
| `start_day` | **必填** | 起始日期 |
|
||
|
|
| `end_day` | **必填** | 结束日期 |
|
||
|
|
| `log_base_path` | `/data/roslog/roslog` | 日志路径 |
|
||
|
|
| `local_download_path` | `/mnt/d/logs` | 本地下载路径 |
|
||
|
|
| `remove_after_compress` | `true` | 删除原文件夹 |
|
||
|
|
| `download_to_local` | `true` | 下载到本地 |
|
||
|
|
| `remove_local_archives` | `true` | 删除远程压缩包 |
|
||
|
|
|
||
|
|
## 常用场景
|
||
|
|
|
||
|
|
### 只压缩不删除
|
||
|
|
|
||
|
|
```bash
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
-e "year=2025 month=11 start_day=21 end_day=30 remove_after_compress=false"
|
||
|
|
```
|
||
|
|
|
||
|
|
### 只压缩不下载
|
||
|
|
|
||
|
|
```bash
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
-e "year=2025 month=12 start_day=1 end_day=31 download_to_local=false"
|
||
|
|
```
|
||
|
|
|
||
|
|
### 保留远程压缩包
|
||
|
|
|
||
|
|
```bash
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
-e "year=2025 month=11 start_day=1 end_day=20 remove_local_archives=false"
|
||
|
|
```
|
||
|
|
|
||
|
|
## 断点续传
|
||
|
|
|
||
|
|
使用 rsync 实现断点续传,网络中断后重新执行会自动从中断点继续下载。
|
||
|
|
|
||
|
|
## 注意事项
|
||
|
|
|
||
|
|
- 必须显式指定时间参数
|
||
|
|
- 确保服务器上安装了 rsync
|
||
|
|
- 确保有足够的磁盘空间
|
||
|
|
- 首次使用建议小范围测试
|
||
|
|
|
||
|
|
## 测试建议
|
||
|
|
|
||
|
|
```bash
|
||
|
|
# check 模式
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
--limit jhlog \
|
||
|
|
-e "year=2025 month=11 start_day=21 end_day=21 remove_after_compress=false" \
|
||
|
|
--check
|
||
|
|
|
||
|
|
# 单天测试
|
||
|
|
ansible-playbook -i inventory/hosts.yaml playbooks/compress_logs.yaml \
|
||
|
|
--limit jhlog \
|
||
|
|
-e "year=2025 month=11 start_day=21 end_day=21 remove_after_compress=false"
|
||
|
|
```
|