Zabbix Agent 배포 ( Use. Ansible )
- -
나중에 서버가 여러 대가 되게 되면 하나하나 들어가서 Zabbix Agent를 설치하고 Config 파일을 수정하는 것은 힘들다고 생각된다. 이럴 때 Ansible을 이용하게 되면 여러 서버에 한 번에 Zabbix Agent를 설치하고 Config 파일을 수정하는 일이 가능하게 된다.
■ TEST 환경
Hostname | Private IP |
lee | 10.,0.3.34 |
mysql1 | 10.0.3.245 |
mysql2 | 10.0.3.175 |
- Zabbix Agent 5 Version
- Cent 7.9
■ Ansible 설치 & 간단 테스트
- Ansible 에서는 관리 대상의 서버 리스트를 inventory라고 부른다.
- inventory는 /etc/ansible/hosts 에 저장되며 서버 그룹과 호스트로 구성된다.
- host는 /etc/hosts 에 사전에 등록되어 있어야 이름을 통해 해당 IP로 찾아갈 수 있다.
- Ansible을 실행시킬때 관리 서버를 패스워드로 접속하는 방법과 공개키로 접속하는 방법이 있다.
- 공개키는 ssh-keygen을 이용하여 만들어준 후 ssh-copy-id root@[host명]을 이용하여 관리 서버로 공개키를 전달해주면 된다.
1. 설치
[root@lee-monitor ~]# yum -y install ansible
2. inventory 설정
[root@lee-monitor ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.3.16 lee-monitor.subnet06081035.goodusdatavcn.oraclevcn.com lee-monitor
10.0.3.34 lee
10.0.3.245 mysql1
10.0.3.175 mysql2
[root@lee-monitor ~]# vi /etc/ansible/hosts
[dbserver]
lee
mysql1
mysql2
3.Inventory 내 server 연결 테스트 ( PW로 하는 법과 공개키로 하는법있음 )
1) 패스워드
[root@lee-monitor .ssh]# ansible mysql1 -m ping -u root -k ## m은 모듈을 뜻한다.
SSH password:
mysql1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2) 공개키 이용
[root@lee-monitor .ssh]# ansible all -m ping -u lee -k
SSH password:
mysql2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
mysql1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
lee | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
*** Key 만드는법
[root@lee-monitor ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gnkm9gnKGoTjHAXaVN2vZE084DLZQaY+ZCeZzVyJ5mE root@lee-monitor
The key's randomart image is:
+---[RSA 2048]----+
| ..... o=+.. |
|.o. .%Eo= |
|. .. @+B= . |
|. . * =+ o |
|oo * *oS. |
|+o.o * +. |
|.oo o |
| o |
|. |
+----[SHA256]-----+
[lee@lee-monitor ~]$ ssh-copy-id lee@mysql1
[lee@lee-monitor ~]$ ssh-copy-id lee@mysql2
[lee@lee-monitor ~]$ ssh-copy-id lee@lee
■ Zabbix-Agent 배포
- get_url 모듈을 이용하여 Zabbix RPM을 인터넷에서 받도록 함
- Yum 모듈을 이용하여 RPM / Agent / Sender 설치
- lineinfile 모듈을 이용하여 Zabbix-Agent를 배포한 후 자동으로 Server에 추가되도록 HostMetadata / ServerActive를 수정
* Zabbix-Agent 자동 등록
2021.12.23 - [Open Source 툴] - Zabbix-agent Auto registration ( 자동 등록 )
- systemd 모듈을 사용하여 Zabbix-Agent Start
[root@lee-monitor zabbix]# cat lee.yml
---
- name: Install zabbix-agent-5
hosts: lee
remote_user: root
tasks:
- name: 1. Test Connection
ping:
- name: 2. Download zabbix-agent
get_url:
url: http://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
dest: /root/
- name: 3. Install Zabbix-agent
yum:
name:
- /root/zabbix-release-5.0-1.el7.noarch.rpm
- zabbix-agent
- zabbix-sender
- name: 4. Zabbix-Agent Config Update
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '{{item.regexp}}'
line: '{{item.line}}'
with_items:
- { regexp: '^Server=127.0.0.1', line: 'Server={{ ip }}' }
- { regexp: '^ServerActive=127.0.0.1', line: 'ServerActive={{ ip }}' }
- { regexp: '^Hostname=Zabbix server', line: '#Hostname=Zabbix server' }
- { regexp: '^#HostnameItem=system.hostname', line: 'HostnameItem=system.hostname' }
- { regexp: '^# HostMetadata=', line: 'HostMetadata={{ Hostmetdata }}' }
- name: 5. Zabbix-Agent Start
systemd:
state: started
name: zabbix-agent
[root@lee-monitor zabbix]# ansible-playbook lee.yml --extra-var "ip=10.0.3.16 Hostmetdata=LEE_TEST"
PLAY [Install zabbix-agent-5] *******************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [mysql1]
ok: [mysql2]
ok: [lee]
TASK [1. Test Connection] ***********************************************************************************************************
ok: [mysql1]
ok: [lee]
ok: [mysql2]
TASK [2. Download zabbix-agent] *****************************************************************************************************
changed: [mysql1]
changed: [mysql2]
ok: [lee]
TASK [3. Install Zabbix-agent] ******************************************************************************************************
changed: [mysql2]
changed: [mysql1]
changed: [lee]
TASK [4. Zabbix-Agent Config Update] ************************************************************************************************
changed: [mysql1] => (item={u'regexp': u'^Server=127.0.0.1', u'line': u'Server=10.0.3.16'})
changed: [mysql2] => (item={u'regexp': u'^Server=127.0.0.1', u'line': u'Server=10.0.3.16'})
changed: [lee] => (item={u'regexp': u'^Server=127.0.0.1', u'line': u'Server=10.0.3.16'})
changed: [mysql1] => (item={u'regexp': u'^ServerActive=127.0.0.1', u'line': u'ServerActive=10.0.3.16'})
changed: [mysql2] => (item={u'regexp': u'^ServerActive=127.0.0.1', u'line': u'ServerActive=10.0.3.16'})
changed: [lee] => (item={u'regexp': u'^ServerActive=127.0.0.1', u'line': u'ServerActive=10.0.3.16'})
changed: [mysql1] => (item={u'regexp': u'^Hostname=Zabbix server', u'line': u'#Hostname=Zabbix server'})
changed: [mysql2] => (item={u'regexp': u'^Hostname=Zabbix server', u'line': u'#Hostname=Zabbix server'})
changed: [lee] => (item={u'regexp': u'^Hostname=Zabbix server', u'line': u'#Hostname=Zabbix server'})
changed: [mysql1] => (item={u'regexp': u'^#HostnameItem=system.hostname', u'line': u'HostnameItem=system.hostname'})
changed: [mysql2] => (item={u'regexp': u'^#HostnameItem=system.hostname', u'line': u'HostnameItem=system.hostname'})
changed: [lee] => (item={u'regexp': u'^#HostnameItem=system.hostname', u'line': u'HostnameItem=system.hostname'})
changed: [mysql1] => (item={u'regexp': u'^# HostMetadata=', u'line': u'HostMetadata=LEE_TEST'})
changed: [mysql2] => (item={u'regexp': u'^# HostMetadata=', u'line': u'HostMetadata=LEE_TEST'})
changed: [lee] => (item={u'regexp': u'^# HostMetadata=', u'line': u'HostMetadata=LEE_TEST'})
TASK [5. Zabbix-Agent Start] ********************************************************************************************************
changed: [mysql1]
changed: [mysql2]
changed: [lee]
PLAY RECAP **************************************************************************************************************************
lee : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
mysql1 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
mysql2 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
■ 참고
https://blog.leocat.kr/notes/2018/02/07/ansible-line-in-file
https://www.codetd.com/ko/article/6668579
https://www.middlewareinventory.com/blog/ansible-lineinfile-multiple-lines-replace-multiple-lines/
2.systemctl
https://runebook.dev/ko/docs/ansible/collections/ansible/builtin/systemd_module
3.Ansible Zabbix-Agent 배포
'Open Source 툴' 카테고리의 다른 글
[ Jmeter ] 성능부하 테스트 (5) | 2023.01.25 |
---|---|
[ Zabbix ] Zabbix - Slack 연동 (0) | 2023.01.17 |
Zabbix-agent Auto Registeration ( 자동 등록 ) (0) | 2021.12.23 |
Maria Replication 모니터링 ( feat. Zabbix ) (0) | 2021.12.13 |
오픈소스 모니터링 Zabbix 5.0 설치 (0) | 2021.11.22 |
소중한 공감 감사합니다