ansible 之 cron

cron

用途:管理 crontab 的模块。可以创建、更新、删除定时任务

官方文档

[root@ansible ~]# ansible-doc  cron
> CRON    (/usr/lib/python2.7/site-packages/ansible-2.8.0.dev0-py2.7.egg/ansible/modules/system/cron.py)

        Use this module to manage crontab and environment variables entries. This module
        allows you to create environment variables and named crontab entries, update, or
        delete them. When crontab jobs are managed: the module includes one line with
        the description of the crontab entry `"#Ansible: <name>"' corresponding to the
        "name" passed to the module, which is used by future ansible/module calls to
        find/check the state. The "name" parameter should be unique, and changing the
        "name" value will result in a new cron task being created (or a different one
        being removed). When environment variables are managed: no comment line is
        added, but, when the module needs to find/check the state, it uses the "name"
        parameter to find the environment variable definition line. When using symbols
        such as %, they must be properly escaped.

  * This module is maintained by The Ansible Community
OPTIONS (= is mandatory):

- backup
        If set, create a backup of the crontab before it is modified. The location of
        the backup is returned in the `backup_file' variable by this module.
        [Default: no]
        type: bool

- cron_file
        If specified, uses this file instead of an individual user's crontab. If this is
        a relative path, it is interpreted with respect to /etc/cron.d. (If it is
        absolute, it will typically be /etc/crontab). Many linux distros expect (and
        some require) the filename portion to consist solely of upper- and lower-case
        letters, digits, underscores, and hyphens. To use the `cron_file' parameter you
        must specify the `user' as well.
        [Default: (null)]

- day
        Day of the month the job should run ( 1-31, *, */2, etc )
        (Aliases: dom)[Default: *]

- disabled
        If the job should be disabled (commented out) in the crontab.
        Only has effect if `state=present'.
        [Default: no]
        type: bool
        version_added: 2.0

- env
        If set, manages a crontab's environment variable. New variables are added on top
        of crontab. "name" and "value" parameters are the name and the value of
        environment variable.
        [Default: no]
        type: bool
        version_added: 2.1

- hour
        Hour when the job should run ( 0-23, *, */2, etc )
        [Default: *]

- insertafter
        Used with `state=present' and `env'. If specified, the environment variable will
        be inserted after the declaration of specified environment variable.
        [Default: (null)]
        version_added: 2.1

- insertbefore
        Used with `state=present' and `env'. If specified, the environment variable will
        be inserted before the declaration of specified environment variable.
        [Default: (null)]
        version_added: 2.1

- job
        The command to execute or, if env is set, the value of environment variable. The
        command should not contain line breaks. Required if state=present.
        (Aliases: value)[Default: (null)]

- minute
        Minute when the job should run ( 0-59, *, */2, etc )
        [Default: *]

- month
        Month of the year the job should run ( 1-12, *, */2, etc )
        [Default: *]

- name
        Description of a crontab entry or, if env is set, the name of environment
        variable. Required if state=absent. Note that if name is not set and
        state=present, then a new crontab entry will always be created, regardless of
        existing ones.
        [Default: (null)]

- reboot
        If the job should be run at reboot. This option is deprecated. Users should use
        special_time.
        [Default: no]
        type: bool
        version_added: 1.0

- special_time
        Special time specification nickname.
        (Choices: reboot, yearly, annually, monthly, weekly, daily, hourly)[Default:
        (null)]
        version_added: 1.3

- state
        Whether to ensure the job or environment variable is present or absent.
        (Choices: absent, present)[Default: present]

- user
        The specific user whose crontab should be modified.
        [Default: root]

- weekday
        Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
        (Aliases: dow)[Default: *]


REQUIREMENTS:  cron

AUTHOR: Dane Summers (@dsummersl), Mike Grozak (@rhaido), Patrick Callahan (@dirtyharrycallahan), Evan Kaufman 
        METADATA:
          status:
          - preview
          supported_by: community
        

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" fro
  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

参数解析

  • backup
    默认值:no
    类型:bool
    如果设置为 yes,则在修改 crontab文件之前先备份。

  • cron_file
    默认值: null
    如果被指定,则使用此指定的文件代替用户私有的 crontab。如果文件是相对路径,则默认存放于 /etc/cron.d 目录下,如果是绝对路径,则会取代 /etc/crontab

  • day
    默认值:*
    一个月中的第几天,例如:(1-31, *, */2等)

  • disabled
    默认值:no
    类型:bool
    crontab 中取消已有的 job,如果 state=present,则会立即生效

  • env
    默认值:no
    类型:bool
    如果被设置,则可以管理 crontab 中的环境变量。新的环境变量被添加在 crontab 顶部,namevalue参数是环境变量中的 namevalue

  • hour
    默认:*
    job运行的小时时间(0-23, *, */2等)

  • insertafter
    默认值:null

  • insertbefore
    默认值:null

  • job
    别名:value
    默认值:null

  • minute
    默认值:*
    分钟( 0-59, *, */2等)

  • month
    默认值:*
    月份( 1-12, *, */2等)

  • name

    crodtab 的描述,如果 state=present,则不管有没有存在相同 namecrontab,将会重新创建一个。如果state=present,则会删除 crontab 中对应namecrondtab

  • reboot
    默认值:no
    类型:bool
    这个参数已被弃用

  • special_time
    默认值:null
    可供选择项: rebootyearlyannuallymonthlyweeklydailyhourly
    定义一个时间昵称

  • state
    默认值:present
    选项:absentpresent
    确认 job 或环境变量立即生效或删除

  • user
    默认值:root
    修改 crontab 时的用户

  • weekday
    默认值:*
    周时间:0-6 for Sunday-Saturday等

posted @ 2018-12-06 12:03  McSiberiaWolf  阅读(352)  评论(0编辑  收藏  举报