본문 바로가기

리눅스

Ansible Playbook으로 NTP 서비스 자동 배포하기

반응형

Ansible Playbook 실행하기

Ansible Playbook을 활용하면 여러 서버에 동일한 설정을 손쉽게 배포할 수 있다.

이번 예제에서는 roles 구조를 사용하여 NTP 패키지를 설치하고 ntp.conf 파일을 배포한 뒤 서비스를 자동으로 재시작하는 방법입니다.

테스트 환경

Control Machine : Ansible Playbook 실행 서버

Managed Node : 설정이 적용될 대상 서버

목적 : NTP 패키지 설치 및 /etc/ntp.conf 배포

1. Playbook 예제 다운로드

Control Machine에서 Ansible 예제 파일을 다운로드한다.

cd /etc/ansible

git clone git://github.com/ansible/ansible.git --recursive
git clone https://github.com/ansible/ansible-examples.git --recursive

2. Playbook 디렉터리 구조

Ansible의 roles 구조를 사용하면 작업(Task), 핸들러(Handler), 템플릿(Template)을 체계적으로 관리할 수 있다.

tree
.
├── roles
│   └── common
│       ├── handlers
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       └── templates
│           └── ntp.conf.j2
└── site.yml

3. Playbook 구성

3-1. site.yml

전체 노드에 공통 설정을 적용하는 메인 Playbook이다.

site.yml
---
# This playbook deploys the whole application stack in this site.

- name: apply common configuration to all nodes
  hosts: all
  remote_user: root

  roles:
    - common

3-2. Handler 설정 설정

파일 변경 시 NTP 서비스를 재시작하도록 Handler를 정의한다.

roles/common/handlers/main.yml
---
# Handler to handle common notifications.

- name: restart ntp
  service:
    name: ntpd
    state: restarted
728x90

3-3. Task 설정

NTP 패키지 설치, 설정 파일 배포, 서비스 시작 작업을 정의한다.

roles/common/tasks/main.yml
---
# Common tasks for all managed nodes.

- name: Install ntp
  yum:
    name: ntp
    state: present
  tags:
    - ntp

- name: Configure ntp file
  template:
    src: ntp.conf.j2
    dest: /etc/ntp.conf
  tags:
    - ntp
  notify:
    - restart ntp

- name: Start the ntp service
  service:
    name: ntpd
    state: started
    enabled: yes
  tags:
    - ntp

- name: Check SELinux status
  command: getenforce
  register: sestatus
  changed_when: false

3-4. Template 파일 작성

Jinja2 템플릿을 사용하여 NTP 서버 정보를 동적으로 설정한다.

 roles/common/templates/ntp.conf.j2
driftfile /var/lib/ntp/drift

restrict 127.0.0.1
restrict -6 ::1

server {{ ntpserver }}

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

4. Playbook 실행

다음 명령으로 Playbook을 실행한다.

ansible-playbook site.yml
PLAY [apply common configuration to all nodes] *********************************

TASK [setup] *******************************************************************
ok: [192.168.0.252]

TASK [common : Install ntp] ****************************************************
ok: [192.168.0.252]

TASK [common : Configure ntp file] *********************************************
changed: [192.168.0.252]

TASK [common : Start the ntp service] ******************************************
changed: [192.168.0.252]

TASK [common : Check SELinux status] *******************************************
ok: [192.168.0.252]

RUNNING HANDLER [common : restart ntp] *****************************************
changed: [192.168.0.252]

PLAY RECAP *********************************************************************
192.168.0.252 : ok=6 changed=3 unreachable=0 failed=0

5. Managed Node 결과 확인

5-1. NTP 패키지 설치 확인

rpm -qa | grep ntp
ntpdate-4.2.6p5-10.el6.centos.2.x86_64
ntp-4.2.6p5-10.el6.centos.2.x86_64

5-2. ntp.conf 배포 확인

cat /etc/ntp.conf
driftfile /var/lib/ntp/drift

restrict 127.0.0.1
restrict -6 ::1

server 192.168.1.2

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

 

서버 수가 많아질수록 수작업보다 Playbook 기반 운영이 훨씬 효율적이며 설정 표준화와 운영 안정성 확보에도 큰 도움이 된다.

 

728x90
반응형