批量執行crontab指定條目的註釋和解註釋

前言:在平常變動過程當中,變動前會註釋某些定時任務(好比巡檢告警等),變動完成後需恢復,有時變動操做的服務器不少,對應須要註釋的crontab也不少且不相同,本文經過分發平臺執行對應腳本批量實現crontab的註釋和解註釋功能。git

環境說明:github

主機名 操做系統版本 ip 用戶名 備註
ansible Centos 7.6.1810 172.27.34.51 user_test crontab測試服務器01
ansible-awx Centos 7.6.1810 172.27.34.50 user_test crontab測試服務器02

1、crontab測試環境準備

1.主機ansible環境準備

[user_test@ansible ~]$ echo $HOME
/home/user_test
[user_test@ansible ~]$ crontab -l
0 0 * * * /home/user_test/bin/date > /dev/null
0 0 * * * $HOME/bin/date > /dev/null
0 0 * * * date > /dev/null
* * * * * df -h > /tmp/df.txt
0 0 * * * $HOME/bin/pwd_test > /dev/null
[user_test@ansible ~]$ pwd
/home/user_test
[user_test@ansible ~]$ ll
總用量 8
drwxrwxr-x 2 user_test user_test   6 9月   8 11:22 bin
-rwxrw-r-- 1 user_test user_test 309 9月   8 11:01 crontab2.sh
-rwxrw-r-- 1 user_test user_test 303 9月   8 10:59 crontab.sh
[user_test@ansible ~]$ cd bin
[user_test@ansible bin]$ ll
總用量 0
[user_test@ansible bin]$ ln -s /usr/bin/date date
[user_test@ansible bin]$ ln -s /usr/bin/pwd pwd_test
[user_test@ansible bin]$ ll
總用量 0
lrwxrwxrwx 1 user_test user_test 13 9月   8 11:23 date -> /usr/bin/date
lrwxrwxrwx 1 user_test user_test 12 9月   8 11:23 pwd_test -> /usr/bin/pwd
[user_test@ansible bin]$ /home/user_test/bin/date 
2020年 09月 08日 星期二 11:23:25 CST
[user_test@ansible bin]$ /home/user_test/bin/pwd_test 
/home/user_test/bin

image-20200908112545814

2.主機ansible-awx環境準備

[user_test@ansible-awx ~]$ echo $HOME
/home/user_test
[user_test@ansible-awx ~]$ crontab -l
0 0 * * * /home/user_test/bin/date > /dev/null
0 0 * * * $HOME/bin/date > /dev/null
0 0 * * * date > /dev/null
0 0 * * * $HOME/bin/df -h > /tmp/df.txt
[user_test@ansible-awx ~]$ pwd   
/home/user_test
[user_test@ansible-awx ~]$ ll
總用量 0
drwxrwxr-x 2 user_test user_test 6 9月   8 11:25 bin
[user_test@ansible-awx ~]$ cd bin
[user_test@ansible-awx bin]$ ll
總用量 0
[user_test@ansible-awx bin]$ ln -s /usr/bin/date date
[user_test@ansible-awx bin]$ ln -s /usr/bin/df df
[user_test@ansible-awx bin]$ ll
總用量 0
lrwxrwxrwx 1 user_test user_test 13 9月   8 11:25 date -> /usr/bin/date
lrwxrwxrwx 1 user_test user_test 11 9月   8 11:25 df -> /usr/bin/df
[user_test@ansible-awx bin]$ /home/user_test/bin/date 
2020年 09月 08日 星期二 11:25:58 CST
[user_test@ansible-awx bin]$ /home/user_test/bin/df -h
文件系統                   容量  已用  可用 已用% 掛載點
/dev/mapper/root--vg-root   10G  229M  9.8G    3% /
devtmpfs                   1.9G     0  1.9G    0% /dev
tmpfs                      1.9G     0  1.9G    0% /dev/shm
tmpfs                      1.9G  201M  1.7G   11% /run
tmpfs                      1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mapper/root--vg-usr    10G  1.7G  8.4G   17% /usr
/dev/mapper/root--vg-home   10G   50M   10G    1% /home
/dev/mapper/root--vg-var    10G  3.0G  7.1G   30% /var
/dev/mapper/root--vg-tmp    10G   33M   10G    1% /tmp
/dev/mapper/root--vg-opt    10G  233M  9.8G    3% /opt
/dev/sda1                  497M  138M  359M   28% /boot
tmpfs                      379M     0  379M    0% /run/user/0

image-20200908112802951

分別在兩臺主機上構造定時任務,其中$HOME/bin下的命令都爲/usr/bin/下系統命令的軟連接,/home/user_test/bin/pwd_test等命令測試正常。bash

本文目標:服務器

註釋ansible的「0 0 * * * $HOME/bin/date > /dev/null」、「0 0 * * * $HOME/bin/pwd_test > /dev/null」app

註釋ansible-awx的「0 0 * * * $HOME/bin/df -h > /tmp/df.txt」運維

2、執行腳本

1.註釋腳本crontab.sh

[user_test@ansible ~]$ more crontab.sh 
#!/bin/bash
host=`hostname`
echo $host

if [ $host = ansible ]
then
sed -i.bak '/$HOME\/bin\/date/s/^/#/' /var/spool/cron/user_test
sed -i.bak '/$HOME\/bin\/pwd_test/s/^/#/' /var/spool/cron/user_test
fi

if [ $host =  ansible-awx ]
then
sed -i.bak '/$HOME\/bin\/df/s/^/#/' /var/spool/cron/user_test
fi

image-20200908113213592

2.解註釋腳本crontab2.sh

[user_test@ansible ~]$ more crontab2.sh 
#!/bin/bash
host=`hostname`
echo $host

if [ $host = ansible ]
then
sed -i '/^#.*$HOME\/bin\/date/s/^#//g'  /var/spool/cron/user_test
sed -i '/^#.*$HOME\/bin\/pwd_test/s/^#//g'  /var/spool/cron/user_test
fi

if [ $host =  ansible-awx ]
then
sed -i '/^#.*$HOME\/bin\/df/s/^#//g'  /var/spool/cron/user_test
fi

image-20200908113505890

兩個腳本判斷邏輯:首先獲取主機名,而後匹配主機名,根據主機名來註釋或解註釋指定的定時任務,指定的定時任務經過sed工具匹配獲取。ide

3、測試執行

1.spug平臺模板配置

使用自動化運維平臺spug(後面文章會介紹)進行測試。工具

image-20200908144548685

模板管理中新建兩個模板'註釋crontab'和'解註釋crontab',這兩個模板其實分別對應腳本crontab.sh和crontab2.sh。測試

若是沒有spug平臺,也可使用ansible平臺進行分發執行。操作系統

2.註釋crontab

選擇主機ansible和ansible-awx

image-20200908145029479

選擇模板'註釋crontab'

image-20200908145137372

image-20200908145156385

選擇模板便是選擇對應執行的腳本,'開始執行'

image-20200908145257040
驗證:

image-20200908145358556

image-20200908145416355

發現主機ansible和ansible-awx都完成對應crontab的註釋。

3.解註釋crontab

選擇模板'解註釋crontab'

image-20200908145550233

執行:

image-20200908145604617

image-20200908145633256.png

驗證:

image-20200908145658096.png

image-20200908145708701

兩臺主機的crontab都已經解註釋,註釋和解註釋測試都符合預期。
 
 
腳本已上傳github:comment-and-uncomment-crontab

相關文章
相關標籤/搜索