본문 바로가기

리눅스

[draft] Ansible을 초기 구성하는 방법

Ansible을 초기 구성하는 방법

테스트 환경

Node 호스트 이름 아이피 주소 비고
Control node node-01 192.168.0.51  
Managed node node-02 192.168.0.52  

1. SSH Key 생성 (Control Node)

Control Node에서 SSH 키를 생성합니다.

ssh-keygen -t rsa -C "vagrant@ControlNode"
  • 기본 경로: ~/.ssh/id_rsa

2. SSH Key 배포 (무비밀번호 접속 설정)

Control Node에서 자기 자신과 Managed Node 모두에 키를 배포합니다.

ssh-copy-id vagrant@192.168.0.51
ssh-copy-id vagrant@192.168.0.52
  • Control Node 자체도 Ansible 관리 대상이 될 수 있음
  • 로컬 테스트 및 공통 설정에 유리

3. Ansible 설정 파일(ansible.cfg) 구성

Ansible 구성 설정 - https://docs.ansible.com/ansible/latest/reference_appendices/config.html

 

기본 설정 파일 생성

  • Ansible 기본 설정 템플릿을 생성합니다.
ansible-config init --disabled > .ansible.cfg
--disabled 옵션은 모든 설정을 주석 처리한 기본 샘플을 생성합니다.

ansible.cfg 편집

vim ~/.ansible.cfg
[defaults]
inventory=~/inventory/hosts.ini
host_key_checking=False

4. Inventory 구성

인벤토리 구축 방법 - https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inheriting-variable-values-group-variables-for-groups-of-groups

 

인벤토리 디렉터리 생성

mkdir ~/inventory

hosts.ini 작성

vim ~/inventory/hosts.ini
[all:vars]
ansible_connection=ssh
ansible_port=22
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=~/.ssh/id_rsa

[all]
node-01	ansible_host=127.0.0.1		ip=127.0.0.1
node-02	ansible_host=192.168.0.52	ip=192.168.0.52

구성 포인트

  • [all:vars] → 모든 호스트에 공통으로 적용되는 변수
  • ansible_host → 실제 접속 대상 IP
  • ip → 플레이북 내에서 사용할 사용자 정의 변수

5. 인벤토리 구조 확인

ansible-inventory -i inventory/hosts.ini --graph
@all:
  |--@ungrouped:
  |  |--node-01
  |  |--node-02

6. Ansible 버전 및 설정 파일 적용 확인

ansible --version
ansible [core 2.14.4]
  config file = /home/vagrant/.ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
  • config file 경로가 ~/.ansible.cfg 인지 반드시 확인
  • 다른 ansible.cfg가 적용되면 설정이 무시될 수 있음

7. Ansible Ad-hoc 명령어로 연결 테스트

전체 노드 ping 테스트

ansible all -m ping
$ ansible all -m ping
node-02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
node-01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

특정 노드 ping 테스트

ansible node-02 -m ping
$ ansible node-02  -m ping
node-02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
ping 모듈은 ICMP ping이 아니라 Ansible 연결 테스트용 모듈입니다.