[Ansible] Ansible을 사용하여 Cron 작업 등록하기

Mr.Song·2023년 5월 22일
0

IaC

목록 보기
3/3
post-thumbnail
Ansible을 통해 Cron 모듈을 통해 배치성 작업을 등록하려한다. 하지만 고려해야할 포인트가 몇가지 있다.
  1. 기존 서버에 Cron 패키지가 설치 되어있는가?
  2. 하나의 매니페스트로 일반적으로 사용하는 OS의 Job케이스를 지원할 수 있는가?
기존 서버에 패키지가 설치되어 있지않은 경우가 있을 수 있다. 어차피 Cron 작업을 등록해야하는거라면, 설치작업부터 진행한다.
두 번째 패키지 설치 시 OS에 따라 패키지 명이 다를 수 있다. 모든 Linux에서 동일한 Cron 패키지를 지원하지않는다. 예를 들어, Ubuntu나 Centos7.x, Redhat7,x 때의 버전들은 지원하지만 이후 최신버전이나 AMZN2,3, Fedofa의 경우 Cronie라는 패키지를 설치해야한다.
위 조건을 충족시키기 위해 node의 Facts를 수집하여 Cron 혹은 Cronie를 설치하도록 작업을 등록하고, 이후 이 매니페스트로 다른 작업을 등록하기 위해 작업정보는 변수처리하여 명령어로 인수로 받아 처리할 수 있도록 했다.

TL;DR

Ansible Cron모듈을 통해 Nginx Reload 작업을 등록한다.

1. 플레이북 매니페스트 작성하기

--- CreateCronJobs.yaml
- name: Create Cron Jobs
  hosts:
    - localhost
  become: true
  vars:
    cron_job_minute: '0'
    cron_job_hour: '3'
    cron_job_day: '*'
    cron_job_month: '*'
    cron_job_weekday: '*'
    cron_job_command: '/usr/sbin/nginx -s reload'
  tasks:
    - name: Determine package manager based on OS
      set_fact:
        cron_package: "{{ 'cron' if ansible_distribution == 'Ubuntu' else 'cronie' if ansible_distribution in ['CentOS', 'RedHat', 'Amazon', 'Fedora']  }}"

    - name: Install cron package
      package:
        name: "{{ cron_package }}"
        state: present
      when: cron_package != ""

    - name: Add Nginx Reload Job
      cron:
        name: Reload nginx
        job: "{{ cron_job_command }}"
        state: present
        minute: "{{ cron_job_minute }}"
        hour: "{{ cron_job_hour }}"
        day: "{{ cron_job_day }}"
        month: "{{ cron_job_month }}"
        weekday: "{{ cron_job_weekday }}"

    - name: Get Crontab List
      command: 'crontab -l'
      register: crontab_list

    - name: Print Crontab List
      debug:
        var: crontab_list.stdout_lines

2. 플레이북 실행 및 디버깅 확인

[ansible@localhost playbook]$ ansible-playbook CreateCronJobs.yaml
PLAY [Create Cron Jobs] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]


TASK [Determine package manager based on OS] *****************************************************************************************************************************************************************************************************************************************
ok: [localhost]


TASK [Install cron package] **********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]


TASK [Add Nginx Reload Job] **********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]


TASK [Get Crontab List] **************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]


TASK [Print Crontab List] ************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "crontab_list.stdout_lines": [
        "# Edit this file to introduce tasks to be run by cron.",
        "# ",
        "# Each task to run has to be defined through a single line",
        "# indicating with different fields when the task will be run",
        "# and what command to run for the task",
        "# ",
        "# To define the time you can provide concrete values for",
        "# minute (m), hour (h), day of month (dom), month (mon),",
        "# and day of week (dow) or use '*' in these fields (for 'any').# ",
        "# Notice that tasks will be started based on the cron's system",
        "# daemon's notion of time and timezones.",
        "# ",
        "# Output of the crontab jobs (including errors) is sent through",
        "# email to the user the crontab file belongs to (unless redirected).",
        "# ",
        "# For example, you can run a backup of all your user accounts",
        "# at 5 a.m every week with:",
        "# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/",
        "# ",
        "# For more information see the manual pages of crontab(5) and cron(8)",
        "# ",
        "# m h  dom mon dow   command",
        "#Ansible: Reload nginx",
        "0 3 * * * /usr/sbin/nginx -s reload"
    ]
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************
localhost        : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    
profile
정리를 못하면 기록이라도 하자!!

0개의 댓글