cron으로 예약된 호스트 보고서 관리

LinuxBeginner
지금 연습하기

소개

신뢰할 수 있는 예약 실행에는 성공한 플레이 요약 이상의 증거가 필요합니다. 일반 사용자용 보호된 보고 프로그램을 배포하고 안정된 이름의 cron 항목을 관리하며 자연 실행을 관찰하고 단계를 갱신한 다음 별도 임시 항목을 제거합니다. 최종 검사는 관련 없는 기존 작업을 보존하면서 새로운 스케줄러 실행을 관찰합니다.

일반 사용자 보고서와 이름 있는 예약 배포하기

이 단계에서는 제공된 보고 프로그램을 배포하고 두 관리 호스트의 automation 사용자를 위해 예약합니다.

사전 요구 사항으로 cron이 설치되어 있고 데몬이 이미 실행 중입니다. 노드는 systemd를 실행하지 않습니다. 각 automation crontab에는 관련 없는 keep-existing-report 항목도 있으므로 보존하세요. cron 모듈은 사용자 crontab 전체를 대체하는 대신 안정된 이름의 항목을 관리합니다.

프로젝트로 이동하고 제공된 컨트롤러 프로그램을 확인합니다.

cd /home/labex/project
cat files/host-report.sh

프로그램은 보호된 관리 로그에 UTC 에포크 타임스탬프, 실제 호스트 이름, 단계 인수를 덧붙입니다. umask 027은 새 로그 파일의 권한을 0640으로 만듭니다. 프로그램을 그대로 두고 cron이 자연스럽게 실행하게 하세요. 수동 호출은 예약 실행을 증명하지 못합니다.

단계를 재사용 입력으로 정의합니다.

cat > group_vars/all.yml <<'YAML'
report_phase: initial
YAML

플레이북을 만듭니다. cron 작업에는 분, 시, 일, 월, 요일 필드가 있습니다. 각 필드의 별표는 매분 한 번 실행하게 합니다. name은 이후 업데이트에 사용할 Ansible 표식을 만들고 user automation은 해당 사용자 crontab을 선택합니다. become은 이름 있는 사용자 예약과 명시적인 파일 소유권 관리를 허용하지만 예약 프로그램을 root로 실행하게 하지는 않습니다.

cat > reporting.yml <<'YAML'
- name: Manage a scheduled ordinary-user host report
  hosts: all
  gather_facts: false
  become: true
  tasks:
    - name: Prepare the ordinary user's reporting directory
      ansible.builtin.file:
        path: /home/automation/reporting
        state: directory
        owner: automation
        group: automation
        mode: "0750"

    - name: Deploy the supplied report program
      ansible.builtin.copy:
        src: /home/labex/project/files/host-report.sh
        dest: /home/automation/reporting/host-report.sh
        owner: automation
        group: automation
        mode: "0750"

    - name: Keep the named report schedule present
      ansible.builtin.cron:
        name: ansible-host-report
        user: automation
        minute: "*"
        hour: "*"
        day: "*"
        month: "*"
        weekday: "*"
        job: "/home/automation/reporting/host-report.sh {{ report_phase }}"
YAML

검증하고 적용합니다.

ansible-playbook reporting.yml --syntax-check
ansible-playbook reporting.yml

실제 automation crontab을 확인합니다. 명령이 조회 계정을 명시하도록 become으로 실행합니다.

ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'

각각 ansible-host-report 표식 하나와 매분 실행 명령 및 변경되지 않은 keep-existing-report 항목을 포함해야 합니다. crontab 줄만으로는 실행을 증명할 수 없습니다.

스케줄러의 첫 자연 실행 시점을 기다립니다. timeout은 대기를 90초로 제한하고 bash의 until 반복문은 2초마다 비어 있지 않은 로그를 확인합니다. cron이 로그를 만드는 동안 명령은 상태를 읽기만 합니다.

ansible all -m ansible.builtin.command -a "timeout 90 bash -c 'until test -s /home/automation/reporting/host-report.log; do sleep 2; done'"
ansible all -m ansible.builtin.command -a 'cat /home/automation/reporting/host-report.log'

각각 time= host=web1 phase=initial 같은 기록이 있어야 하며 web2에는 해당 실제 호스트 이름이 있어야 합니다. 타임스탬프는 달라지며 자연스러운 분 단위 예약 실행과 맞아야 합니다. 제한된 대기가 실패하면 직접 성공 로그를 쓰지 말고 예약과 실행 중인 데몬을 확인하세요.

