빠른 참조

Ansible 초보자용 퀵스타트 가이드 치트시트

Ansible 초보자를 위한 단계별 가이드: 설치, 프로젝트 구조, 인벤토리 구성, 플레이북 작성 및 자동화 실행.

· 게시일

Ansible 설치, 인벤토리 파일 구성, 첫 번째 YAML 플레이북 작성 및 서버 작업 오케스트레이션을 위한 실무 입문 가이드.

🔗 Related: Ansible 기본 치트시트 · Ansible Roles 치트시트 · Ansible 플레이북 실무 예제

설치 방법

# macOS (Homebrew)
brew install ansible

# Ubuntu / Debian
sudo apt update && sudo apt install -y ansible

# Python Pip (Universal)
pip install ansible

참고: Ansible 공식 설치 문서: Ansible Installation Docs


프로젝트 초기 설정

mkdir -p ~/ansible-setup
cd ~/ansible-setup

인벤토리, 플레이북 및 변수 정의를 위한 전용 작업 디렉터리를 생성합니다.


구성 파일

인벤토리 파일 (Inventory)

hosts

[webservers]
192.168.0.10
192.168.0.11

[databases]
192.168.0.20

[local]
localhost ansible_connection=local

인벤토리는 논리적으로 그룹화된 대상 노드를 정의합니다. ansible_connection=local 설정을 사용하면 SSH 연결 없이도 로컬 환경에서 자동화를 테스트할 수 있습니다.

참고: Ansible 인벤토리 작성 가이드: Ansible How to build your inventory


플레이북 정의

playbook.yml

---
- name: Basic Server Provisioning
  hosts: webservers
  become: true
  tasks:
    - name: Update apt package cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Ensure Nginx is installed
      apt:
        name: nginx
        state: present

    - name: Start Nginx and enable on boot
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Create hello world banner
      shell: echo "Managed by Ansible" > /tmp/motd.txt

실행 가이드

연결 검증 및 플레이북 실행

애드혹 ping 명령어로 연결 상태 확인:

ansible -i hosts all -m ping

인벤토리를 대상으로 플레이북 전체 실행:

ansible-playbook -i hosts playbook.yml

드라이 런 (체크 모드)

원격 노드에 실제 변경 사항을 적용하지 않고 변경 예측 내용을 사전 확인:

ansible-playbook -i hosts playbook.yml --check --diff

참고 자료

관련 주제