【实验环境及需求】

1
2
3
4
OS 版本: centos7.9
zabbix 版本: 4.0 LTS

test 主机中使用 playbook 进行批量部署 zabbix-agent

【playbook 剧本】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
# playbook 中排除特定主机
# - test:!10.101.20.87:!10.101.20.88
# 批量部署zabbix-agent

- hosts: test
vars:
IPAddress: "{{ ansible_bond1.ipv4.address }}"

tasks:
- name: disable firewalld service
service:
name: firewalld
state: stopped
enabled: no

- name: temporary close selinux
shell: /usr/sbin/setenforce 0

- name: permanent close selinux
selinux:
state: disabled

- name: download zabbix rpm
shell: /usr/bin/rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm && /usr/bin/yum clean all

- name: install zabbix-agent package
yum:
name: zabbix-agent
state: present

- name: set service start on boot
service:
name: zabbix-agent
state: started
enabled: yes

- name: modify zabbix-server host address
lineinfile:
path: "/etc/zabbix/zabbix_agentd.conf"
regexp: '^Server='
line: "Server=10.243.20.51"
notify:
- Restart zabbix-agent

- name: modify listen port
lineinfile:
path: "/etc/zabbix/zabbix_agentd.conf"
regexp: '^# ListenPort'
line: "ListenPort=10050"
notify:
- Restart zabbix-agent

- name: modify Listen Address
lineinfile:
path: "/etc/zabbix/zabbix_agentd.conf"
regexp: '^# ListenIP'
line: "ListenIP=0.0.0.0"
notify:
- Restart zabbix-agent

- name: modify StartAgents
lineinfile:
path: "/etc/zabbix/zabbix_agentd.conf"
regexp: '^# StartAgents'
line: "StartAgents=5"
notify:
- Restart zabbix-agent

- name: modify Hostname is Local IP
lineinfile:
path: "/etc/zabbix/zabbix_agentd.conf"
regexp: '^Hostname'
line: "Hostname={{ IPAddress }}"
notify:
- Restart zabbix-agent

handlers:
- name: Restart zabbix-agent
service:
name: zabbix-agent
state: restarted