본문 바로가기

리눅스

[draft] Ansible 인벤토리를 YAML 파일로 설정하는 방법

Ansible 인벤토리를 YAML 파일로 설정하는 방법

Ansible은 인벤토리(Inventory)를 통해 관리 대상 서버와 그룹 구조를 정의합니다.

기본 INI 형식뿐 아니라 YAML 형식의 인벤토리를 지원하며 YAML은 계층 구조 표현, 가독성, 대규모 환경 관리에 특히 유리합니다.

YAML 기반 Ansible 인벤토리란?

정의

  • 호스트 그룹(Group)
  • 각 그룹에 속한 호스트(Host)
  • SSH 접속 정보 (IP, 사용자, 인증 방식 등)
  • 그룹 간 계층 관계 (children)

권장 파일명

  • hosts.yml
  • hosts.yaml
  • inventory.yml
  • inventory.yaml

기본 YAML 인벤토리 예제

인벤토리 파일 생성

vim hosts.yml
---
all:  # all 그룹 정의
  hosts:
    webserver:
      ansible_host: 192.168.1.10
      ansible_user: ubuntu
      ansible_ssh_private_key_file: /path/to/private_key.pem
    database:
      ansible_host: 192.168.1.11
      ansible_user: centos
      ansible_password: your_password

webservers:  # webservers 그룹 정의
  hosts:
    web1:
      ansible_host: 192.168.1.20
      ansible_user: ubuntu
      ansible_ssh_private_key_file: /path/to/private_key.pem
    web2:
      ansible_host: 192.168.1.21
      ansible_user: centos
      ansible_password: your_password

dbservers:  # dbservers 그룹 정의
  hosts:
    db1:
      ansible_host: 192.168.1.30
      ansible_user: ubuntu
      ansible_ssh_private_key_file: /path/to/private_key.pem
    db2:
      ansible_host: 192.168.1.31
      ansible_user: centos
      ansible_password: your_password

이 예제에서는 all, webservers, dbservers와 같은 그룹을 정의하고 각 그룹에 속하는 호스트를 지정했습니다. 호스트는 해당하는 그룹에 속하며 필요한 연결 정보(호스트 주소, 사용자명, 비밀번호 또는 키 파일 등)를 제공합니다.

YAML 인벤토리를 사용한 플레이북 실행

ansible-playbook -i hosts.yml playbook.yml
  • -i hosts.yml : YAML 인벤토리 지정
  • playbook.yml : 실행할 플레이북

대규모 환경을 위한 인벤토리 디렉터리 구조

운영 환경에서는 인벤토리 디렉터리 구조를 사용하는 것이 일반적입니다.

 

디렉터리 구조

inventory/
├── inventory.yaml
├── group_vars/
│   └── all.yml
├── blog/
│   ├── hosts.yaml
│   ├── b-db/
│   │   └── hosts.yaml
│   └── b-web/
│       └── hosts.yaml
└── site-a/
    ├── hosts.yaml
    ├── development/
    │   └── hosts.yaml
    ├── stage/
    │   └── hosts.yaml
    └── production/
        └── hosts.yaml

inventory.yaml (루트 인벤토리)

# inventory.yaml
all:
  children:
    blog:
    site-a:

  hosts:
    localhost:
      ansible_host: 192.168.50.24
      ip: 192.168.50.24

서비스 및 환경별 hosts.yaml 구성

site-a 서비스

cat ./site-a/hosts.yaml
# site-a/hosts.yaml
site-a:
  children:
    development:
    stage:
    production:

개발 환경

cat ./site-a/development/hosts.yaml
# site-a/development/hosts.yaml
development:
  hosts:
    devweb-214:

스테이지 환경

cat ./site-a/stage/hosts.yaml
# site-a/development/hosts.yaml
development:
  hosts:
    devweb-214:

운영 환경

cat ./site-a/production/hosts.yaml
# site-a/production/hosts.yaml
production:
  hosts:
    websrv-231:
    websrv-232:

group_vars를 활용한 공통 설정 관리

모든 그룹에 공통 적용 (group_vars/all.yml)

vim group_vars/all.yml
ansible_connection: ssh
ansible_port: 22
ansible_ssh_user: ec2-user
ansible_ssh_private_key_file: ~/aws-key/keyfile.pem
# ansible_python_interpreter: /usr/bin/python

인벤토리 구조 확인

트리 구조 확인

tree inventory

Ansible 인벤토리 그래프 출력

ansible-inventory -i inventory --graph

또는

ansible-inventory -i ~/ansible-spec/inventory --graph
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
@all:
  |--@blog:
  |  |--@b-db:
  |  |  |--bdb-245
  |  |  |--bdb-246
  |  |--@b-web:
  |  |  |--admin-249
  |  |  |--admin-250
  |  |  |--api-247
  |  |  |--api-248
  |  |  |--web-245
  |  |  |--web-246
  |--@site-a:
  |  |--@development:
  |  |  |--devweb-214
  |  |--@production:
  |  |  |--websrv-231
  |  |  |--websrv-232
  |  |--@stage:
  |  |  |--stgweb-240
  |--@ungrouped:
  |  |--localhost

 

참고URL

- 변군이글루 블로그 : Ansible 구성 설정(Configuration Settings) 파일의 우선 순위에 대한 설명

- 변군이글루 블로그 : ansible inventory(인벤토리) 설정