본문 바로가기

Engineering/Ansible

[Ansible] 2. Ansible Inventory File

 

Inventory

앤서블이 자동화 대상으로 하는 관리 노드를 지정한다. 'ini' 혹은 'yaml'을 포함한 다양한 파일 형식을 호환한다.

IP나 호스트명을 선언하여 지정할 수 있고, 그룹으로 묶어 정의할 수도 있다. 이때 선언하는 호스트 명은 /etc/hosts 파일에 정의된 내용에 의존된다.

 

 

Ansible Inventory directory 생성

$ mkdir -p /home/ansible/inventory

Inventory 작성 방법

 

 

IP를 이용한 Inventory 파일 작성

$ vi /home/ansible/inventory/inventory.ini
172.16.22.131
172.16.22.132

위와 같이 IP 기반의 대상 호스트 목록을 작성할 수 있다.


호스트 명을 이용한 Inventory 파일 작성

 

# /etc/hosts 파일에 host define
$ vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6


172.16.22.130  node1
172.16.22.131  node2

#inventory 파일에 호스트네임 선언
$ vi /home/ansible/inventory/inventory.ini
node1
node2

위에서 언급했듯, 인벤토리 파일에 선언되는 호스트 명은 '/etc/hosts' 파일에 정의된 내용에 의존되기 때문에, 호스트 명을 이용하여 인벤토리 파일을 작성하기 전에 해당 파일에 호스트 명을 정의 후, 선언한다.

 


그룹 설정

#/etc/hosts 파일에 호스트 네임이 정의되어 있다고 가정.
[web-server]
web1.ex.com
web2.ex.com

[db-server]
db1.ex.com
db2.ex.com


#중첩 그룹설정
[servers:children]
web-server
db-server

 

위 예시와 같이 호스트들을 그룹으로 묶어 관리할 수 있다. 마지막 예시와 같이 그룹들을 그룹으로 묶어 중첩 그룹 설정도 가능하다.


범위를 사용한 호스트 사양 간소화

'[start:end]' 형식을 이용하여 숫자 혹은 영문자로 범위를 지정한 인벤토리를 작성할 수 있다.

#172.16.0.0 ~ 172.16.0.255의 범위
[IPv4]
172.16.0.[0:255]

#2001:db8::a ~ 2001:db8::f의 범위
[IPv6]
2001:db8::[a:f]

#a.dns.com, b.dns.com, c.dns.com
[dns]
[a:c].dns.com

 


 

인벤토리 확인

 

ansible-inventory 명령어를 이용하여 특정 인벤토리를 json 형태로 확인할 수 있다.

#list 확인
[root@Ansible-Controller inventory]# ansible-inventory -i /home/ansible/inventory/inventory.ini --list
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
{
    "Ansible-node1": {
        "hosts": [
            "node1"
        ]
    },
    "Ansible-node2": {
        "hosts": [
            "node2"
        ]
    },
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped",
            "node-group"
        ]
    },
    "node-group": {
        "children": [
            "Ansible-node1",
            "Ansible-node2"
        ]
    }
}

#트리 형태로 확인
[root@Ansible-Controller inventory]# ansible-inventory -i /home/ansible/inventory/inventory.ini --graph
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
@all:
  |--@ungrouped:
  |--@node-group:
  |  |--@Ansible-node1:
  |  |  |--node1
  |  |--@Ansible-node2:
  |  |  |--node2

 

 

'Engineering > Ansible' 카테고리의 다른 글

[Ansible] 6. Ansible Vault  (0) 2024.08.29
[Ansible] 5. 변수  (1) 2024.08.28
[Ansible] 4. Playbook 살펴보기  (0) 2024.08.28
[Ansible] 3. Ansible.cfg, Generating SSH key  (0) 2024.08.26
[Ansible] 1. Ansible 시작하기  (0) 2024.08.20