Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

批量执行注释和解注释指定crontab

NotificationsYou must be signed in to change notification settings

loong576/comment-and-uncomment-crontab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

前言:在日常变更过程中,变更前会注释某些定时任务(比如巡检告警等),变更完成后需恢复,又是变更操作的服务器很多,对应需要注释的crontab也很多且不相同,本文通过分发平台执行对应脚本批量实现crontab的注释和解注释功能。

环境说明:

主机名操作系统版本ip用户名备注
ansibleCentos 7.6.1810172.27.34.51crontab测试服务器01
ansible-awxCentos 7.6.1810172.27.34.50crontab测试服务器02

1.主机ansible环境准备

[user_test@ansible~]$echo$HOME/home/user_test[user_test@ansible~]$ crontab -l0 0*** /home/user_test/bin/date> /dev/null0 0***$HOME/bin/date> /dev/null0 0*** date> /dev/null***** df -h> /tmp/df.txt0 0***$HOME/bin/pwd_test> /dev/null[user_test@ansible~]$pwd/home/user_test[user_test@ansible~]$ ll总用量 8drwxrwxr-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总用量 0lrwxrwxrwx 1 user_test user_test 13 9月   8 11:23 date -> /usr/bin/datelrwxrwxrwx 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 -l0 0*** /home/user_test/bin/date> /dev/null0 0***$HOME/bin/date> /dev/null0 0*** date> /dev/null0 0***$HOME/bin/df -h> /tmp/df.txt[user_test@ansible-awx~]$pwd   /home/user_test[user_test@ansible-awx~]$ ll总用量 0drwxrwxr-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总用量 0lrwxrwxrwx 1 user_test user_test 13 9月   8 11:25 date -> /usr/bin/datelrwxrwxrwx 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% /devtmpfs                      1.9G     0  1.9G    0% /dev/shmtmpfs                      1.9G  201M  1.7G   11% /runtmpfs                      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% /boottmpfs                      379M     0  379M    0% /run/user/0

image-20200908112802951

分别在两台主机上构造定时任务,其中$HOME/bin下的命令都为/usr/bin/下系统命令的软链接,/home/user_test/bin/pwd_test等命令测试正常。

本文目标:

注释ansible的“0 0 * * * $HOME/bin/date > /dev/null”、“0 0 * * * $HOME/bin/pwd_test > /dev/null”

注释ansible-awx的“0 0 * * * $HOME/bin/df -h > /tmp/df.txt”

二、执行脚本

1.注释脚本crontab.sh

[user_test@ansible~]$ more crontab.sh#!/bin/bashhost=`hostname`echo$hostif [$host= ansible ]thensed -i.bak'/$HOME\/bin\/date/s/^/#/' /var/spool/cron/user_testsed -i.bak'/$HOME\/bin\/pwd_test/s/^/#/' /var/spool/cron/user_testfiif [$host=  ansible-awx ]thensed -i.bak'/$HOME\/bin\/df/s/^/#/' /var/spool/cron/user_testfi

image-20200908113213592

2.解注释脚本crontab2.sh

[user_test@ansible~]$ more crontab2.sh#!/bin/bashhost=`hostname`echo$hostif [$host= ansible ]thensed -i'/^#.*$HOME\/bin\/date/s/^#//g'  /var/spool/cron/user_testsed -i'/^#.*$HOME\/bin\/pwd_test/s/^#//g'  /var/spool/cron/user_testfiif [$host=  ansible-awx ]thensed -i'/^#.*$HOME\/bin\/df/s/^#//g'  /var/spool/cron/user_testfi

image-20200908113505890

两个脚本判断逻辑:首先获取主机名,然后匹配主机名,根据主机名来注释或解注释指定的定时任务,指定的定时任务通过sed工具匹配获取。

三、测试执行

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都已经解注释,注释和解注释测试都符合预期。

About

批量执行注释和解注释指定crontab

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp