본문 바로가기

Project

Ansible 웹서버 구축

1단계. apache ROLE 생성 및 사용하기

다음 요구 사항에 맞춰 /home/greg/ansible/roles 디렉토리에 apache 역할을 생성하고, 이를 사용하여 웹 서버를 구성해 보았습니다.

  • httpd 소프트웨어 패키지가 설치되고, 시스템 시작 시 활성화시작 상태여야 합니다.
  • 방화벽이 활성화되어 실행 중이어야 하며, 웹 서버 접근을 허용하는 규칙을 사용해야 합니다.
  • index.html.j2 템플릿 파일이 존재하며, 이 파일을 사용해 다음과 같은 출력 파일을 생성해야 합니다.

1. apache 역할 생성

ansible-galaxy를 사용하여 apache 역할을 생성합니다.

# apache 역할 생성
ansible-galaxy role init --init-path /home/greg/ansible/roles apache

2. tasks/main.yml 작성

apache 역할의 작업을 정의합니다.

# tasks/main.yml 파일 편집
vim /home/greg/ansible/roles/apache/tasks/main.yml

 

다음과 같이 작업을 작성합니다.

---
- name: Install Apache
  yum:
    name: httpd
    state: latest

- name: Start and enable Apache service
  systemd:
    name: httpd
    state: started
    enabled: yes

- name: Start and enable firewalld
  systemd:
    name: firewalld
    state: started
    enabled: yes

- name: Configure firewalld to allow HTTP
  firewalld:
    service: http
    permanent: yes
    state: enabled
    immediate: yes

- name: Deploy index.html template
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

3. templates/index.html.j2 작성

템플릿 파일을 생성하여 웹 페이지의 내용을 정의합니다.

# templates/index.html.j2 파일 편집
vim /home/greg/ansible/roles/apache/templates/index.html.j2

 

다음과 같이 템플릿을 작성합니다.

Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}

4. 플레이북 작성

apache 역할을 사용하는 플레이북을 작성합니다.

# 플레이북 파일 생성
vim /home/greg/ansible/apache.yml

 

다음과 같이 플레이북을 작성합니다.

---
- name: Deploy Apache role
  hosts: webservers
  roles:
    - apache

5. 플레이북 실행 및 검증

플레이북을 실행하고 결과를 검증합니다.

# 플레이북 실행
ansible-navigator run apache.yml -m stdout

# 검증
ansible webservers -a 'systemctl status httpd'
ansible webservers -a 'firewall-cmd --list-all'
ansible webservers --list-hosts
curl http://node3
curl http://node4

2단계. Ansible Galaxy에서 ROLE 사용하기

/home/greg/ansible/roles.yml 플레이북을 생성하고, balancer 및 phpinfo 역할을 사용해 보았습니다.

 

  • balancer는 웹서버의 로드밸런서를 의미하며, node3, node4에 연결되어 있어야 합니다.
  • phpinfo는 php로 이루어진 웹사이트의 자세한 정보를 출력해줍니다.

 


1. roles.yml 플레이북 작성

 

balancer 및 phpinfo 역할을 사용하는 플레이북을 작성합니다.

# 플레이북 파일 생성
vim /home/greg/ansible/roles.yml

 

다음과 같이 플레이북을 작성합니다.

---
- name: Use phpinfo role
  hosts: webservers
  roles:
    - phpinfo

- name: Use balancer role
  hosts: balancers
  roles:
    - balancer

2. 플레이북 실행 및 검증

플레이북을 실행하고 결과를 검증합니다.

# 플레이북 실행
ansible-navigator run /home/greg/ansible/roles.yml -m stdout

# 검증
curl http://172.25.250.13
curl http://node3/hello.php
curl http://node4/hello.php

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Project' 카테고리의 다른 글

Ansible 하드웨어 보고서 생성하기  (0) 2025.02.25
Ansible 파티션 설정  (0) 2025.02.25
Ansible 사용자 계정 생성하기  (0) 2025.02.25
Ansible 설치 및 구성  (0) 2025.02.25