Ansible

[Ansible-playbook] 다른 서버들의 Log 압축 후 수집

밝은숲 2021. 4. 29. 14:37

 

목적

- myserver에서 forest-server1 ~ forest-server3에 있는 /var/log/forest 폴더를 압축하여 가져오기

 

 


방법 1. ansible

 

Archive 1

$ ansible -m archive -a "path=/var/log/forest dest=/tmp/forest.tar format=tar" -i /etc/inventory.ini forest-server

  • 모든 host의 파일명이 forest.tar로 동일하여 수집 시 구분이 어려움

Archive 2

$ ansible -m archive -a "path=/var/log/forest dest=/tmp/forest-log-{{inventory_hostname}}.tar format=tar" -i /etc/inventory.ini forest-server

  • 파일명에 hostname을 넣어 구분
  • 생성파일 : /tmp/forest-log-forest-server1.tar
  • ansible’s inventory_hostname is a built-in variable.

 

Copy 1

$ ansible -m copy -a "src=/tmp/forest-log-{{inventory_hostname}}.tar dest=/root/ansible_tmp/log/" -i /etc/inventory.ini forest-server

  • copy 모듈은 로컬에서 원격지로 파일을 복사할 경우에 사용
  • ansible’s inventory_hostname is a built-in variable.

 

Copy 2

$ ansible -m fetch -a "src=/tmp/forest-log-{{ansible_hostname}}.tar dest=/root/ansible_tmp/log/ flat=yes" -i /etc/inventory.ini forest-server

  • 원격지에서 로컬으로 파일을 가지고 올 경우 fetch 모듈 사용
  • 'flat=yes' 옵션이 없으면 호스트네임 폴더 아래 소스 파일의 폴더 구조까지 복사 됨.  /root/ansible_tmp/log/kolla-controller01/tmp/kolla-log-kolla-controller01.tar
  • 'flat=yes' 옵션을 넣으면 파일만 복사 됨. /root/ansible_tmp/log/kolla-log-kolla-controller01.tar

 

 

 

 


 

방법 2. ansible-playbook

 

여러 명령을 하나의 playbook으로 작성하여 한 번에 실행

 

(1) playbook 작성

log_archive_pull.yaml
--
- name: Ansible Log Archive
  hosts: forest-server
  tasks:
    - name: Archive forest Log!
      archive:
        path: /var/log/forest
        dest: /tmp/forest-log-{{ansible_hostname}}.tar
        format: tar
    - name: Get Log Files!
      fetch:
        src: /tmp/forest-log-{{ansible_hostname}}.tar
        dest: /root/ansible_tmp/log/
        flat: yes

 

(2) playbook 실행

# root@kolla-deploy:~/ansible_tmp# ansible-playbook log_archive_pull.yml -i /etc/inventory.ini