Files
log-fetch/playbooks/compress_logs.yaml

114 lines
3.6 KiB
YAML
Raw Normal View History

2025-12-17 16:18:59 +08:00
---
2025-12-19 17:52:11 +08:00
- name: 自动拉取服务器日志
2025-12-17 16:18:59 +08:00
hosts: logs
gather_facts: false
vars:
# 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
tasks:
2025-12-19 17:52:11 +08:00
- name: 缺少必填参数
2025-12-17 16:18:59 +08:00
ansible.builtin.fail:
msg: "缺少必填参数!必须提供: year, month, start_day, end_day"
when: year is not defined or month is not defined or start_day is not defined or end_day is not defined
2025-12-19 17:52:11 +08:00
- name: 检查日志目录
2025-12-17 16:18:59 +08:00
ansible.builtin.stat:
path: "{{ log_base_path }}/{{ year }}/{{ month }}"
register: log_dir
2025-12-19 17:52:11 +08:00
- name: 日志目录不存在
2025-12-17 16:18:59 +08:00
ansible.builtin.fail:
msg: "日志目录 {{ log_base_path }}/{{ year }}/{{ month }} 不存在"
when: not log_dir.stat.exists
2025-12-19 17:52:11 +08:00
- name: 压缩日志
2025-12-17 16:18:59 +08:00
ansible.builtin.shell: |
cd {{ log_base_path }}/{{ year }}/{{ month }}
for day in $(seq -f "%02g" {{ start_day }} {{ end_day }}); do
if [ -d "$day" ]; then
echo "压缩目录: $day"
tar -czf ${day}.tar.gz $day
else
echo "目录 $day 不存在,跳过"
fi
done
args:
executable: /bin/bash
become: true
register: compress_result
2025-12-19 17:52:11 +08:00
- name: 展示压缩结果
2025-12-17 16:18:59 +08:00
ansible.builtin.debug:
var: compress_result.stdout_lines
2025-12-19 17:52:11 +08:00
- name: 删除原始日志
2025-12-17 16:18:59 +08:00
ansible.builtin.shell: |
cd {{ log_base_path }}/{{ year }}/{{ month }}
for day in $(seq -f "%02g" {{ start_day }} {{ end_day }}); do
if [ -d "$day" ] && [ -f "${day}.tar.gz" ]; then
echo "删除原始目录: $day"
rm -rf $day
2025-12-19 17:52:11 +08:00
else
echo "文件 ${day} 或 ${day}.tar.gz 不存在,跳过"
2025-12-17 16:18:59 +08:00
fi
done
args:
executable: /bin/bash
become: true
2025-12-19 17:52:11 +08:00
when: remove_after_compress | bool
register: delete_result
2025-12-17 16:18:59 +08:00
2025-12-19 17:52:11 +08:00
- name: 展示删除结果
ansible.builtin.debug:
var: delete_result.stdout_lines
- name: 收集压缩文件
ansible.builtin.find:
paths: "{{ log_base_path }}/{{ year }}/{{ month }}"
patterns: "*.tar.gz"
register: compressed_files
- name: 检查下载目录
2025-12-17 16:18:59 +08:00
ansible.builtin.file:
path: "{{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}"
state: directory
delegate_to: localhost
2025-12-19 17:52:11 +08:00
when: download_to_local | bool
2025-12-17 16:18:59 +08:00
become: false
2025-12-19 17:52:11 +08:00
- name: 下载到本地
2025-12-17 16:18:59 +08:00
ansible.posix.synchronize:
src: "{{ log_base_path }}/{{ year }}/{{ month }}/{{ item.path | basename }}"
dest: "{{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}/"
mode: pull
rsync_opts:
- "--partial"
- "--progress"
with_items: "{{ compressed_files.files }}"
2025-12-19 17:52:11 +08:00
when: download_to_local | bool
2025-12-17 16:18:59 +08:00
2025-12-19 17:52:11 +08:00
- name: 删除压缩文件
2025-12-17 16:18:59 +08:00
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
with_items: "{{ compressed_files.files }}"
2025-12-19 17:52:11 +08:00
when: remove_local_archives | bool and download_to_local | bool
2025-12-17 16:18:59 +08:00
become: true
- name: 完成信息
ansible.builtin.debug:
msg: |
日志处理完成!
- 服务器: {{ label }} ({{ inventory_hostname }})
- 路径: {{ log_base_path }}/{{ year }}/{{ month }}
- 日期范围: {{ start_day }}-{{ end_day }}
- 压缩文件数: {{ compressed_files.files | length }}
{% if download_to_local %}
- 本地下载路径: {{ local_download_path }}/{{ label }}/{{ year }}/{{ month }}
{% endif %}