Ansible Module - SCHEDULED TASKS

codingdaddy·2022년 3월 19일
0

Ansible -Study

목록 보기
10/11
post-thumbnail

Ansible-cron-doc

add a cron job Clear Lastlog on node00 to empty the /var/log/lastlog logs file. The job must run at 12am everyday.

- hosts: node00
  tasks:
  - cron:
      job: echo "" > /var/log/lastlog
      month: "*"
      day: "*"
      hour: "0"
      minute: "0"

execute this script after every 2 hour (i.e 12am, 2am, 4am etc), the command to execute the script is sh /root/free.sh and schedule should be 0 /2 * *

- hosts: node00
  tasks:
  - cron:
      job: sh /root/free.sh
      month: "*"
      day: "*"
      hour: "*/2"
      minute: "*"

remove cron job from node00

- name: remove cron job from node00
  hosts: node00
  tasks:
  - name: Remove cron job
    cron:
      name: "Check Memory" ; cron job name
      state: absent

Add a cron named cleanup on node00 that will execute after every reboot and will clean /tmp location.

- hosts: node00
  tasks:
   - cron:
      name: cleanup
      job: rm -rf /tmp/*
      special_time: reboot

special_time:

  • annually
  • daily
  • hourly
  • monthly
  • reboot
  • weekly
  • yearly

On node00 we want to keep the installed packages up to date, so we would like to run yum updates regularly. Create a playbook ~/playbooks/yum_update.yml and create a cron job as described below:

a. Do not add cron directly using crontab instead create a cron file /etc/cron.d/ansible_yum.

b. The cron must run on every Sunday at 8:05 am.

c. The name of the cron must be yum update.

d. Cron should be added for user root

- name: Create cron for yum
  hosts: node00
  gather_facts: no
  tasks:
    - name: Creates a cron
      cron:
        name: yum update
        weekday: 0
        minute: 5
        hour: 8
        user: root
        job: "yum -y update"
        cron_file: ansible_yum
  • user: The specific user whose crontab should be modified.

  • cron_file: 상대 경로인 경우 /etc/cron.d에 대해 해석됩니다.

profile
Creative - DevOps in Korea

0개의 댓글