리눅스

[draft] Ansible inventory(인벤토리) 설정하는 방법

SangChul Dot Kr Blog 2026. 2. 28. 14:49

Ansible inventory(인벤토리) 설정하는 방법

1. Ansible Inventory란?

Ansible은 Inventory(인벤토리) 라고 불리는 호스트 목록(또는 그룹)을 기반으로 여러 서버(관리 대상 노드)에 대해 동시에 작업을 수행합니다.

  • Inventory는 대상 서버 리스트 역할을 수행
  • 그룹 및 패턴을 사용해 실행 대상 선택 가능
  • INI, YAML, 디렉터리 구조 등 다양한 방식 지원

2. Ansible 설정 파일 우선순위

  • ANSIBLE_CONFIG (환경 변수)
  • ansible.cfg (현재 디렉토리)
  • ~/.ansible.cfg (홈 디렉토리)
  • /etc/ansible/ansible.cfg (기본 설정)

3. 사용자 전용 ansible.cfg 설정

vim ~/.ansible.cfg
cat ~/.ansible.cfg
[defaults]
inventory      = ~/inventory/hosts.ini
host_key_checking = False
  • inventory : 기본 인벤토리 파일 경로
  • host_key_checking = False : SSH known_hosts 확인 비활성화 (자동화 환경에서 유용)

4. INI 기반 Inventory 구성 예제

  • 기본 Inventory 파일 위치 기본값: /etc/ansible/hosts
  • 사용자 지정 예제: inventory.ini
vim inventory.ini

공통 변수 정의 (all:vars)

[all:vars]
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

호스트 정의

[all]
localhost       ansible_host=127.0.0.1          ip=127.0.0.1
web1            ansible_host=192.168.0.51       ip=192.168.0.51
web2            ansible_host=192.168.0.52       ip=192.168.0.52
web3            ansible_host=192.168.0.53       ip=192.168.0.53
web4            ansible_host=192.168.0.54       ip=192.168.0.54
web5            ansible_host=192.168.0.55       ip=192.168.0.55
db1             ansible_host=192.168.0.201      ip=192.168.0.201
db2             ansible_host=192.168.0.202      ip=192.168.0.202
api1            ansible_host=192.168.0.61       ip=192.168.0.61
api2            ansible_host=192.168.0.62       ip=192.168.0.62
redis1          ansible_host=192.168.0.211      ip=192.168.0.211
redis2          ansible_host=192.168.0.212      ip=192.168.0.212
redis3          ansible_host=192.168.0.213      ip=192.168.0.213
was1            ansible_host=192.168.0.81       ip=192.168.0.81
was2            ansible_host=192.168.0.82       ip=192.168.0.82
was3            ansible_host=192.168.0.83       ip=192.168.0.83
was4            ansible_host=192.168.0.84       ip=192.168.0.84
was5            ansible_host=192.168.0.85       ip=192.168.0.85

5. 그룹 및 그룹 계층 구조

개별 그룹

[local]
localhost

[web]
web[1:5]

[was]
was[1:5]

[db]
db1
db2

[api]
api1
api2

[redis]
redis1
redis2
redis3

그룹의 그룹 (children)

[webgroups:children]
web
was
api

[dbgroups:children]
db
redis

6. Inventory 매개변수 정리(inventory parameters)

  • ansible_connection : 연결 유형(default : smart)

[일반 매개변수]

  • ansible_host : 실제 접속할 호스트 주소
  • ansible_port : SSH 포트(default : 22)
  • ansible_user : 접속 사용자

[SSH 전용 매개변수]

  • ansible_ssh_user : SSH 사용자
  • ansible_ssh_private_key_file : SSH 개인 키

7. Inventory 구조 검증

그룹 트리 확인

ansible-inventory -i inventory.ini --graph
$ ansible-inventory -i inventory.ini --graph
@all:
  |--@dbgroups:
  |  |--@db:
  |  |  |--db1
  |  |  |--db2
  |  |--@redis:
  |  |  |--redis1
  |  |  |--redis2
  |  |  |--redis3
  |--@local:
  |  |--localhost
  |--@ungrouped:
  |--@webgroups:
  |  |--@api:
  |  |  |--api1
  |  |  |--api2
  |  |--@was:
  |  |  |--was1
  |  |  |--was2
  |  |  |--was3
  |  |  |--was4
  |  |  |--was5
  |  |--@web:
  |  |  |--web1
  |  |  |--web2
  |  |  |--web3
  |  |  |--web4
  |  |  |--web5

전체 Inventory JSON 출력

ansible-inventory -i inventory.ini --list
$ ansible-inventory -i inventory.ini --list
{
    "_meta": {
        "hostvars": {
            "api1": {
                "ansible_connection": "ssh", 
                "ansible_host": "192.168.0.61", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "192.168.0.61"
            }, 
            "localhost": {
                "ansible_connection": "ssh", 
                "ansible_host": "127.0.0.1", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "127.0.0.1"
            }, 
...
            "web5": {
                "ansible_connection": "ssh", 
                "ansible_host": "192.168.0.55", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "192.168.0.55"
            }
        }
    }, 
    "all": {
        "children": [
            "dbgroups", 
            "local", 
            "ungrouped", 
            "webgroups"
        ]
    }, 
    "api": {
        "hosts": [
            "api1", 
            "api2"
        ]
    }, 
... 
    "web": {
        "hosts": [
            "web1", 
            "web2", 
            "web3", 
            "web4", 
            "web5"
        ]
    }, 
    "webgroups": {
        "children": [
            "api", 
            "was", 
            "web"
        ]
    }
}

8. 디렉터리 기반 YAML Inventory 구성

대규모 환경에서는 디렉터리 Inventory 방식이 유용합니다.

 

디렉터리 구조 예제

$ tree blog-scbyun.com
blog-scbyun.com
├── api1
│   └── hosts.yaml
├── api2
│   └── hosts.yaml
├── db1
│   └── hosts.yaml
├── db2
│   └── hosts.yaml
├── inventory.yaml
├── web1
│   └── hosts.yaml
└── web2
    └── hosts.yaml

최상위 Inventory 정의

$ cat blog-scbyun.com/inventory.yaml
all:
  hosts:
    localhost:

개별 그룹 Inventory 예제

$ cat blog-scbyun.com/api1/hosts.yaml
api1:
  hosts:
    apiserver1:

디렉터리 Inventory 검증

$ ansible-inventory -i blog-scbyun.com --graph
@all:
  |--@api1:
  |  |--apiserver1
  |--@api2:
  |  |--apiserver2
  |--@db1:
  |  |--dbserver1
  |--@db2:
  |  |--dbserver2
  |--@ungrouped:
  |  |--localhost
  |--@web1:
  |  |--webserver1
  |--@web2:
  |  |--webserver2

 

참고URL

https://docs.ansible.com/ansible/2.9/user_guide/intro_inventory.html#id17