Files
log-fetch/README.md

53 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2025-12-13 13:18:58 +08:00
# Ansible 日志管理自动化
## 目录结构
```
ansible/
├── inventory/
│ └── hosts.yaml # 服务器清单
├── playbooks/
│ └── compress_logs.yaml # 日志压缩 playbook
├── scripts/
│ └── compress_logs.sh # 快捷执行脚本
└── README.md
```
2026-02-27 15:10:12 +08:00
## 使用方式
2025-12-13 13:18:58 +08:00
2026-02-27 15:10:12 +08:00
### 1. linux 环境
ansible 在 windows 环境中运行不稳定,最好在 linux 环境中运行。如果要下载到本地,最好的方法是在 wsl 中安装 ansible
### 2. python 环境
ansible 依赖于 python 环境,在安装前需要确保系统里已经安装了 python。
ansible 在运行时会连接到目标机器进行操作,因此需要确定好本机和目标机器的 python 版本兼容性。ansble 官网列出了一个[版本支持矩阵](https://docs.ansible.com/projects/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix),根据这个表格确定需要安装的 python 版本和 ansible 版本
由于当前项目要操作的服务器中,最低版本的 python 为 3.8centos 预置),因此主机(控制机)要使用的 python 版本要在 3.8 - 3.13 之间。linux 系统中一般都会预置特定版本的 python如果主机预置的 python 版本不在范围内,需要手动安装范围内版本的 python不同 linux 发行版的安装方式略有差异。
### 3. 安装 ansible
ansible 有两个分支社区版本ansible和核心版本ansible-core现在项目适配的 ansible 版本为 ansible-core 2.19(适用于 python 3.8 - 3.13
```bash
pipx install ansible-core==2.19
```
### 4. 执行脚本
ansible 的执行命令比较繁琐,有一个简单的脚本文件包装了 ansible 的命令,方便在执行时传参
2025-12-13 13:18:58 +08:00
```bash
2025-12-19 17:52:11 +08:00
# 将拉取 s - e 之间日期的所有日志,会断点续传
2026-02-27 15:10:12 +08:00
# 月份和日期必须是两位数!
2025-12-19 17:52:11 +08:00
./scripts/compress_logs.sh -y 2025 -m 12 -s 01 -e 10
2025-12-13 13:18:58 +08:00
```
2026-02-27 15:10:12 +08:00
## 注意事项
脚本中开头有定义参数,尽量列出了有必要的变量用于灵活修改,但是还有些硬编码的部分,需要根据实际情况进行修改。
一次性不要拉太多日志,由于拉取流程中需要在服务器上压缩一次日志,要确保服务器空间有足够空余