Doctor Pepper

[Ansible] 대상 호스트 선정 본문

Network 심화/Ansible

[Ansible] 대상 호스트 선정

Doctor Pepper 2024. 12. 22. 17:08
728x90

 

 

1. 인벤토리 파일를 이용한 자동화 대상 호스트 설정

앤서블(Ansible)은 인벤토리 파일을 사용하여 관리할 자동화 대상 호스트를 설정한다. 이 파일은 텍스트 파일로 작성되며, 다양한 형식(INI 스타일, YAML 등)을 지원한다.

 

  • INI 스타일 인벤토리 파일 :  INI 스타일로 작성된 기본 인벤토리 파일은 관리 호스트의 호스트명 또는 IP 주소를 한 줄씩 나열한다.
web1.example.com
web2.example.com
db1.example.com
db2.example.com
192.0.2.42

 

- 인벤토리 파일 생성 방법

  • IP를 이용한 인벤토리 파일 생성 : 자동화 대상 호스트로 인벤토리 파일을 생성해야 한다.
[root@ansible-server ~] # mkdir my-ansible
[root@ansible-server ~] # cd my-ansible
[root@ansible-server my-ansible] # vi inventory
192.168.100.5
192.168.100.6
192.168.100.7

 

  • 호스트명을 이용한 인벤토리 파일 생성 : 호스트명을 사용하려면 앤서블이 호스트명을 인식할 수 있도록 /etc/hosts 파일을 수정해야 한다.
[root@ansible-server my-ansible] # vi /etc/hosts
...
192.168.100.5  tnode1-centos.exp.com
192.168.100.6  tnode2-ubuntu.exp.com
192.168.100.7  tnode3-ubuntu.exp.com
[root@ansible-server my-ansible] # vi inventory
tnode1-centos.exp.com
tnode2-ubuntu.exp.com
tnode3-ubuntu.exp.com

 

2. 역할에 따른 호스트 그룹 설정

앤서블의 인벤토리 파일을 사용하면 호스트를 그룹화하여 특정 역할에 따라 작업을 효율적으로 처리할 수 있다.

  • 그룹별 호스트 설정 : 호스트 그룹은 대괄호([]) 안에 그룹명을 작성하고, 그룹에 속하는 호스트명이나 IP 주소를 나열한다.
[webservers]
web1.example.com
web2.example.com
192.0.2.42

[db-servers]
db01.example.com
db02.example.com

 

  • 중첩 그룹 정의 : 중첩 그룹은 기존 호스트 그룹을 포함하는 새로운 그룹을 정의할 수 있다. 중첩 그룹은 그룹명 뒤에 :children 접미사를 추가하여 작성한다.
[webservers]
web1.example.com
web2.example.com

[db-servers]
db01.example.com
db02.example.com

[datacenter:children]
webservers
dbservers

 

  • 범위를 사용한 호스트 설정 간소화 : 호스트 이름이나 IP 주소 범위를 사용하면 인벤토리 파일을 간결하게 작성할 수 있다. 범위는 대괄호([]) 안에 시작과 종료 구문을 지정하여 나타낸다.
[webservers]
web[1:2].example.com

[db-servers]
db[01:02].example.com
# IP 범위 설정
[defaults]
192.168.4.[0:255]

# 호스트명 범위 설정
[compute]
com[01:20].example.com

# DNS 범위 설정
[dns]
[a:c].dns.example.com

# IPv6 범위 설정
[ipv6]
2001:db8::[a:f]

 

 

728x90