利用ansible cron进行服务器定时日志清理

方案:创建定时清理日志脚本,通过find命令删除7天以外的日志文件,然后通过ansible分发到各个服务器进行定时执行

vim /data/script/clean_log.sh

function cleanLog(){
        path=$1
        name=$2
        if [ -d $path ];then
                echo "清理$path路径下的$name日志"
                find $path -mtime +7 -name "$name" -delete
        else
                echo "$path不存在"
        fi
 
}
 
#根据实际情况进行更新
cleanLog "/data/var/log/hadoop/hdfs" "*"
cleanLog "/var/log" "*.log.*"
cleanLog "/var/log" "*.out.*"

添加执行权限:
chmod +x /data/script/clean_log.sh

通过ansible将脚本分发到各个机器:
ansible -m copy -a “src=/data/script/clean_log.sh dest=/data/script/clean_log.sh”

检查是否分发成功:
ansible -m shell -a “ls /data/script/clean_log.sh”

配置crontab,在每天凌晨1点执行定时任务:
ansible -m cron -a ‘name=“clean_log” minute=0 hour=1 job=“sh /data/script/clean_log.sh >/dev/null 2&1”’
然后抽查两台机器,通过执行crontab -l命令检查定时任务是否配置成功

你可能感兴趣的:(备忘,日志清理,ansible)