Ansible은 서버 설정을 자동화하고, 인프라를 구성하는데 사용하는 오픈소스.
운영환경에서 서버를 안정적으로 배포할 수 있을만큼 활용성이 높다.
클라우드 환경에서 서버를 설정할 때 Ansible를 활용한다.
스케일 아웃 구조로 운영해야 할 서버의 수가 변하는 경우 설치된 OS에 서비스를 운용하기 위한 패키지들을 설치해 줘야 한다.
이때, Cloud Init의 오픈소스를 사용해서 OS가 구동된 후 프로그램 사용자가 등록한 스크립트에 따라서 필요한 패키지를 설치해 준다. 그렇지만 사용하는 서비스의 종류가 달라질 것이고 달라질 때마다 추가해줘야 하는 스크립트를 사용자가 관리하기가 어려워진다.
이 문제를 해결하는 방법 중 하나가 Ansible이다
Ansible은 서버의 설정과 인프랑 프로비저닝 등을 자동화할 수 있는 오픈소스 도구이다.
playbook.yml 파일을 통해 작업을 정의- name: Install and Configure Nginx
# webserver 그룹에 속한 원격서버에서 작업을 수행
hosts: webserver
# 관리자 권한으로 실행되도록 설정
become: true
#apt 모듈을 사용하여 nginx를 설치
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Copy Nginx configuration file
# 템플릿 파일을 dest 경로에 복사
template:
src: nginx.conf.j2
dest: etc/nginx/nginx.conf
# Restart Nginx 핸들러를 호출
notify:
- Restart Nginx
handlers:
- name: Restart Nginx
# nginx 모듈을 재시작
service:
name: nginx
state: restarted
roles/
commons/ # hierarchy represents a 'role'
tasks/ # include role tasks defined through yml
main.yml
handlers/ # include handler file that execute when event arise
files/ # inlcude static file that be copied to remote system
foo.txt
bar.sh
templates/ # template file that used as dynamically create file
ntp.conf.j2
vars/ #include varialbe file
main.yml
defaults/ #include file that used default variable
main.yml
meta/ # include metadata file (Author, dependencies, lisense)
main.yml
library/ # inlcude custom module
module_utils/ inlcude custom module_utils
webtier/ # same kind of common structure
monitoring/ # ``
name: hello
hosts: {{target | default('all')}}
roles:
- {
role: roles-remote/devops.common,
hs_service_name: "hello",
become: yes
}
...
- {
role: rols-remote/devops.ds,
become: yes,
when: df_enabled|default(false)
- src: git+https://github.com/dev...
name: devops.common
...
ansible-galaxy install 명령어를 통해서 local에 필요한 파일 다운로드