-
[Ansible 101] 호스트명 설정하기IT/Ansible 2024. 2. 4. 05:54728x90
앤서블로 접근하기 위한 대상 서버(tnode1~3)들은 이미 제어 노드(server)의 인벤토리에 등록되어 있습니다.
- ansible.builtin.hostname 모듈 : 호스트명 설정에 사용합니다.
- ansible.builtin.lineinfile 모듈 : 호스트명을 hosts 파일에 추가할 때 사용
- /etc/hosts 에 tnode 정보들을 등록하기 위해 필요한 정보들을 변수로 정의합니다.
1) 프로젝트 디렉터리 생성 및 ansible.cfg, inventory 파일 작성
chapter_10.2/..
# mkdir ~/ansible-project/chapter_10.2 cd ~/ansible-project/chapter_10.2 # ansible.cfg, inventory 파일 작성 cat <<EOT> ansible.cfg [defaults] inventory = ./inventory remote_user = ansible ask_pass = false inject_facts_as_vars = false roles_path = ./roles [privilege_escalation] become = true become_method = sudo become_user = root become_ask_pass = false EOT cat <<EOT> inventory [tnode] tnode1 tnode2 tnode3 EOT
2) 변수 정의 파일 생성
touch ~/ansible-project/chapter_10.2/vars_hosts_info.yml
chapter_10.2/vars_hosts_info.yml
tnodes: - hostname: tnode1 fqdn: tnode1.local net_ip: 10.10.1.11 - hostname: tnode2 fqdn: tnode2.local net_ip: 10.10.1.12 - hostname: tnode3 fqdn: tnode3.local net_ip: 10.10.1.13
3) 메인 플레이북 작성 :
hostname은 inventory 정보를 통해서 설정,
/etc/hosts 파일은 내용 추가는 변수 정의 파일에서 반복문을 통해 가져온다.
regexp는 대체(기본값 수정, state=present)하거나 혹은 삭제(state=absent).
touch ~/ansible-project/chapter_10.2/set_hostname.yml
chapter_10.2/set_hostname.yml
--- - hosts: tnode tasks: - name: Set hostname from inventory ansible.builtin.hostname: name: "{{ inventory_hostname }}" - hosts: all vars_files: vars_hosts_info.yml tasks: - name: Add host ip to hosts ansible.builtin.lineinfile: path: /etc/hosts line: "{{ item.net_ip }} {{ item.hostname }} {{ item.fqdn }}" regexp: "^{{ item.net_ip }}" loop: "{{ tnodes }}"
4) 플레이북 실행
- 실행 전
ansible -m shell -a "cat /etc/hosts" tnode
-실행 후
# 문법 체크 ansible-playbook --syntax-check set_hostname.yml # 플레이북 실행 ansible-playbook set_hostname.yml ... # 확인 ansible -m shell -a "hostname" tnode ansible -m shell -a "cat /etc/hosts | grep tnode" tnode # tnode1에서 다른 노드 통신 확인 ssh tnode1 ping -c 1 tnode2 ssh tnode1 ping -c 1 tnode3.local
728x90반응형'IT > Ansible' 카테고리의 다른 글
[Ansible 101] 보안 설정 (7) 2024.02.12 [Ansible 101] 환경 설정 자동화 (0) 2024.02.04 [Ansible 101] NTP 서버 설치 및 설정하기 (0) 2024.02.04 [Ansible 101] SSH 키 생성 및 복사하기 (0) 2024.02.04 [Ansible 101] 사용자 계정 생성하기 (0) 2024.02.04