1. 添加zabbix自动发现规则
1.1 发现规则定义如下
1.2 点击添加之后就会发现添加成功了
[root@localhost ~]# yum -y install zabbix-get
[root@localhost ~]# zabbix_get -s 127.0.0.1 -k agent.ping
1
[root@localhost ~]#
2. 添加zabbix自动发现的动作
动作添加成功
3. Ansible配置
3.1 zabbix目录配置
创建zabbix工作目录
[root@localhost ~]# mkdir zabbix
[root@localhost ~]# cd zabbix/
复制zabbix源到工作目录
[root@localhost zabbix]# cp /etc/yum.repos.d/zabbix_Aliyun.repo ./
复制 zabbix_agentd.conf 配置文件到工作目录
[root@localhost zabbix]# cp /etc/zabbix/zabbix_agentd.conf ./
修改配置文件中的两处
[root@localhost zabbix]# vim zabbix_agentd.conf
Server=192.168.153.183
ServerActive=192.168.153.183
3.2 添加主机清单
[root@localhost zabbix]# vim /etc/ansible/hosts
[zabbix_agent]
192.168.153.184
192.168.153.185
3.3 复制公钥
[root@localhost zabbix]# ssh-copy-id root@192.168.153.184
[root@localhost zabbix]# ssh-copy-id root@192.168.153.185
3.4 测试主机连通性
[root@localhost fonts]# ansible zabbix_agent -m ping
192.168.153.185 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.153.184 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost fonts]#
3.5 编写agent playbook文件
[root@localhost zabbix]# vim agent.yml
---
- hosts: zabbix_agent
tasks:
- name: "推送zabbix yum源"
copy: src=/root/zabbix/zabbix_Aliyun.repo dest=/etc/yum.repos.d/
- name: "安装zabbix客户端"
yum: name=zabbix-agent state=latest
- name: "推送agent配置文件"
copy: src=/root/zabbix/zabbix_agentd.conf dest=/etc/zabbix/
tags:
- cfg-agent
notify:
- "restart zabbix-agent"
- name: "开启zabbix客户端服务"
systemd: name=zabbix-agent state=started
- name: "关闭防火墙服务"
systemd: name=firewalld state=stopped
handlers:
- name: "restart zabbix-agent"
systemd: name=zabbix-agent state=restarted
3.6 检测并执行agent playbook文件
3.6.1 检测
[root@localhost zabbix]# ansible-playbook --syntax-check agent.yml
playbook: agent.yml
3.6.2 执行
[root@localhost zabbix]# ansible-playbook agent.yml
PLAY [zabbix_agent] *************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.153.185]
ok: [192.168.153.184]
TASK [推送zabbix yum源] ************************************************************************************************************************************
changed: [192.168.153.185]
changed: [192.168.153.184]
TASK [安装zabbix客户端] **************************************************************************************************************************************
changed: [192.168.153.184]
changed: [192.168.153.185]
TASK [推送agent配置文件] **************************************************************************************************************************************
changed: [192.168.153.184]
changed: [192.168.153.185]
TASK [开启zabbix客户端服务] ************************************************************************************************************************************
changed: [192.168.153.185]
changed: [192.168.153.184]
TASK [关闭防火墙服务] ******************************************************************************************************************************************
changed: [192.168.153.184]
changed: [192.168.153.185]
RUNNING HANDLER [restart zabbix-agent] ******************************************************************************************************************
changed: [192.168.153.184]
changed: [192.168.153.185]
PLAY RECAP **********************************************************************************************************************************************
192.168.153.184 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.153.185 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost zabbix]#
4. 验证
在zabbix端查看,主机发现成功
可以在图形界面查看发现主机的一些信息