Invertory Basics
Ansible Inventory는 관리할 시스템 및 장비 목록을 정의한 파일입니다.
이 파일을 통해 Ansible은 특정 호스트에 작업을 실행할 수 있습니다.
기본적으로는 INI 또는 YAML 형식의 정적 파일을 사용하며, 동적 인벤토리도 지원합니다.
hosts format
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
hosts.yaml format
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
Default Groups
Ansible은 기본적으로 'all' 과 'ungrouped' 그룹이로 구분되며
all group은 hosts에 정의된 모든 호스트 (ex. two.example.com)이고
ungrouped는 그룹화되지 않은 호스트 (ex. mail.example.com)를 말합니다.
Parent Child Group
그룹 간에 상위, 하위 그룹을 나눌 수 있습니다.
상위그룹은 하위그룹을 포함하여 중복 선언을 줄입니다.
format INT
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
format YAML
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
children:
webservers:
east:
test:
children:
west:
Adding range of hosts
범위를 설정하여 패턴이 있는 이름을 가진 호스트들을 손쉽게 추가할 수 있습니다.
format INI
[webservers]
www[01:50].example.com
format YAML
# ...
webservers:
hosts:
www[01:50].example.com:
규칙에 의해 증가하는 시퀀스 숫자를 적용할 수 있습니다.
format INI
[webservers]
www[01:50:2].example.com
format YAML
# ...
webservers:
hosts:
www[01:50:2].example.com:
알파벳 범위도 가능합니다.
[databases]
db-[a:f].example.com
Group Variables
그룹내의 모든 호스트들이 공유할 수 있는 변수를 선언 할 수 있습니다.
format INI
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
format YAML
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
Organizing host and group variables
사용하는 변수를 그룹별, 호스트별로 구분하여 설정할 수 있습니다.
예를 들어 host 파일에 www-01-example-com ~ www-10-example-com
호스트들이 webserver group에 정의되어 있습니다.
/ansible/inventory/hosts
[webservers]
www-[01:10]-example-com
[prometheus]
www-[01:03]-example-com
prometheus group에만 추가되는 변수를 선언하고,
www-01-example-com 호스트에만 따로
grafana가 설치되어 관련 변수를 선언할 예정입니다.
/ansible/group_vars/prometheus
# prometheus
prometheusTarPackage: prometheus-2.32.1.linux-amd64.tar.gz
prometheusDir: /etc/lib/prometheus-2.32.1.linux-amd64
/ansible/host_vars/www-01.example-com
# grafana
smtp:
emailUser: mymail@dotent.com
emailPasswd: aabbcc123
www-01.example-com 호스트는 webservers, prometheus group_var의 변수와 함께
grafana 관련 변수도 사용할 수 있습니다.
각 파일의 경로는 아래와 같습니다.
/ansible/inventory/hosts
/ansible/group_vars/prometheus
/ansible/group_vars/webservers
/ansible/host_vars/www-01.example-com
참조 :
https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html
How to build your inventory — Ansible Community Documentation
Ansible Community Documentation Ansible How to build your inventory Ansible automates tasks on managed nodes or “hosts” in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ans
docs.ansible.com
'Infra > Ansible' 카테고리의 다른 글
Ansible - 조건적 실행 ( handler, when ) (0) | 2025.01.11 |
---|---|
Ansible - Playbook Roles 작성 (0) | 2023.03.27 |
Ansible - ssh-key를 통한 비밀번호 없이 ssh접속 (0) | 2023.03.26 |
Ansible - 설치 및 실행 (0) | 2023.03.26 |