Ansible은 에이전트가 필요 없는 자동화 도구로, IT 인프라 및 애플리케이션 배포를 간소화하는 데 사용된다. SSH 기반으로 원격 서버를 관리하며, YAML 기반의 Playbook을 사용하여 작업을 자동화한다.
| 개념 | 설명 |
|---|---|
| Inventory (인벤토리) | 관리할 호스트(서버) 목록을 정의 (예: hosts.yml) |
| Module (모듈) | Ansible에서 제공하는 작업 단위 (예: apt, yum, service) |
| Task (태스크) | 모듈을 사용하여 수행할 작업 정의 |
| Playbook (플레이북) | 여러 개의 Task를 YAML 형식으로 정의한 파일 |
| Role (롤) | 재사용 가능한 Playbook을 구조화하는 방법 |
Ansible Galaxy는 Ansible Role을 공유하고 다운로드할 수 있는 플랫폼으로, 다른 사람이 만든 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 nginx | Galaxy에서 nginx 관련 Role 검색 |
update , upgrade 차이
| 명령어 | 역할 | 실행 필요 여부 |
|---|---|---|
apt update | 패키지 목록을 최신 상태로 업데이트 (설치 X) | 필수 |
apt upgrade | 업데이트된 패키지를 설치 (새 패키지는 설치 X) | 필수 |
apt dist-upgrade | 업그레이드 + 의존성 자동 정리 | 선택 (시스템 업그레이드 시 추천) |
정확한 업데이트를 위해 항상 apt update → apt 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
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
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