Linux使用ansible实现批量创建cron计划任务

Posted 白-胖-子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux使用ansible实现批量创建cron计划任务相关的知识,希望对你有一定的参考价值。

利用ansible的cron模块实现

创建计划任务

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 crom

> CRON    (/usr/lib/python3.6/site-packages/ansible/modules/system/cron.py)

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

以上是关于Linux使用ansible实现批量创建cron计划任务的主要内容,如果未能解决你的问题,请参考以下文章

LINUX——关于ansible批量控制,批量命令及部署的使用

Linux命令集锦:ansible命令

zabbix批量部署Windows和Linux的agent

Linux-ansible

Linux Ansible详解

Linux中ansible自动化运维工具