ansible cron 模块就是计划任务,很简单
backup:操作之前先备份目标主机上的原计划任务
cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
day: 指定日(1-31,*,*/1,……)
hour:指定小时(0-23,*,*/2,……)
minute:指定分钟(0-59,*,*/3,……)
month:指定月(1-12,*,*/4,……)
weekday:指定周(0-7,*,……)
job:要执行的任务,依赖于state=present
name:该任务的描述
special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
state:确认该任务计划是创建还是删除
user:以哪个用户的身份执行
1.每分钟说一句话到/tmp下
# ansible dbserver -m cron -a "name=‘echo ckl to tmp‘ minute=*/1 job=‘echo \"ckl is good man\" >> /tmp/kk.txt‘ user=root" 172.16.110.49 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "echo ckl to tmp" ] }
# crontab -l #Ansible: echo ckl to tmp */1 * * * * echo "ckl is good man" >> /tmp/kk.txt
# cat kk.txt ckl is good man
2.将计划任务写到目标主机的文件中
# ansible dbserver -m cron -a "name=‘mei diao yong‘ cron_file=/root/ckl_cron.txt user=root job=‘minute=*/3 /bin/bash /opt/script/df.sh‘" 172.16.110.49 | SUCCESS => { "changed": true, "cron_file": "/root/ckl_cron.txt", "envs": [], "jobs": [ "mei diao yong" ] }
# cat /root/ckl_cron.txt #Ansible: mei diao yong * * * * * root minute=*/3 /bin/bash /opt/script/df.sh
3.备份原有的任务计划,创建新的之前
# ansible dbserver -m cron -a "name=‘test back‘ backup=yes user=root minute=*/2 job=‘/bin/ls /root‘" 172.16.110.49 | SUCCESS => { "backup_file": "/tmp/crontabU1wKmy", "changed": true, "envs": [], "jobs": [ "echo ckl to tmp", "test back" ] }
# crontab -l #Ansible: echo ckl to tmp */1 * * * * echo "ckl is good man" >> /tmp/kk.txt #Ansible: test back */2 * * * * /bin/ls /root
查看备份:
# cat /tmp/crontabU1wKmy #Ansible: echo ckl to tmp */1 * * * * echo "ckl is good man" >> /tmp/kk.txt You have new mail in /var/spool/mail/root
时间: 2024-10-26 16:56:15