LoginSignup
15
7

More than 5 years have passed since last update.

Ansibleでcronを設定する

Last updated at Posted at 2018-11-02

Ansibleでcronを設定する

Ansibleで、Amazon S3にディレクトリのバックアップを午前3時に実行するcronを設定する。
cron-s3.shには、AWS CLIでS3とsyncするコマンドが記述されている。

main.yml
- name: S3バックアップのシェルを配置
  become: True
  template:
    src: templates/cron-s3.sh.j2
    dest: /root/cron-s3.sh

- name: cron設定
  become: True
  cron:
    name: S3バックアップ
    minute: 0
    hour: 3
    job: "sh /root/cron-s3.sh 1>> /var/log/nas-log/exec.log 2>> /var/log/nas-log/error.log"
    state: present

cronモジュールを使って、crontabの設定を編集する。
minute・hour・day・month・weekdayは、指定がなければデフォルトで*になる。

結果を確認

結果
$ sudo crontab -l
#Ansible: S3バックアップ
0 3 * * * sh /root/cron-s3.sh 1>> /var/log/nas-log/exec.log 2>> /var/log/nas-log/error.log

nameで書いたものが、#Ansible:<name>で設定される。

cron - Manage cron.d and crontab entries — Ansible Documentation

15
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
7