0%

Zabbix(8)使用ansible批量部署zabbix agent

相同系统环境部署zabbix agent,可以使用ansible playbook来实现,大概分为以下几步。

ansible 目录结构

[root@sl_confluence ansible]# pwd
/etc/ansible
[root@sl_confluence ansible]# tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.
├── ansible.cfg
├── hosts
├── roles
│   └── zabbix_agent
│   ├── files
│   │   └── zabbix-agent-3.4.8-1.el6.x86_64.rpm
│   ├── tasks
│   │   └── main.yml
│   └── templates
│   └── zabbix_agentd.conf.j2
└── zabbix_agent.yml

5 directories, 6 files

查看 hosts文件

[root@sl_confluence ansible]# cat hosts

1
2
[zabbix_agent]
dbserver

[root@sl_confluence ansible]# cat zabbix_agent.yml

1
2
3
4
5
6
7
8
9
- hosts: zabbix_agent
user: root
vars:
zabbix_serverip: 192.168.6.186 # zabbix 服务器IP
zabbix_activeip: 192.168.6.186 # zabbix 服务器IP
agent_hostname: '{{ ansible_hostname }}' # 客户端hostname
agent_ip: '{{ ansible_eth0.ipv4.address }}' # 客户端IP 根据实际情况修改自己的网卡名字eth0 改成自己服务器
roles:
- zabbix_agent

[root@sl_confluence tasks]# cat main.yml

1
2
3
4
5
6
7
8
9
10
- name: copy zabbix_agentd 
copy: src=/etc/ansible/roles/zabbix_agent/files/zabbix-agent-3.4.8-1.el6.x86_64.rpm dest=/usr/local/src/zabbix-agent-3.4.8-1.el6.x86_64.rpm
- name: install zabbix-agend
shell: rpm -ivh /usr/local/src/zabbix-agent-3.4.8-1.el6.x86_64.rpm # 安装zabbix agentd
- name: up zabbix-agent file client # 上传配置文件
template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
- name: enabled service zabbix-agent # 打开zabbix-agent 开机启动
service: name=zabbix-agent enabled=yes
- name: start service zabbix-agent # 启动zabbix-agent
service: name=zabbix-agent state=started

定义zabbix_agent.conf模板文件

1
2
3
4
5
6
7
8
9
10
[root@sl_confluence templates]# cat zabbix_agentd.conf.j2 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
EnableRemoteCommands=1
Server={{zabbix_serverip}}
ListenPort=10050
ServerActive={{zabbix_activeip}}
Hostname={{ansible_hostname}}
AllowRoot=1
Include=/etc/zabbix/zabbix_agentd.d/*.conf

执行安装

1
ansible-playbook zabbix_agent.yml

z