[Ansible] 05-2. 인벤토리 파일 작성 방법

Date:     Updated:

카테고리:

태그:

[Ansible] 05-2. 인벤토리 파일 작성 방법


🔔 자세한 모듈 내용은 아래 문서를 참고

모듈 내용 : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#modules

예제 Github : https://github.com/revenge1005/Ansible_study


🔔 인벤토리 내용 확인 명령어

$ ansible-inventory --list 


📜 인벤토리 파일 작성 방법

[app] # (1)
app1 ansible_host=some.app.host
app2 ansible_host=another.app.host

[db]
db1 ansible_host=db.1.host
db2 ansible_host=db.2.host

[app:vars] # (2)
admin_username=app_user
admin_uid=1001
admin_has_data=False # (3)

[db:vars]
admin_username=app_user
admin_uid=1002
admin_has_data=True

[korea] # (4)
app1

[usa] # 
구분 설명
(1) [..]으로 구성된 부분을 섹션이라 하며, [그룹 명] 형식으로 지정해 호스트를 특정 그룹에 할당
(2) [<그룹 이름="">:vars] 형식으로 그룹 변수 전용 섹션 사용 가능
(3) Boolean 값은 True 혹은 False 와 같이 대문자로 시작해야 함
(4) 호스트를 여러 그룹에 할당


📜 인벤토리 변수를 YAML 파일에 정의

  • 인벤토리 변수는 전용 YAML 파일에 정의 가능하며, 정한 변수를 정의하는 파일 규칙은 다음과 같다.

    • 호스트 변수를 정의하는 파일은 host_vars/<호스트 이름="">.YAML

    • 그룹 변수를 정의하는 파일은 group_vars/<그룹 이름="">.YAML

  • 인벤토리 변수 우선순위

    • (A) 플레이북에 있는 변수를 정의한 YAML 파일

    • (B) 인벤토리에 있는 변수를 정의한 YAML 파일

    • (C)인벤토리 파일에서 정의한 변수

./
├── hosts
├── group_vars
│   ├── app.yml
│   └── db.yml
└── host_vars
    ├── app1.yml
    └── app2.yml
$  cat group_vars/db.yml
---
admin_username: app_user
admin_uid: 1002
admin_has_data: True
# admin_has_data: TRUE
# admin_has_data: true
# admin_has_data: yes
$ host_vars/app1.yml
---
ansible_host: some.app.host


📜 기본값을 정의하는 그룹 변수 파일

  • all이라는 이름은 “인텐토리 내의 모든 호스트가 할당된 특별한 그룹 이름”

    • 기본값을 설정할 때 용이

    • group_vars/all.yml 과 같이 사용 가능

  • 우선순위가 가장 낮게 설정

    • all 그룹 변수 < 다른 그룹 변수 < 호스트 변수


📜 그룹의 그룹

  • :children 접미사를 사용하여 그룹의 하위 그룹을 만드는것도 가능하다.

  • 또한 이렇게 정의한 그룹에 :vars를 이용하여 변수를 지정해주는것 역시 가능하다.

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest


ANSIBLE 카테고리 내 다른 글 보러가기

댓글 남기기