ansible常用模块之script模块和cron模块(十)
【摘要】
1.script模块
语法格式
ansible backup -m script -a "脚本"
直接写脚本路径即可
12
案例:安装rsync
1.写脚本
[root@ansible ~]# ...
1.script模块
语法格式
ansible backup -m script -a "脚本"
直接写脚本路径即可
- 1
- 2
案例:安装rsync
1.写脚本
[root@ansible ~]# vim /server/scripts/rsync.sh
#!/bin/bash
#批量部署rsync
yum -y install rsync &>/dev/null
echo -e "\e[033m1.rsync install... \e[0m"
cat > /etc/rsyncd.conf <<EOF
uid = rsync
gid = rsync
port 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync_passwd
log file = /var/log/rsync/rsync.log
[backup]
comment = backup
path = /backup
EOF
echo -e "\e[033m2.rsync config finish... \e[0m"
id rsync &> /dev/null
if [ $? -ne 0 ];then
useradd -M -s /sbin/nolog rsync
fi
if [ ! -e /backup ];then
mkdir /backup
fi
chown -R rsync:rsync /backup
echo -e "\e[033m3.backup dir finish... \e[0m"
echo "rsync_backup:123456" > /etc/rsync_passwd
chmod 600 /etc/rsync_passwd
echo -e "\e[033m4.rsync auth finish... \e[0m"
systemctl restart rsyncd
systemctl enable rsyncd &>/dev/null
lsof -i:873 &>/dev/null
if [ $? -eq 0 ];then
echo -e "\e[033m5.rsync finish.....\e[0m"
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
运行脚本
[root@ansible ~]# ansible backup -m script -a "/server/scripts/rsync.sh"
- 1
2.cron模块
语法格式
ansible all -m cron -a "name=任务名 minute=分 hour=时 day=日 month=月 weekday=星期 job=命令"
参数
name //描述,必须要写,如果不写,默认为None。会导致无法指定删除某条计划任务
job //执行的命令
state //执行状态
absent //删除
present //创建(默认)
minute //分钟(0-59,*,*/2,默认是*)
hour //小时(1-23,*,*/2,默认是*)
day //日(1-31,*,*/2.默认是*)
month //月(1-12,*,*/2,默认是*)
weekday //星期(0-6,*,*/2,默认是*)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
案例1:在每天的21:30执行rsync备份脚本
1.准备脚本
[root@ansible ~]# ansible all -m file -a "path=/script state=directory"
2.将备份脚本上传至各主机
[root@ansible ~]# ansible all -m copy -a "src=/script/rsyncd_backup.sh dest=/script"
3.创建计划任务
[root@ansible ~]# ansible all -m cron -a "name='rsync backup' minute=30 hour=21 job='/bin/bash /script/rsyncd_backup.sh'"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
案例2:在5月份的周一至周五每2小时的30分时执行rsync备份脚本
[root@ansible ~]# ansible all -m cron -a "name=rsyncbackup2 minute=30 hour=*/2 month=5 weekday=1,5 job='/bin/sh /script/rsync_backup2.sh'"
- 1
- 2
案例3:删除rsyncbackup2计划任务
[root@ansible ~]# ansible all -m cron -a "name=rsyncbackup2 state=absent"
- 1
文章来源: jiangxl.blog.csdn.net,作者:Jiangxl~,版权归原作者所有,如需转载,请联系作者。
原文链接:jiangxl.blog.csdn.net/article/details/110489945
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)