Linux使用ansible实现批量创建cron计划任务
利用ansible的cron模块可以实现批量部署计划任务
关于ansible的内容请参见:Linux自动化运维ansible–简介和基本
https://blog.csdn.net/timonium/article/details/117170986
创建计划任务
ansible 10.0.0.108 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backupMysql" job=/root/mysql_backup.sh'
ansible sunserver -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime"
启用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 10.0.0.88 &>/dev/null' name='Synctime' disabled=no"
禁用计划任务
ansible sunserver -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 10.0.0.88 &>/dev/null' name='Synctime' disabled=yes"
删除任务
ansible websrvs -m cron -a "name='backupMysql' state=absent"
ansible websrvs -m cron -a "name='Synctime' state=absent"
cron模块帮助中的示例
ansible-doc cron
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
cron:
name: "check dirs"
minute: "0"
hour: "5,2"
job: "ls -alh > /dev/null"
- name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: an old job" f>
cron:
name: "an old job"
state: absent
- name: Creates an entry like "@reboot /some/job.sh"
cron:
name: "a job for reboot"
special_time: reboot
job: "/some/job.sh"
- name: Creates an entry like "PATH=/opt/bin" on top of crontab
cron:
name: PATH
env: yes
job: /opt/bin
- name: Creates an entry like "APP_HOME=/srv/app" and insert it after PATH declaration
cron:
name: APP_HOME
env: yes
job: /srv/app
insertafter: PATH
- name: Creates a cron file under /etc/cron.d
cron:
name: yum autoupdate
weekday: "2"
minute: "0"
hour: "12"
user: root
job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
cron_file: ansible_yum-autoupdate
- name: Removes a cron file from under /etc/cron.d
cron:
name: "yum autoupdate"
cron_file: ansible_yum-autoupdate
state: absent
- name: Removes "APP_HOME" environment variable from crontab
cron:
name: APP_HOME
env: yes
state: absent
多个服务器实现时间同步,使用ansible cron模块对主机记性周期性时间同步
ansible命令执行
# 所有记录主机安装ntp
ansible app -m shell -a "yum install ntp -y"
# 所有记录主机添加定时任务
ansible all-m cron -a "name=ntpdate job='ntpdate time1.aliyun.com' minute=0 hour=*/1"
ansible-playboot脚本
---
- hosts: all
gather_facts: false
remote_user: root
tasks:
- name: name=ntpdate date
cron: name="ntpdate" job="ntpdate time1.aliyun.com" minute-0 hour=*/1
文章目录
关闭