IaC/Ansible 실습

Ansible 실습 | Docker로 Apache 웹 서비스 배포 Playbook 작성

Greta Lee 2021. 8. 18. 18:04
SMALL

Docker로 웹 서비스 배포하는 Playbook 작성

  1. Docker 설치
  2. Apache 웹 서비스 컨테이너 생성
  3. 웹 서비스 접근되는지 확인


◆ 참고 : Ubuntu에 Docker 설치하기
https://docs.docker.com/engine/install/ubuntu/

 

Install Docker Engine on Ubuntu

 

docs.docker.com


◆ Docker 관련 모듈 확인

ansible-doc -l | grep docker


1. Playbook 작성

- name: Create Docker Containers for Apache
  hosts: 192.168.200.102
  tasks:
  - name: Install related Packages
    apt:
      update_cache: yes
      name: apt-transport-https, ca-certificates, curl, python3-pip, virtualenv, python3-setuptools, gnupg, lsb-release
      state: present
  # key 파일을 받아서 gpg 명령어 적용
  - name: Add docker's gpg key
    apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present
  - name: Add repo Repository
    apt_repository:
      repo: deb https://download.docker.com/linux/ubuntu bionic stable
      state: present
  - name: Install Docker packages
    apt:
      update_cache: yes
      name: docker-ce, docker-ce-cli, containerd.io
  # docker 파이썬 라이브러리 설치
  - name: Install Docker for Python
    pip:
      name: docker
      state: present
  - name: Run Docker Container for Httpd
    docker_container:
      name: httpd
      image: httpd:2.4
      ports:
      - "8080:80"
      state: started
  - name: Check Web Status
    uri:
      url: http://192.168.200.102:8080
      return_content: yes
      status_code: 200

 


2. Playbook 실행

ansible-playbook docker.yaml -b


3. 동작 Test

curl 192.168.200.102:8080

 

SMALL