이름 있는 항목 하나 갱신 및 새 단계 관찰하기

이 단계에서는 안정된 신원과 관련 없는 예약을 보존하면서 작업 입력을 바꿉니다.

저장된 단계 값을 갱신합니다. cron 태스크는 같은 이름을 유지하면서 새 명령을 렌더링합니다.

cat > group_vars/all.yml <<'YAML'
report_phase: verified
YAML
ansible-playbook reporting.yml

두 crontab을 다시 확인합니다.

ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'

호스트마다 여전히 정확히 하나의 ansible-host-report 항목이 있고 이제 verified로 끝나야 합니다. 이름을 재사용하면 두 번째 예약을 누적하는 대신 기존 항목을 갱신합니다. keep-existing-report는 변경되지 않아야 합니다.

갱신된 단계를 사용하는 새 자연 실행을 기다립니다. grep -q는 일치하는 로그 기록을 출력 없이 확인하고 timeout과 until 반복문은 대기를 제한합니다.

ansible all -m ansible.builtin.command -a "timeout 90 bash -c 'until grep -q phase=verified /home/automation/reporting/host-report.log; do sleep 2; done'"
ansible all -m ansible.builtin.command -a 'cat /home/automation/reporting/host-report.log'

이제 둘 다 실제 스케줄러의 새 phase=verified 기록을 포함해야 합니다. 이전 initial 기록은 유용한 이력이므로 유지되어야 합니다. crontab 명령 갱신은 오래된 로그 줄을 소급 변경하지 않습니다.

이름 있는 임시 작업만 제거하고 보고 유지하기

이 단계에서는 안정된 이름으로 임시 예약을 제거하고 의도한 일반 사용자 보고서가 계속 실행되는지 검증합니다.

애드혹 cron 태스크로 무해한 이름 있는 임시 항목을 만듭니다. 자정에 /usr/bin/true를 예약하여 실제 제거할 항목을 제공합니다.

ansible all -b -m ansible.builtin.cron -a 'name=ansible-temporary-report user=automation minute=0 hour=0 job=/usr/bin/true'
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'

작은 정리 플레이북을 작성합니다. state absent와 일치하는 name/user는 해당 관리 항목만 제거합니다. 전체 crontab을 비우거나 대체하지 마세요.

cat > cleanup.yml <<'YAML'
- name: Remove only the obsolete named schedule
  hosts: all
  gather_facts: false
  become: true
  tasks:
    - name: Remove the obsolete report entry by its stable name
      ansible.builtin.cron:
        name: ansible-temporary-report
        user: automation
        state: absent
YAML
ansible-playbook cleanup.yml
ansible all -b -m ansible.builtin.command -a 'crontab -u automation -l'

임시 표식과 명령이 없어져야 합니다. phase verified의 ansible-host-report와 keep-existing-report는 모두 남아야 합니다.

두 선언을 반복합니다.

ansible-playbook reporting.yml
ansible-playbook cleanup.yml

두 플레이북은 두 호스트 모두 changed=0, unreachable=0, failed=0을 보고해야 합니다. cron은 독립적으로 로그 기록을 계속 덧붙일 수 있습니다. 이러한 자연스러운 실행 기록은 선언형 배포의 멱등성을 깨뜨리지 않습니다.

최종 검사는 이름 있는 일반 사용자 작업, 정확히 배포된 프로그램, 보호된 로그를 점검한 다음 두 호스트의 또 다른 자연 스케줄러 실행 시점을 기다립니다. 약 1분이 걸릴 수 있습니다. 수동 준비된 성공 파일이 아니라 지속 실행을 확인할 수 있도록 보고 예약과 데몬을 실행 상태로 두세요.

요약

의도한 일반 사용자의 안정된 이름 있는 cron 항목을 관리하고 실제 예약 실행을 관찰했으며 작업을 중복하지 않고 명령 입력을 갱신하고 임시 항목만 제거했습니다. 명시적인 프로그램/로그 권한과 관련 없는 예약 보존으로 범위를 명확히 했습니다. 보고서가 자연스럽게 계속 실행되는 동안 배포 반복은 변경 없이 완료되었습니다.