컬쥐네 다락방

Ansible | 인벤토리 본문

클라우드/Ansible

Ansible | 인벤토리

코딩하는 갱얼쥐 2022. 4. 14. 02:18

인벤토리

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#intro-inventory

기본 인벤토리 파일 : /etc/ansible/hosts

  • 일반적으로 사용하지 않는다.

기본 위치에 있는 인벤토리 파일이 아닌 -i 옵션을 사용

포맷 : ini ,yaml

ini 형식의 예

key = value

[Seection]
key=value
  • Inventory basics:
mail.example.com    

[webservers]  
for.example.com  
bar.example.com

[dbervers]  
one.example.com  
two.example.com  
three.example.com

[] : 인벤토리 그룹

하나의 노드는 여러 그룹에 속할 수 있다.

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

기본 그룹

  • all
  • ungrouped

인벤토리 생성 원칙 : 간결하게 작성할 것

그룹에 호스트를 분류한다

  • what
  • where
  • when
[webservers].[01:50].example.com
192.168.100.[10:19]

인벤토리의 구조를 보는 명령어
--list --graph

[vagrant@controller ~]$ ansible-inventory -i a.ini --list  
{  
    "_meta": {  
        "hostvars": {}  
    },  
    "abc": {  
        "hosts": [  
            "b",  
            "c"  
        ]  
    },  
    "all": {  
        "children": [  
            "alpha",  
            "ungrouped"  
        ]  
    },  
    "alpha": {  
        "children": [  
            "abc",  
            "xyz"  
        ]  
    },  
    "ungrouped": {  
        "hosts": [  
            "a"  
        ]  
    },  
    "xyz": {  
        "hosts": [  
            "x",  
            "y",  
            "z"  
        ]  
    }  
}
[vagrant@controller ~]$ cat a.ini  
a

[abc]  
b  
c

[xyz]  
x  
y  
z

[alpha:children]  
abc  
xyz  
[vagrant@controller ~]$ ansible-inventory -i a.ini --graph  
@all:  
|--@alpha:  
| |--@abc:  
| | |--b  
| | |--c  
| |--@xyz:  
| | |--x  
| | |--y  
| | |--z  
|--@ungrouped:  
| |--a

호스의 범위

[webservers]
www[01:50].example.com
192.168.100.[10:19]

인벤토리 변수

[webservers]
www[01:50].example.com A=100 B=200
192.168.100.[10:19]

인벤토리 그룹 변수

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

중첩 그룹

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[usa:children]
southeast

인벤토리 파일 확인

생성한 인벤토리 파일 계층 구조 확인

ansible-inventory -i <INVENTORY_FILE> --graph

JSON 형식 및 호스트/그룹 변수

ansible-inventory -i <INVENTORY_FILE> --list

호스트에 설정된 변수 확인

ansible-inventory -i <INVENTORY_FILE> --host <HOST>

호스트 매칭 확인

ansible <HOST_PATTERN> -i <INVENTORY_FILE> --list-hosts

'클라우드 > Ansible' 카테고리의 다른 글

Ansible | Playbook  (0) 2022.04.18
Ansible | Ad_hoc  (0) 2022.04.18
Ansible | 구성 파일 및 권한 설정  (0) 2022.04.14
Ansible | SSH  (0) 2022.04.12
Ansible | 기초 개념  (0) 2022.04.12
Comments