1. 程式人生 > >saltstack學習筆記之crond管理

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

   兩種用法相同,

  2,新增crontab

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

檢視是否新增

3,刪除crontab

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

第二種方法,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是同步檔案