Provisioning, tools / Ansible

Jeonghak Cho·2025년 3월 17일

Provisioning

목록 보기
14/44

Ansible 개요

Ansible은 에이전트가 필요 없는 자동화 도구로, IT 인프라 및 애플리케이션 배포를 간소화하는 데 사용된다. SSH 기반으로 원격 서버를 관리하며, YAML 기반의 Playbook을 사용하여 작업을 자동화한다.

특징

  • 에이전트리스 (Agentless) → 원격 서버에 별도 소프트웨어 설치 불필요 (SSH 사용)
  • YAML 기반 Playbook → 사람이 읽기 쉬운 형식으로 자동화 스크립트 작성
  • Idempotency (항등성) → 같은 Playbook을 여러 번 실행해도 상태가 동일하게 유지됨
  • 멀티 플랫폼 지원 → Linux, Windows, 클라우드 환경(AWS, GCP, Azure) 등 관리 가능

주요 개념

개념설명
Inventory (인벤토리)관리할 호스트(서버) 목록을 정의 (예: hosts.yml)
Module (모듈)Ansible에서 제공하는 작업 단위 (예: apt, yum, service)
Task (태스크)모듈을 사용하여 수행할 작업 정의
Playbook (플레이북)여러 개의 Task를 YAML 형식으로 정의한 파일
Role (롤)재사용 가능한 Playbook을 구조화하는 방법

Ansible Galaxy 개요

Ansible Galaxy는 Ansible Role을 공유하고 다운로드할 수 있는 플랫폼으로, 다른 사람이 만든 Role을 쉽게 가져와 사용할 수 있다.

기능

  • 공유된 Role 다운로드 가능 → 기존에 작성된 Role을 재사용
  • 자신만의 Role 생성 가능 → ansible-galaxy init 명령어로 새로운 Role 생성
  • Role 관리 → requirements.yml 파일을 통해 Role을 쉽게 설치 가능

주요 명령어

명령어설명
ansible-galaxy init my_role새로운 Role 생성 (roles/my_role 구조 자동 생성)
ansible-galaxy install geerlingguy.nginx지정된 Role을 다운로드 및 설치
ansible-galaxy list설치된 Role 목록 확인
ansible-galaxy remove my_role특정 Role 제거
ansible-galaxy search nginxGalaxy에서 nginx 관련 Role 검색

Ansible 설치 ( Ansible-Galaxy 함께 설치)

update , upgrade 차이

명령어역할실행 필요 여부
apt update패키지 목록을 최신 상태로 업데이트 (설치 X)필수
apt upgrade업데이트된 패키지를 설치 (새 패키지는 설치 X)필수
apt dist-upgrade업그레이드 + 의존성 자동 정리선택 (시스템 업그레이드 시 추천)

정확한 업데이트를 위해 항상 apt updateapt upgrade 순서로 실행!

sudo apt update && sudo apt upgrade -y

sudo apt install ansible
sudo apt update && sudo apt install -y sshpass

버전 확인

vagrant@master:~$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Mar 11 2025, 17:45:31) [GCC 9.4.0]

vagrant@master:~/k8s-install$ ansible-galaxy --version
ansible-galaxy 2.9.6
  config file = /home/vagrant/k8s-install/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-galaxy
  python version = 3.8.10 (default, Mar 11 2025, 17:45:31) [GCC 9.4.0]

프로젝트 생성

다음 형태로 프로젝트를 구성한다.

vagrant@master:~/k8s-install$ tree
.
├── ansible.cfg
├── inventories
│   └── hosts.yml
├── roles
│   └── common
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── README.md
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       ├── tests
│       │   ├── inventory
│       │   └── test.yml
│       └── vars
│           └── main.yml
└── site.yml

프로젝트 생성은 폴더 생성으로 끝난다.

mkdir myproject
cd myproject

설정

vi ansible.cfg

[defaults]
inventory = inventories/hosts.yml
remote_user = vagrant
host_key_checking = False
timeout = 30

Site

become: yes 는 루트 권한으로 role 수행을 하게 한다.

---
- name: Setup common
  hosts: common
  become: yes
  roles:
    - common

인벤토리 생성

패스워드를 입력하는 것은 테스트 환경에서만 진행한다. ansible playbook 실행 시 패스워드를 입력하는 식이 권장된다.

ansible-playbook -i hosts.yml site.yml --ask-become-pass
BECOME password:*****

vi inventories/hosts.yml

all:
  children:
    common:
      hosts:
        192.168.56.10:
          ansible_user: vagrant
          ansible_password: vagrant
        192.168.56.101:
          ansible_user: vagrant
          ansible_password: vagrant
        192.168.56.102:
          ansible_user: vagrant
          ansible_password: vagrant
        192.168.56.103:
          ansible_user: vagrant
          ansible_password: vagrant

role 생성

common 역할을 하나 만든다.

ansible-galaxy init roles/common

시험 삼아 모든 노드에 웹서버를 설치해 본다.
vi roles/common/tasks/main.yml

- name: Install Nginx
  apt:
    name: nginx
    state: present

- name: Start Nginx Service
  systemd:
    name: nginx
    state: started
    enabled: yes

실행

ansible-playbook -i inventories/hosts.yml site.yml

실행하면 모든 노드에 웹서버가 깔린다. 노드 마다 들어가서 'curl localhost'라고 치면 index.html 내용이 보일 것이다.

Nginx 를 지우기 위한 태스크를 추가한다.
vi roles/common/tasks/main.yml

- name: Uninstall Nginx (with configuration files)
  apt:
    name: nginx
    state: absent
    purge: yes
  tags: delete
- name: Remove Nginx configuration and logs
  file:
    path: "{{ item }}"
    state: absent
  loop:
    - /etc/nginx
    - /var/log/nginx
    - /var/cache/nginx
  tags: delete

tags 를 delete로 주었다. delete 만 수행하려면 다음과 같이 한다.

ansible-playbook -i inventories/hosts.yml site.yml --tags delete

0개의 댓글