1 在zabbix-server机器上安装ansible
yum -y install ansible
2 将准备安装zabbix-agent的服务器写入ansible主机清单
vim /etc/ansible/hosts
[zabbix] 192.168.123.131 hostname=node1 192.168.123.137 hostname=node2 192.168.123.138 hostname=node3
注:其中hostname 是后面template模块会用到的hostname变量
3 创建部署所需要的role—zabbix-agent
cd /etc/ansible/roles mkdir -pv zabbix-agent/{files,templates,tasks}
注: 其中zabbix-agent下的三个文件夹都很重要,files下可以放zabbix-agent安装包或者yum源,templates放含有变量hostname的zabbix-agent配置文件
3.1 在files目录下,配置yum源(这个yum源直接用来配置zabbix-server也完全没问题的)
cd /etc/ansible/roles/zabbix-agent/files
[root@zabbix-server files]# vim zabbix.repo
[zabbix] name=zabbix baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/ gpgcheck=0 enabled=1 [zabbix2] name=zabbix frontend baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/frontend/ gpgcheck=0 enabled=1
3.2 在templates目录下,cp配置文件,设置变量。
server和server active都设置为zabbix-server的ip,hostname使用变量。cp的配置文件一定是j2结尾
cp /etc/zabbix/zabbix_agentd.conf /etc/ansible/roles/zabbix-agent/templates/zabbix_agentd.conf.j2
注:配置文件中,上面三个位置需要修改
3.3 在tasks目录下,创建main.yml 文件
vim /etc/ansible/roles/zabbix-agent/tasks/main.yml
- name: get the zabbix-agent copy: src: zabbix.repo dest: /etc/yum.repos.d/ - name: install the zabbix-agent yum: name: zabbix-agent state: present - name: copy the zabbix_agentd.conf template: src: zabbix_agentd.conf.j2 dest: /etc/zabbix/zabbix_agentd.conf - name: start zabbix-agent service: name: zabbix-agent state: started enabled: true
注: zabbix-agent目录下可以直接调用,所以无需写明templates和files的绝对路径
4 给其他节点服务器做免密
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:SUHLRNME2qo3Z2ly2UUA6xL/sWSMffpKSqvdPHneaJI root@node1
The key’s randomart image is:
±–[RSA 2048]----+
| oO=o |
| = =… |
| o * . |
| * = . |
| o S * o |
| . . B * |
| . + O *o |
| . X *E.oo |
| …+ +B+ . |
±—[SHA256]-----+
ssh-copy-id root@192.168.123.131 ssh-copy-id root@192.168.123.137 ssh-copy-id root@192.168.123.138
以上三台密钥设置成功
5 配置一个playbook,我直接在root下创建的
vim zabbix-agent.yml
--- - name: zabbix_install hosts: zabbix roles: - zabbix-agent
6 执行剧本,ansible部署zabbix-agent完成
ansible-playbook zabbix-agent.yml
执行完成
PLAY [zabbix_install]
TASK [Gathering Facts]
******************************************************************************* ok: [192.168.123.138] ok: [192.168.123.137] ok: [192.168.123.131]
TASK [get the zabbix-agent]
************************************************************************** ok: [192.168.123.137] ok: [192.168.123.138] ok: [192.168.123.131]
TASK [install the zabbix-agent]
********************************************************************** ok: [192.168.123.138] ok: [192.168.123.131] ok: [192.168.123.137]
TASK [zabbix-agent : copy the zabbix_agentd.conf]
**************************************************** ok: [192.168.123.137] ok: [192.168.123.138] ok: [192.168.123.131]
TASK [start zabbix-agent]
**************************************************************************** changed: [192.168.123.137] changed: [192.168.123.138] changed:
[192.168.123.131]
PLAY RECAP
192.168.123.131 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.123.137 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.123.138 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7 zabbix-server界面设置自动发现主机,并且添加至主机组(附大量图片)
创建发现规则
最后点击add,我这个创建好了,所以是update按钮
创建动作
注: 还需要点击operations 点击操作哦,动作和操作同时完成才ok
以上完成了,点击add,或者update即可
8 zabbix-server中查看已发现,和成功添加的主机
怎么看自己添加的主机呢?最后一张图了,坚持就是胜利
完结,撒花~~~~