C.W.K.
Stream
Lesson 04 of 08 · published

Ansible playbook — YAML 의 idempotent ops

~10 min · yaml, ansible, automation

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Task 리스트로서의 서버 설정

Ansible playbook 이 YAML play 리스트, 각각 host 타깃하는 task 포함. Runtime 이 각 host 에 SSH 후 task 순서대로 idempotent 하게 실행 — 이미 설정된 host 에 playbook 재실행 시 변경 없는 task 엔 no-op.

다섯 핵심 섹션

  • hosts: — 어느 inventory 그룹 타깃.
  • vars: — playbook 범위 변수 (또는 vars/ 파일에서 include).
  • tasks: — 순서 있는 액션. 각각이 module 호출 (apt:, copy:, systemd:) + 파라미터.
  • handlers: — task 의 notify: 에서 fire. 여러 번 알림 받아도 play 끝에 한 번만 실행.
  • roles: — task, var, file, template 의 재사용 묶음.

Inventory — host 파일

Inventory 는 YAML, INI, 또는 동적 (클라우드 API 플러그인). YAML inventory 가 host 그룹화 + 변수 할당.

원칙: Ansible 의 가치는 idempotency. 잘 작성된 task 는 '이 명령 실행' 이 아니라 '원하는 state 선언'. shell: 또는 command: 로 fallback 전 전용 module (apt:, file:, systemd:) 잡아. 전용 module 이 '이미 됐음' 감지 후 skip 할 줄 앎.

Code

작은 playbook·yaml
---
- name: Configure web servers
  hosts: web
  become: true
  vars:
    nginx_user: www-data
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
        update_cache: true

    - name: Deploy nginx config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        mode: '0644'
      notify: Restart nginx

    - name: Ensure nginx is running
      systemd:
        name: nginx
        state: started
        enabled: true

  handlers:
    - name: Restart nginx
      systemd:
        name: nginx
        state: restarted
YAML inventory·yaml
all:
  children:
    web:
      hosts:
        web1.example.com:
        web2.example.com:
      vars:
        nginx_workers: 4
    db:
      hosts:
        db1.example.com:
          ansible_user: postgres
Playbook 실행·bash
# Dry run (--check)
ansible-playbook -i inventory.yaml site.yaml --check

# 적용
ansible-playbook -i inventory.yaml site.yaml

# web task 만 실행
ansible-playbook -i inventory.yaml site.yaml --limit web

# 디버깅 verbose
ansible-playbook -i inventory.yaml site.yaml -vvv

External links

Exercise

최근 수동으로 한 server-side 변경 (패키지 설치, config 배포, systemd service 설정) 골라. 3-task Ansible play 로 표현. 테스트 host 에 --check 로 실행. dry-run 모드가 정확히 무엇이 변할지 알려주는 거 봐 — 그 리포트가 워크플로우 이득.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.