본문 바로가기
Q&A2022년 8월 14일1분 읽기

Ansible로 서버 자동화하기

오
오수빈
조회 656댓글 1

서버 여러 대에 같은 설정을 반복하는 게 지겨워서 Ansible을 배우려고 합니다.

댓글 1

김김영삼4년 전

Ansible 기본 사용법:

1. 인벤토리 (hosts):

# /etc/ansible/hosts 또는 inventory.yml
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[dbservers]
db1 ansible_host=192.168.1.20

2. Playbook (자동화 스크립트):

# deploy.yml
---
- hosts: webservers
become: yes
tasks:
- name: Update apt cache
apt: update_cache=yes

- name: Install nginx
apt: name=nginx state=latest

- name: Start nginx
service: name=nginx state=started enabled=yes

- name: Copy config
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx

handlers:
- name: restart nginx
service: name=nginx state=restarted

실행:

ansible-playbook -i inventory.yml deploy.yml

SSH만 되면 에이전트 설치 없이 동작합니다.

Ctrl+Enter로 등록