news 2026/6/23 11:45:15

采用ansible收集多个centos6主机的一个特定日志文件vsftpd.log的后3000行

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
采用ansible收集多个centos6主机的一个特定日志文件vsftpd.log的后3000行

因维护需要、要到多个centos6主机去检查某个特定日志文件vsftpd.log的后3000行,用于分析ftp服务器的可维护时间窗口。一台一台登录去处理太慢,为提高效率,采用ansible批量处理。

具体使用方法如下:

基础环境

# lsb_release -aLSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release7.6.1810(Core)Release:7.6.1810 Codename: Core

ansible版本

因要采集的是一批centos6主机的日志文件,故ansible版本不宜过高,采用一台centos7.6默认安装的ansible。

# ansible --versionansible2.9.27 configfile=/etc/ansible/ansible.cfg configured module search path=[u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location=/usr/lib/python2.7/site-packages/ansible executable location=/usr/bin/ansible python version=2.7.5(default, Nov142023,16:14:06)[GCC4.8.520150623(Red Hat4.8.5-44)]

配置ansible信任

ssh-keygen -t rsa -b 2048
ssh-copy-id root@10.128.1.1
ssh-copy-id root@10.128.1.2
ssh-copy-id root@10.128.1.3
ssh-copy-id root@10.128.1.4
ssh-copy-id root@10.128.1.5

ansible要用到的主机组

# cat hosts.ini[vsftpdHosts]10.128.1.110.128.1.210.128.1.310.128.1.410.128.1.5

要用到的yml

功能主要是从远程主机取得/data/log_vsftpd/vsftpd.log的后3000行,并取回本调度机,放在logs目录下,用{remote_IP}_vsftpd.log为区别。

vifetch_vsftpd_logs.yml.run --- - name: Collect vsftpd logs from all hosts hosts: vsftpdHosts become:yestasks: - name: Checkiflogfileexists stat: path: /data/log_vsftpd/vsftpd.log register: log_file_check - name: Get last3000lines of vsftpd log shell:tail-n3000/data/log_vsftpd/vsftpd.log register: log_content when: log_file_check.stat.exists ignore_errors:yes- name: Verify log content length debug: msg:"Log content has {{ log_content.stdout | length }} characters"when: log_content.stdout is defined# ---------- 本地目录 & 文件 ----------- name: Createlocallogs directoryifnot exists ansible.builtin.file: path: ./logs state: directory mode:'0755'delegate_to: localhost delegate_to: localhost - name: Write log content tolocalfileansible.builtin.copy: content:"{{ log_content.stdout }}"dest:"./logs/{{ inventory_hostname }}_vsftpd.log"mode:'0644'delegate_to: localhost when: log_content.stdout is defined and log_content.stdout|length>0- name: Checklocallogfileexistence stat: path:"./logs/{{ inventory_hostname }}_vsftpd.log"register: local_file_check delegate_to: localhost - name: Displayfilestatus debug: msg:"File exists: {{ local_file_check.stat.exists }}, Size: {{ local_file_check.stat.size }} bytes"when: local_file_check is defined

ansible-playbook -i hosts.ini fetch_vsftpd_logs.yml.run -u root

执行日志如下:

