뭐라도 끄적이는 BLOG

Ansible Inventory 본문

Infra/Ansible

Ansible Inventory

Drawhale 2023. 7. 23. 09:12

Inventory

Ansible은 인프라에 존재하는 여러 호스트를 관리한다. 호스트의 목록 또는 그룹을 지정한 호스트를 관리할 수 있는 파일이 필요한데 이것이 Inventory이다. Inventory가 정의되면 패턴을 사용하여 Ansible을 실행할 노드 또는 그룹을 선택할 수 있다. 기본 인벤토리 파일은 `/etc/ansible/hosts`이며, `-i` 옵션을 사용하면 다른 인벤토리 파일을 지정할 수 있다.

Inventory 파일은 INI또는 YAML 형식이다. 아래 같은 설정을 다른 파일 형식으로 표기한 예시이다.

INI 형식 YAML 형식
mail.example.com

[webserver]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.exampe.com

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

Inventory의 우선순위

  1. ansible-playbook 또는 ad-hoc 명령어에 -i 옵션을 통해서 인벤토리 파일을 지정하는 경우
  2. ansible.cfg에 기입되어 있는 inventory 파일의 위치

Inventory 생성 및 테스트

  1. 아무 디렉토리를 생성한뒤 안에 inventory.yaml파일을 편집한다.
  2. 호스트에 대한 그룹을 추가 후, 관리되는 각 노드의 IP주소 또는 FQDN을 ansible_host필드에 지정한다.
virtualmachines:
  hosts:
    slave01:
      ansible_host: 192.168.56.202
    slave02:
      ansible_host: 192.168.56.203

인벤토리 설정을 확인한다.

ansible-inventory -i inventory.yaml --list

Ping을 테스트 해본다.

ansible-inventory -i inventory.yaml --list

왼쪽은 실패했을때 출력되는 에러 메세지이며 오른쪽은 성공했을때 콘솔 메세지이다. 왼쪽의 경우 SSH 공개키 교환을 하지 않아 일어난 에러이다.

기본 그룹

인벤토리 파일에 그룹을 정의하지 않더라고 Ansible은 all과 ungrouped 두 가지 기본 그룹을 생성한다.

  • all: 모든 호스트 포함
  • ungrouped: 그룹에 속하지 않은 모든 호스트 포함
all:
  hosts:
      mail.example.com:
  children:
    webservers:
      hosts:
        foo.exmaple.com:
        bar.example.com:
  dbservers:
    hosts:
      one.example.com:
      two.example.com:
      three.exampe.com:

모든 호스트는 항상 2개 이상의 그룹에 속하게 된다. 위 예시로 설명하자면 mail.example.com 호스트는 all 그룹과 ungrouped에 속하고 two.example.com 호스트는 all 그룹과 dbservers 그룹에 속한다. 

여러 그룹에 속한 호스트

각 호스트는 둘 이상의 그룹에 넣을 수 있다.

  • What - Application, stack, microservice(database servers, web servers)
  • Where - Local DNS, storage등과 데이터센터 또는 지역(동부, 서부)
  • When - production 리소스, testing 리소스
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:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    test:
      hosts:
        bar.example.com:
        three.example.com:

여러 그룹에 중첩되어 있는 호스트들을 확인할 수 있다.

parent/child 그룹 관계

INI에서는 :children을 이용해서 YAML은 children:을 이용해서 그룹간 부모/자식 관계를 만들 수 있다.

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:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      children:
        east:
    test:
      children:
        west:

child 그룹의 멤버인 host는 자동으로 parent그룹의 멤버가 된다. 그룹은 부모와 자식을 여러 개 가질 수 있지만 순환 관계가 되면 안된다. 호스트는 여러 그룹에 속할 수도 있지만 런타임에는 호스트의 인스턴스가 하나만 존재한다. Ansible은 여러 그룹의 데이터를 병합한다.

host 범위로 추가

비슷한 패턴을 가진 host가 많은경우 각 호스트 이름을 개별적으로 나열하는 대심 범위로 추가할 수 있다.

...
  webservers:
    hosts:
      www[01:50].example.com:

간격을 정할수도 있다.

...
  webservers:
    hosts:
      www[01:50:2].example.com:

알파벳의 범위를 지정할 수도 있다.

[databases]
db-[a:f].example.com
Passing multipl

인벤토리 확인

ansible또는 ansible-inventory명령으로 확인할 수 있다. 기본 인벤토리 파일이 아닌 경우 -i 또는 -inventory옵션을 사용할 수 있다.

ansible all --list-hosts
ansible webservers --list-hosts
ansible foo.example.com --list-hosts
ansible-inventory --list
ansible-inventory --host foo.example.com

참고 자료

 

Building an inventory — Ansible Documentation

Inventories organize managed nodes in centralized files that provide Ansible with system information and network locations. Using an inventory file, Ansible can manage a large number of hosts with a single command. Inventories also help you use Ansible mor

docs.ansible.com

 

How to build your inventory — Ansible Documentation

When declared inline with the host, INI values are interpreted as Python literal structures (strings, numbers, tuples, lists, dicts, booleans, None). Host lines accept multiple key=value parameters per line. Therefore they need a way to indicate that a spa

docs.ansible.com

 

반응형

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

Ansible 구성 파일  (0) 2023.07.02
Ansible 이란  (0) 2023.06.29