ansible常用模块之script模块和cron模块(十)
1.script模块
语法格式
ansible backup -m script -a "脚本"
直接写脚本路径即可
案例:安装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
运行脚本
[root@ansible ~]# ansible backup -m script -a "/server/scripts/rsync.sh"
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:在每天的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'"
案例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'"
案例3:删除rsyncbackup2计划任务
[root@ansible ~]# ansible all -m cron -a "name=rsyncbackup2 state=absent"
目录 返回
首页