총 3대의 VM 을 준비합니다.
Control Node : 1ea
Managed Node: 2ea
목표 : 웹 서버 자동화 배포 프로젝트
실습 목표
- Ansible의 롤(Roles) 개념을 활용한 구조화된 프로젝트 실습
- 여러 서버에 공통 설정 및 Apache 웹 서버 설치 자동화
디렉터리 구조
ansible-mini-project/
├── inventory.ini
├── site.yml
├── roles/
│ ├── common/
│ │ └── tasks/main.yml
│ ├── ntp/
│ │ └── tasks/main.yml
│ └── apache/
│ ├── tasks/main.yml
│ ├── handlers/main.yml
│ └── templates/index.html.j2
대상 서버 목록
# inventory.ini
[web]
192.168.60.160 ansible_user=w1
192.168.60.161 ansible_user=w2
1. 대상 서버 ping 테스트
ansible -i inventory.ini all -m ping
메인 playbook
1. site.yaml 파일 생성
- name: Configure Web Servers
hosts: web
become: yes
roles:
- common
- ntp
- apache
2. roles/common/tasks/main.yaml 파일 생성
- name: Update apt cache
apt:
update_cache: yes
- name: Install common packages
apt:
name: [curl, vim]
state: present
3. roles/ntp/tasks/main.yaml 파일 생성
- name: Install NTP
apt:
name: ntp
state: present
- name: Ensure NTP is running
service:
name: ntp
state: started
enabled: yes
4. roles/apache/tasks/main.yaml 파일 생성
- name: Install Apache
apt:
name: apache2
state: present
- name: Deploy custom index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
notify: Restart Apache
5. roles/apache/handlers/main.yaml 파일 생성
- name: Restart Apache
service:
name: apache2
state: restarted
6. roles/apache/templates/index.html.j2 파일 생성
<h1>Hello from {{ inventory_hostname }}</h1>
<p>Welcome to your Ansible-deployed Apache Server!</p>
실행
ansible-playbook -i inventory.ini site.yml --ask-become-pass
결과
브라우저 확인
단순한 작업도 Roles 를 통해 구조화하여 보다 안정적이고 확장 가능한 인프라 자동화를 구축하여 Ansible 을 이해했습니다.
'사이드 프로젝트' 카테고리의 다른 글
Terraform small project (0) | 2025.04.24 |
---|---|
DEVOPS - CI/CD 자동화 (0) | 2025.03.07 |