saltstack学习笔记之crond管理

        关于crontab,都知道是linux运维必不可少的操作。cron的有多种配置方式,比如,crontab -e或者写到/etc/cron.*(hourly,daily等等).那么通过saltsatck如何管理minion的crontab呢。

           参考链接:https://docs.saltstack.com/en/latest/ref/states/all/salt.states.cron.html

           

anagement of cron, the Unix command scheduler

Cron declarations require a number of parameters. The following are theparameters used by Salt to define the various timing values for a cron job:

  • minute
  • hour
  • daymonth
  • month
  • dayweek (0 to 6 are Sunday through Saturday, 7 can also be used forSunday)

        分,时,日 ,月,周(0-6为周日到周六,7也可以用于周日)

注意:上述所有单位都是默认*,默认用户为root

        saltstack管理cron有两种方式,一种是模块,一种是sls文件

        首先来讲模块,saltstack自带模块cron:

1,查看crontab

    salt '*' cron.list_tab user

    salt '*' cron.ls user

   两种用法相同,saltstack学习笔记之crond管理

  2,添加crontab

salt.modules.cron.set_job(user, minute, hour, daymonth, month, dayweek, cmd, commented=False,comment=None, identifier=None)

saltstack学习笔记之crond管理

查看是否添加

saltstack学习笔记之crond管理

3,删除crontab

salt.modules.cron.rm_job(user, cmd, minute=None, hour=None, daymonth=None, month=None, day-week=None, identifier=None)

saltstack学习笔记之crond管理

第二种方法,sls文件

[[email protected] init]# cat cron.sls
date > /tmp/crontest:         #ID
  cron.present:                     #下发
     - name: /etc/sysconfig/x.sh   #内容(cmd)
     - user: root                            #用户

     - hour: '*/1'                            #执行时间

可以通过两种方式查看,一种cron.ls,一种是在Minion上crontab -l

#crontab -l
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/etc/sysconfig/modules/CloudWAN.modules
* */1 * * * /etc/sysconfig/x.sh

更新cron

[[email protected] init]# cat cron.sls
date > /tmp/crontest:
  cron.present:
     - name: /etc/sysconfig/x.sh
     - user: root
     - hour: '*/2'

执行

当name不变的时候,会直接更新时间;当name变更的时候,会新建crontab

name在未指定 identifier:时,会作为identifier使用,如果相同,则更新,如果不同,则新建

确认对指定的user移除指定的cron job; 只有name匹配才会移除cron job.

  • name:

    要移除user crontab的命令

  • user:

    需要修改(移除)crontab的user,默认是root

  • identifier:

    跟踪cron job的用户自定义identifier,默认是state的id

还有一种类似于rc.local的用法,那就是absent

/home/pop/x.sh:
  cron.absent:
    - user: root
    - special: '@hourly'

当重启时自动执行,

Added the opportunity to set a job with a special keyword like '@reboot' or'@hourly'. Quotes must be used, otherwise PyYAML will strip the '@' sign.

/path/to/cron/script:
  cron.present:
    - user: root
    - special: '@hourly'

The script will be executed every reboot if cron daemon support this option.

/path/to/cron/otherscript:
  cron.absent:
    - user: root
    - special: '@daily'
PS:  Warnings: 'special' is an invalid keyword argument for 'cron.present'. If
              you were trying to pass additional data to be used in a template
              context, please populate 'context' with 'key: value' pairs. Your
              approach will work until Salt Carbon is out. Please update your
              state files.

2015版本执行present出报错,当然具体的用法并没有太深究,用的不多。


上面的管理方式使用的并不多,更多的是通过

cron.sls

file.managed:

   - name: /etc/cron.houry

   - source: salt://init/files/xxx.sh

   - mode: 755

   - user: root

或者是

[[email protected] init]# cat file_bak.sls
file_rsync:   
   file.recurse:
     - source: salt://init/files/pop/
     - name: /home/pop/
     - user: root
     - group: root
     - makedirs: True
     - dir_mode: 755
     - backup: minion
     - include_enpty: True

同步文件来完成。

PS:recurse是同步目录,managed是同步文件