본문 바로가기

카테고리 없음

Ansible 스터디 4주차 - 모니터링 자동화

Ansible facts를 이용한 모니터링.

 

/home/ansible/ansible-project/20240206/1/ansible.cfg

[defaults]
inventory = ./inventory
remote_user = ansible
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

 

ansible 유저로 접속하여 Sudo로 명령하도록 설정. inventory 위치설정

 

/home/ansible/ansible-project/20240206/1/inventory

[compute]
node01 ansible_host=192.168.122.231
node02 ansible_host=192.168.122.132

[control]
node03 ansible_host=192.168.122.245


[db]
node04 ansible_host=192.168.122.63

[all:children]
compute
control
db

[all:vars]
host_key_checking = False

 

관리서버 설정.

 

---

- hosts: compute
  vars:
    log_directory: /var/log/daily_check

  tasks:
    - name: Print system info
      ansible.builtin.debug:
        msg:
        - "################ Start #####################"
        - "Date: {{ ansible_facts.date_time.date }} {{ ansible_facts.date_time.time }}" 
        - "HostName: {{ ansible_facts.hostname }}"
        - "OS: {{ ansible_facts.distribution }}"
        - "OS Version: {{ ansible_facts.distribution_version }}"
        - "OS Kernel: {{ ansible_facts.kernel }}"
        - "CPU Cores: {{ ansible_facts.processor_vcpus }}"
        - "Memory: {{ ansible_facts.memory_mb.real }}"
        - "Interfaces: {{ ansible_facts.interfaces }}"
        - "IPv4: {{ ansible_facts.all_ipv4_addresses }}"
        - "Devices: {{ ansible_facts.mounts }}"
        - "################# End #######################" 
      register: result

    - name: Create log directory
      ansible.builtin.file:
        path: "{{ log_directory }}"
        state: directory

    - name: Print logs to log file
      ansible.builtin.shell: |
        echo "{{ item }}" >> "{{ log_directory }}"/system_info.logs
      loop: "{{ result.msg }}"

 

debug 모듈을 통해 ansible facts내용 출력. ansible facts가 제공하는 정보를 통하여 관리서버의 여러 정보들을 result.msg 에 저장.

result.msg의 내용을 각 관리노드 /var/log/daily_check/system_info.logs 에 저장함.

 

 

플레이북 파일 문법체크.

 

 

엔서블 플레이북 실행 

 

 

파일 생성확인

 

 

파일 내용확인.

 

Ansible Facts가 제공되지 않는 정보 모니터링.

 

 

iostat, vmstat 등 더 자세한 정보 수집을 위해 shell module을 사용하여 명령어 실행.

 

 

/home/ansible/ansible-project/20240206/1/vars_packages.yml

 

---

log_directory: /home/ansible/logs
packages:
  - dstat
  - sysstat

log를 저장할 폴더와 설치를 원하는 패키지를 변수 선언 

 

 

/home/ansible/ansible-project/20240206/1/monitoring_system.yml

 

---

- hosts: all
  vars_files: vars_packages.yml

  tasks:
    - name: Install packages on RedHat
      ansible.builtin.yum:
        name: "{{ item }}"
        state: present
      loop: "{{ packages }}"
      when: ansible_facts.os_family == "RedHat"

    - name: Install packages on Ubuntu
      ansible.builtin.apt:
        name: "{{ item }}"
        state: present
      loop: "{{ packages }}"
      when: ansible_facts.os_family == "Debian"

    - name: Create log directory
      ansible.builtin.file:
        path: "{{ log_directory }}"
        state: directory

    - name: Monitoring dstat
      ansible.builtin.shell: |
        {{ item }} >> {{ log_directory }}/dstat.log
      loop:
        - dstat 2 10
        - dstat -cmdlt -D vda 2 10

    - name: Monitoring iostat
      ansible.builtin.shell: |
        {{ item }} >> {{ log_directory }}/iostat.log
      loop:
        - iostat
        - echo "==============="
        - iostat -t -c -d -p vda
       - echo "==============="

    - name: Monitoring vmstat
      ansible.builtin.shell: |
        {{ item }} >> {{ log_directory }}/vmstat.log
      loop:
        - vmstat
        - echo "==============="
        - vmstat -dt
        - echo "==============="
        - vmstat -D
        - echo "==============="

    - name: Monitoring df
      ansible.builtin.shell: |
        df -h >> {{ log_directory }}/df.log

 

 

vars_package.yml 에서 정의한 패키지명을 불러와 패키지 설치

directory없으면 생성 뒤, loop에서 정의한 명령 하나씩 순차적으로 실행 뒤 log파일에 저장.

 

 

문법 체크후

 

 

플레이북 실행

 

 

 

 

log파일 확인.