Skip to content

ansible-directory-structure

Ansibleμ—μ„œ inventory, playbooks, rolesλ₯Ό ν¬ν•¨ν•œ 디렉토리 κ΅¬μ‘°λŠ” 가독성과 μž¬μ‚¬μš©μ„±, μœ μ§€λ³΄μˆ˜ μš©μ΄μ„±, ν™˜κ²½ 뢄리, μ—­ν•  λͺ…ν™•ν™” 등을 μœ„ν•΄ ν‘œμ€€μ μΈ ꡬ쑰둜 κ΅¬μ„±ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. μ•„λž˜μ— μ‹€λ¬΄μ—μ„œ κ°€μž₯ 널리 μ“°μ΄λŠ” 디렉토리 ꡬ쑰 μ˜ˆμ‹œμ™€ 각 κ΅¬μ„±μš”μ†Œμ˜ 역할을 μ„€λͺ…λ“œλ¦΄κ²Œμš”.


πŸ“ Best Practice 디렉토리 κ΅¬μ‘°βš‘

ansible-project/
β”œβ”€β”€ inventories/
β”‚   β”œβ”€β”€ production/
β”‚   β”‚   β”œβ”€β”€ hosts.yml
β”‚   β”‚   └── group_vars/
β”‚   β”‚       β”œβ”€β”€ all.yml
β”‚   β”‚       β”œβ”€β”€ web.yml
β”‚   β”‚       └── db.yml
β”‚   └── staging/
β”‚       β”œβ”€β”€ hosts.yml
β”‚       └── group_vars/
β”œβ”€β”€ playbooks/
β”‚   β”œβ”€β”€ site.yml
β”‚   β”œβ”€β”€ web.yml
β”‚   β”œβ”€β”€ db.yml
β”‚   └── k3s-cluster.yml
β”œβ”€β”€ roles/
β”‚   β”œβ”€β”€ common/
β”‚   β”‚   β”œβ”€β”€ tasks/
β”‚   β”‚   └── templates/
β”‚   β”œβ”€β”€ k3s/
β”‚   β”œβ”€β”€ helm/
β”‚   β”œβ”€β”€ argocd/
β”‚   └── app/
β”œβ”€β”€ files/
β”‚   └── custom-configs/
β”œβ”€β”€ templates/
β”‚   └── nginx.conf.j2
β”œβ”€β”€ group_vars/
β”‚   └── all.yml
β”œβ”€β”€ ansible.cfg
└── requirements.yml

🧩 각 κ΅¬μ„±μš”μ†Œ μ„€λͺ…βš‘

1. inventories/βš‘

  • ν™˜κ²½λ³„ 인벀토리 μ •μ˜ (예: production, staging)

  • 각 ν™˜κ²½λ³„ hosts.yml + group_vars/ ꡬ쑰둜 λ‚˜λˆ  μœ μ§€λ³΄μˆ˜ 용이

  • πŸ”‘ SSH 정보, λ³€μˆ˜ μ„€μ •, 호슀트 κ·Έλ£Ήν™” 등을 μ„€μ •

ansible-playbook -i inventories/production/hosts.yml playbooks/site.yml

2. playbooks/βš‘

  • μ‹€μ œ 싀행될 ν”Œλ ˆμ΄λΆ YAML 파일 μœ„μΉ˜

  • 보톡 μ—­ν• (role)을 λ¬ΆλŠ” μƒμœ„ λ‹¨μœ„μ˜ orchestration μ—­ν•  μˆ˜ν–‰

  • 예: web.yml, k3s-cluster.yml, deploy-app.yml

- name: Web setup
  hosts: web
  roles:
    - common
    - nginx

3. roles/βš‘

  • Ansible의 핡심 μž¬μ‚¬μš© λ‹¨μœ„

  • 각 μ—­ν• (role)은 독립적인 κΈ°λŠ₯ λ‹¨μœ„ (예: k3s, helm, argocd)

  • ν•˜μœ„ λ””λ ‰ν† λ¦¬λ‘œ 뢄리:

roles/helm/
β”œβ”€β”€ tasks/main.yml
β”œβ”€β”€ defaults/main.yml
β”œβ”€β”€ files/
β”œβ”€β”€ templates/
β”œβ”€β”€ handlers/main.yml
└── vars/main.yml
  • λ‹€λ₯Έ ν”„λ‘œμ νŠΈμ—μ„œλ„ μž¬μ‚¬μš© κ°€λŠ₯

4. group_vars/, host_vars/βš‘

  • κ·Έλ£Ή λ˜λŠ” κ°œλ³„ 호슀트 λ‹¨μœ„ λ³€μˆ˜ μ„€μ •

  • group_vars/all.yml: λͺ¨λ“  ν™˜κ²½μ— 곡톡 μ μš©λ˜λŠ” λ³€μˆ˜

예: group_vars/web.yml

nginx_port: 80

5. files/, templates/βš‘

  • files/: κ·ΈλŒ€λ‘œ 볡사할 νŒŒμΌλ“€

  • templates/: Jinja2 문법 μ‚¬μš©ν•˜μ—¬ λ³€μˆ˜λ₯Ό λ™μ μœΌλ‘œ 넣을 수 μžˆλŠ” ν…œν”Œλ¦Ώ


6. ansible.cfgβš‘

  • Ansible 싀행에 λŒ€ν•œ 섀정을 μ •μ˜

  • 예: κΈ°λ³Έ inventory, ssh args, roles_path λ“±

[defaults]
inventory = inventories/production/hosts.yml
roles_path = roles

βœ… 운영 κΈ°μ€€ μš”μ•½βš‘

디렉토리 λͺ©μ 
inventories/ ν™˜κ²½λ³„ 인벀토리 및 λ³€μˆ˜ 관리
playbooks/ μ‹€ν–‰ λ‹¨μœ„ 묢음 (호좜점)
roles/ κΈ°λŠ₯ λ‹¨μœ„ μž¬μ‚¬μš© λͺ¨λ“ˆ
group_vars/ 그룹별 λ³€μˆ˜
host_vars/ ν˜ΈμŠ€νŠΈλ³„ λ³€μˆ˜
files/ 정적 파일 배포
templates/ Jinja2 기반 ν…œν”Œλ¦Ώ
ansible.cfg κΈ°λ³Έ λ™μž‘ μ„€μ •

🧠 TIPβš‘

  • λ‘œμ»¬μ—μ„œ κ΄€λ¦¬ν•˜λ‹€κ°€ ν΄λΌμš°λ“œλ‘œ ν™•μž₯ν•  κ³„νšμ΄λΌλ©΄ 이 κ΅¬μ‘°λŠ” κ·ΈλŒ€λ‘œ ν™•μž₯ κ°€λŠ₯

  • μ—­ν• λ§Œ μΆ”κ°€ν•˜κ³ , inventory만 ν΄λΌμš°λ“œμ— 맞게 늘리면 됨