본문 바로가기

리눅스

ansible inventory(인벤토리) 설정

반응형

Ansible inventory 설정

 Ansible은 인벤토리로 알려진 목록 또는 목록 그룹을 사용하여 인프라의 여러 관리 노드 또는 "호스트"에 대해 동시에 작동합니다. 인벤토리가 정의되면 패턴을 사용하여 Ansible을 실행할 호스트 또는 그룹을 선택합니다.(대상 서버 리스트)

구성 설정(Configuration settings)

  • ANSIBLE_CONFIG (환경 변수에 지정한 경우)
  • ansible.cfg (현재 디렉토리)
  • ~/.ansible.cfg (홈 디렉토리)
  • /etc/ansible/ansible.cfg (기본)
vim ~/.ansible.cfg
cat ~/.ansible.cfg
[defaults]
inventory      = ~/inventory/hosts.ini
host_key_checking = False

기본 Inventory : /etc/ansible/hosts

vim inventory.ini
$ cat inventory.ini
[all:vars]
ansible_connection=ssh
ansible_port=22
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=~/aws-key/keyfile.pem
#ansible_python_interpreter=/usr/bin/python

[all]
localhost       ansible_host=127.0.0.1          ip=127.0.0.1
web1            ansible_host=192.168.0.51       ip=192.168.0.51
web2            ansible_host=192.168.0.52       ip=192.168.0.52
web3            ansible_host=192.168.0.53       ip=192.168.0.53
web4            ansible_host=192.168.0.54       ip=192.168.0.54
web5            ansible_host=192.168.0.55       ip=192.168.0.55
db1             ansible_host=192.168.0.201      ip=192.168.0.201
db2             ansible_host=192.168.0.202      ip=192.168.0.202
api1            ansible_host=192.168.0.61       ip=192.168.0.61
api2            ansible_host=192.168.0.62       ip=192.168.0.62
redis1          ansible_host=192.168.0.211      ip=192.168.0.211
redis2          ansible_host=192.168.0.212      ip=192.168.0.212
redis3          ansible_host=192.168.0.213      ip=192.168.0.213
was1            ansible_host=192.168.0.81       ip=192.168.0.81
was2            ansible_host=192.168.0.82       ip=192.168.0.82
was3            ansible_host=192.168.0.83       ip=192.168.0.83
was4            ansible_host=192.168.0.84       ip=192.168.0.84
was5            ansible_host=192.168.0.85       ip=192.168.0.85

[local]
localhost

[webgroups:children]
web
was
api

[dbgroups:children]
db
redis

[web]
web[1:5]

[was]
was[1:5]

[db]
db1
db2

[api]
api1
api2

[redis]
redis1
redis2
redis3

인벤토리 매개 변수(inventory parameters)

  • ansible_connection : 연결 유형(default : smart)

[일반]

  • ansible_host : 연결하려는 호스트의 이름
  • ansible_port : 연결하려는 호스트의 포트(default : 22)
  • ansible_user : 연결하려는 호스트의 사용자 이름

[SSH 연결]

  • ansible_ssh_user : ssh에서 사용하는 사용자 이름
  • ansible_ssh_private_key_file : ssh에서 사용하는 개인 키 파일
ansible-inventory -i inventory.ini --graph
$ ansible-inventory -i inventory.ini --graph
@all:
  |--@dbgroups:
  |  |--@db:
  |  |  |--db1
  |  |  |--db2
  |  |--@redis:
  |  |  |--redis1
  |  |  |--redis2
  |  |  |--redis3
  |--@local:
  |  |--localhost
  |--@ungrouped:
  |--@webgroups:
  |  |--@api:
  |  |  |--api1
  |  |  |--api2
  |  |--@was:
  |  |  |--was1
  |  |  |--was2
  |  |  |--was3
  |  |  |--was4
  |  |  |--was5
  |  |--@web:
  |  |  |--web1
  |  |  |--web2
  |  |  |--web3
  |  |  |--web4
  |  |  |--web5