PLAY[Collect vsftpd logs from all hosts]************************************************************************************************ TASK[Gathering Facts]******************************************************************************************************************* ok:[10.128.1.1]ok:[10.128.1.2]ok:[10.128.1.3]ok:[10.128.1.4]ok:[10.128.1.5]fatal:[10.128.1.6]: UNREACHABLE!=>{"changed":false,"msg":"Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).","unreachable":true}TASK[Checkiflogfileexists]********************************************************************************************************** ok:[10.128.1.1]ok:[10.128.1.2]ok:[10.128.1.3]ok:[10.128.1.4]ok:[10.128.1.5]TASK[Get last3000lines of vsftpd log]************************************************************************************************* changed:[10.128.1.1]changed:[10.128.1.2]changed:[10.128.1.3]changed:[10.128.1.4]changed:[10.128.1.5]TASK[Verify log content length]********************************************************************************************************* ok:[10.128.1.1]=>{"msg":"Log content has 323400 characters"}ok:[10.128.1.2]=>{"msg":"Log content has 290198 characters"}ok:[10.128.1.3]=>{"msg":"Log content has 303307 characters"}ok:[10.128.1.4]=>{"msg":"Log content has 334068 characters"}ok:[10.128.1.5]=>{"msg":"Log content has 383272 characters"}TASK[Createlocallogs directoryifnot exists]***************************************************************************************** changed:[10.128.1.1 ->localhost]ok:[10.128.1.2 ->localhost]ok:[10.128.1.3 ->localhost]ok:[10.128.1.4 ->localhost]ok:[10.128.1.5 ->localhost]TASK[Write log content tolocalfile]*************************************************************************************************** changed:[10.128.1.1 ->localhost]changed:[10.128.1.2 ->localhost]changed:[10.128.1.3 ->localhost]changed:[10.128.1.4 ->localhost]changed:[10.128.1.5 ->localhost]TASK[Checklocallogfileexistence]**************************************************************************************************** ok:[10.128.1.1 ->localhost]ok:[10.128.1.2 ->localhost]ok:[10.128.1.3 ->localhost]ok:[10.128.1.4 ->localhost]ok:[10.128.1.5 ->localhost]TASK[Displayfilestatus]*************************************************************************************************************** ok:[10.128.1.1]=>{"msg":"File exists: True, Size: 152225 bytes"}ok:[10.128.1.2]=>{"msg":"File exists: True, Size: 362393 bytes"}ok:[10.128.1.3]=>{"msg":"File exists: True, Size: 372055 bytes"}ok:[10.128.1.4]=>{"msg":"File exists: True, Size: 457034 bytes"}ok:[10.128.1.5]=>{"msg":"File exists: True, Size: 338930 bytes"}PLAY RECAP *******************************************************************************************************************************10.128.1.1:ok=8changed=2unreachable=0failed=0skipped=0rescued=0ignored=010.128.1.2:ok=8changed=2unreachable=0failed=0skipped=0rescued=0ignored=010.128.1.3:ok=8changed=3unreachable=0failed=0skipped=0rescued=0ignored=010.128.1.4:ok=8changed=2unreachable=0failed=0skipped=0rescued=0ignored=010.128.1.5:ok=8changed=2unreachable=0failed=0skipped=0rescued=0ignored=0

取得的日志文件

会将vsftpdHosts组下的主机的/data/log_vsftpd/vsftpd.log的后3000行,采集到本机的当前logs目录下,

-rw-r--r--1root root323400121113:5610.128.1.1_vsftpd.log -rw-r--r--1root root290198121113:5610.128.1.2_vsftpd.log -rw-r--r--1root root303307121113:5610.128.1.3_vsftpd.log -rw-r--r--1root root334068121113:5610.128.1.4_vsftpd.log -rw-r--r--1root root383272121113:5610.128.1.5_vsftpd.log
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 9:09:22

07FlyLTAS旅游地接社ERP系统实际业务中的核心应用场景

地接社 ERP 系统功能清单在实际业务中的核心应用场景,对应功能模块的落地价值: 1. 订单承接与资源匹配场景 涉及功能 基础数据管理(线路 / 酒店 / 司机) 地接散客 / 团队管理(订单操作 / 资源分配) 场景描…

作者头像 李华
网站建设 2026/6/22 17:50:15

07FlyLTAS旅游行业地接社ERP系统产品技术文档

1. 系统概述 1.1 系统介绍 旅游行业地接社ERP系统是一套专业的旅行社ERP管理软件地接版,专为地接社设计,提供产品报价、计划管理、调度安排、导游报账、审核对账、财务管理、汇总统计与决策分析等功能模块,是地接社日常运营的必备工具。 1…

作者头像 李华
网站建设 2026/6/21 18:49:48

07FlyLTAS 地接社 ERP 系统功能说明文档

一、系统概述 07FlyLTAS 是专为旅游地接行业打造的开源 ERP 系统,覆盖地接社产品开发、销售、计调、财务、统计分析全业务链,实现各角色分工协作、流程自动化与数据精细化管理,提升运营效率、降低成本风险。二、核心功能模块 (一…

作者头像 李华
网站建设 2026/6/23 8:22:19

3天掌握Postman便携版:零基础到API测试高手的完整指南

3天掌握Postman便携版:零基础到API测试高手的完整指南 【免费下载链接】postman-portable 🚀 Postman portable for Windows 项目地址: https://gitcode.com/gh_mirrors/po/postman-portable 还在为API测试工具的安装配置烦恼吗?Postm…

作者头像 李华
网站建设 2026/6/22 18:44:59

Python GDSII设计实战:从零开始构建半导体版图 [特殊字符]

Python GDSII设计实战:从零开始构建半导体版图 🚀 【免费下载链接】gdspy Python module for creating GDSII stream files, usually CAD layouts. 项目地址: https://gitcode.com/gh_mirrors/gd/gdspy 在半导体设计和光电子器件开发领域&#xf…

作者头像 李华
网站建设 2026/6/22 16:37:53

虚拟显示器终极指南:零成本扩展桌面空间的完整教程

虚拟显示器终极指南:零成本扩展桌面空间的完整教程 【免费下载链接】Virtual-Display-Driver Add virtual monitors to your windows 10/11 device! Works with VR, OBS, Sunshine, and/or any desktop sharing software. 项目地址: https://gitcode.com/gh_mirro…

作者头像 李华