본문 바로가기

A101[1기]

Ansible 스터디 3주차 - 환경 설정 자동화

 

hostname 모듈을 활용한 hostname 설정

 

ansible inventory 및 설정파일 

 

/home/ansible/ansible-project/4/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

 

/home/ansible/ansible-project/4/inventory

 

[compute]
com01 ansible_host=192.168.122.231
com02 ansible_host=192.168.122.132

[control]
192.168.122.245


[db]
db01 ansible_host=192.168.122.63

 

변수파일 정의

 

/home/ansible/ansible-project/4/vars_hosts_info.yml

nodes:
  - hostname: com01
    fqdn: compute01.local
    net_ip: 192.168.122.231
  - hostname: com02
    fqdn: compute02.local
    net_ip: 192.168.122.132
  - hostname: ctrl01
    fqdn: control01.local
    net_ip: 192.168.122.245
  - hostname: db01
    fqdn: database01.local
    net_ip: 192.168.122.63

 

해당 내용으로 /etc/hosts에 박아넣을것임.

 

 

/home/ansible/ansible-project/4/set_hostname.yml

 

---
- hosts: all
  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: "{{ nodes }}"

 

정규 표현식으로 IP가 있을경우, 대체하여 /etc/hosts에 추가.

매칭되는 내용이 없으면 line의 내용으로 추가.

 

 

플레이북 실행

 

 

결과 확인

 

 

inventory에서 ansible_hosts를 이용하지 않고 그대로 Ip만 박아넣어 사용한것만 맨 앞자리로 Hostname이 설정되고, 나머지는 정상적으로 hostname이 적용, /etc/hosts에 ip들이 들어가 있는것을 확인 할 수 있음.

 

 

또한 정상적으로 hostname으로 Ping이 나가는것을 확인 할 수 있음.

 

 

Docker 설치

 

 

ansible.cfg/inventory는 이전에 사용했던 설정 재사용.

 

Ansible Galaxy에서 role 다운로드

 

ansible-galaxy role install -p ./roles geerlingguy.docker

 

 

install 확인 파일 구조 확인

 

 

테스크 정보 확인.

 

 

Document확인

 

 

/home/ansible/ansible-project/7/install_docker.yml

---
- hosts: compute
  roles:
    - role: geerlingguy.docker

 

플레이북 실행

ansible@server:~/ansible-project/7$ ansible-playbook -i inventory install_docker.yml

 

 

 

 

설치 확인

 

 

 

 

 

다른것과 마찬가지로 Ansible-galaxy로 남들이 짜준 role을 사용해도, Document를 제대로 읽지 않고 혹은 이해하지 않고 돌리면 계속 삽질하게 됨. 직접 role을 짜는것 보다 느릴 수 있고 환경에 맞지 않을 수 있으니  SOP가 있다면 직접 짜도되고 상황에 따라 판단하자.