ansible-inventory -i inventory.ini --list
$ ansible-inventory -i inventory.ini --list
{
    "_meta": {
        "hostvars": {
            "api1": {
                "ansible_connection": "ssh", 
                "ansible_host": "192.168.0.61", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "192.168.0.61"
            }, 
            "localhost": {
                "ansible_connection": "ssh", 
                "ansible_host": "127.0.0.1", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "127.0.0.1"
            }, 
...
            "web5": {
                "ansible_connection": "ssh", 
                "ansible_host": "192.168.0.55", 
                "ansible_port": 22, 
                "ansible_ssh_private_key_file": "~/aws-key/keyfile.pem", 
                "ansible_ssh_user": "ec2-user", 
                "ip": "192.168.0.55"
            }
        }
    }, 
    "all": {
        "children": [
            "dbgroups", 
            "local", 
            "ungrouped", 
            "webgroups"
        ]
    }, 
    "api": {
        "hosts": [
            "api1", 
            "api2"
        ]
    }, 
... 
    "web": {
        "hosts": [
            "web1", 
            "web2", 
            "web3", 
            "web4", 
            "web5"
        ]
    }, 
    "webgroups": {
        "children": [
            "api", 
            "was", 
            "web"
        ]
    }
}

 

ansible-inventory -i ./inventory --host localhost
$ ansible-inventory -i ./inventory --host localhost
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
{
    "ansible_connection": "ssh", 
    "ansible_hostr": "127.0.0.1", 
    "ansible_port": 22, 
    "ansible_ssh_user": "ec2-user", 
    "ip": "127.0.0.1"
}

앤서블 인벤토리(디렉터리) YAML 구현

YAML INI
$ tree blog-scbyun.com
blog-scbyun.com
├── inventory.yaml
├── kube_node
│   └── hosts.yaml
└── nfs_server
    └── hosts.yaml
-
$ cat blog-scbyun.com/inventory.yaml
all:
  calico_rr:
  children:
    etcd:
      hosts:
        kube-control1:
    k8s_cluster:
      children:
        kube_control_plane:
          hosts:
            kube-control1:
        kube_node:
        calico_rr:
$ cat inventory.ini
[all]

[kube_control_plane]
kube-control1

[etcd]
kube-control1

[kube_node]
kube-node1
kube-node2
kube-node3
kube-node4
kube-node5
kube-node6

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

[nfs_server]
kube-nfs1
$ cat blog-scbyun.com/kube_node/hosts.yaml
kube_node:
  hosts:
    kube-node1:
    kube-node2:
    kube-node3:
    kube-node4:
    kube-node5:
    kube-node6:
-
$ cat blog-scbyun.com/nfs_server/hosts.yaml
nfs_server:
  hosts:
    kube-nfs1:
-
$ ansible-inventory -i blog-scbyun.com --graph
@all:
  |--@etcd:
  |  |--kube-control1
  |--@k8s_cluster:
  |  |--@calico_rr:
  |  |--@kube_control_plane:
  |  |  |--kube-control1
  |  |--@kube_node:
  |  |  |--kube-node1
  |  |  |--kube-node2
  |  |  |--kube-node3
  |  |  |--kube-node4
  |  |  |--kube-node5
  |  |  |--kube-node6
  |--@nfs_server:
  |  |--kube-nfs1
  |--@ungrouped:
$ ansible-inventory -i inventory.ini --graph
@all:
  |--@etcd:
  |  |--kube-control1
  |--@k8s_cluster:
  |  |--@calico_rr:
  |  |--@kube_control_plane:
  |  |  |--kube-control1
  |  |--@kube_node:
  |  |  |--kube-node1
  |  |  |--kube-node2
  |  |  |--kube-node3
  |  |  |--kube-node4
  |  |  |--kube-node5
  |  |  |--kube-node6
  |--@nfs_server:
  |  |--kube-nfs1
  |--@ungrouped:

 

참고URL

https://docs.ansible.com/ansible/2.9/user_guide/intro_inventory.html#id17

 

728x90
반응형