본문 바로가기

리눅스

[리눅스] Ansible을 초기 구성하는 방법

728x90

Ansible을 초기 구성하는 방법

구성 환경

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

ssh keygen 생성

$ ssh-keygen -t rsa -C "vagrant@ControlNode"

ssh key 교환

ssh-copy-id vagrant@192.168.0.51
ssh-copy-id vagrant@192.168.0.52

ansible.cfg 구성

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

ansible.cfg 파일 생성하기

ansible-config init --disabled > .ansible.cfg

ansible.cfg 편집

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

Inventory 구성

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

Inventory 파일 편집

mkdir ~/inventory
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
ansible-inventory -i inventory/hosts.ini --graph
$ ansible-inventory -i inventory/hosts.ini --graph
@all:
  |--@ungrouped:
  |  |--node-01
  |  |--node-02

ansible 버전 정보 확인

  • config file 위치가 사용자의 홈 디렉터리 밑에 있는 .ansible.cfg 파일로 변경됨
$ 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

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"
}
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"
}

 

728x90