Ansible-playbook install Zabbix_agent
一、创建目录
在下面显示的目录结构中,包含了zabbix安装、卸载和配置三个角色(roles),以及每一个roles的所有tasks列表、vars变量和zabbix配置文件。其中安装和卸载分别通过zabbix_install.yml、zabbix_delete.yml主任务程序的入口文件调用
规划如下:
[root@ansible /etc/ansible ]# tree zabbix_rhel/
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
|
[root@ansible
/etc/ansible
]
# tree zabbix_rhel/
zabbix_rhel/
└── zabbix_agent
├── roles
│ ├── common
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ └── vars
│ ├── configure
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ ├──
install
│ │ ├── files
│ │ │ └── ansible-zabbix-3.0.8.
tar
.gz
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ ├── tasks
│ │ │ ├── 01-create-user.yml
│ │ │ ├── 02-copy-code.yml
│ │ │ ├── 03-start-zabbix.yml
│ │ │ ├── 04-add-iptables.yml
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ ├── zabbix_agentd
│ │ │ └── zabbix_agentd.conf
│ │ └── vars
│ │ └── main.yml
│ └── uninstall
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ ├── del_iptables.yml
│ │ ├── main.yml
│ │ └── uninstall_zabbix.yml
│ ├── templates
│ └── vars
│ └── main.yml
├── zabbix_delete.yml
└── zabbix_install.yml
|
通过如下命令创建
1
2
3
4
5
|
[root@ansible
/etc/ansible
]
# mkdir zabbix_rhel
[root@ansible
/etc/ansible
]
# mkdir zabbix_rhel/zabbix_agent
[root@ansible
/etc/ansible
]
# mkdir zabbix_rhel/zabbix_agent/roles
[root@ansible
/etc/ansible
]
# mkdir zabbix_rhel/zabbix_agent/roles
[root@ansible
/etc/ansible
]
# mkdir zabbix_rhel/zabbix_agent/roles/{common,install,uninstall,configure}/{handlers,files,meta,tasks,templates,vars} -p
|
主机列表/etc/ansible/hosts
1
2
3
|
[testhosts]
10.17.83.33
10.17.83.34
|
二、安装程序的tasks任务列表
1、定义安装程序入口文件zabbix_install.yml
1
2
3
4
5
6
7
8
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim zabbix_install.yml
---
- hosts: testhosts
remote_user: root
gather_facts: True
roles:
- common
-
install
|
2、定义安装程序-创建用户任务01-create-user.yml
1
2
3
4
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/tasks/01-create-user.yml
---
- name: Create zabbix user
user: name={{ zabbix_user }} state=present createhome=no shell=
/sbin/nologin
|
3、定义安装程序-拷贝安装文件任务02-copy-code.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/tasks/02-copy-code.yml
---
- name: Copy zabbix agentd code
file
to clients
copy: src=ansible-zabbix-{{ zabbix_version }}.
tar
.gz dest=
/usr/local/src/ansible-zabbix-
{{ zabbix_version }}.
tar
.gz
owner=root group=root
- name: Uncompression ansible-zabbix-{{ zabbix_version }}.
tar
.gz
shell:
tar
zxf
/usr/local/src/ansible-zabbix-
{{ zabbix_version }}.
tar
.gz -C
/usr/local
- name: Copy zabbix start script
template: src=zabbix_agentd dest=
/etc/init
.d
/zabbix_agentd
owner=root group=root mode=0755
- name: Copy zabbix config
file
template: src=zabbix_agentd.conf dest={{ zabbix_dir }}
/etc/zabbix_agentd
.conf
owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644
- name: Modify zabbix basedir permisson
file
: path={{ zabbix_dir }} owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755 recurse=
yes
- name: Link zabbix_agentd
command
shell:
ln
-s {{ zabbix_dir }}
/sbin/zabbix_agentd
/usr/local/sbin/zabbix_agentd
- name: Delete ansible-zabbix-{{ zabbix_version }}.
tar
.gz
source
file
shell:
rm
-rf
/usr/local/src/ansible-zabbix-
{{ zabbix_version }}.
tar
.gz
|
4、定义安装程序-启动zabbix_agentd服务任务03-start-zabbix.yml
1
2
3
4
5
6
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/tasks/03-start-zabbix.yml
---
- name: Start zabbix service
shell:
/etc/init
.d
/zabbix_agentd
start
- name: Add boot start zabbix service
shell: chkconfig --level 345 zabbix_agentd on
|
5、定义安装程序-添加iptable规则04-add-iptables.yml
1
2
3
4
5
6
7
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/tasks/04-add-iptables.yml
---
- name: insert iptables rule
for
zabbix
lineinfile: dest=
/etc/sysconfig/iptables
create=
yes
state=present regexp=
"{{ zabbix_agentd_port }}"
insertafter=
"^:OUTPUT "
line=
"-A INPUT -p tcp --dport {{ zabbix_agentd_port }} -s {{ zabbix_server_ip }} -j ACCEPT"
notify: restart iptables
|
6、定义安装程序-tasks任务列表的主调用接口文件main.yml
1
2
3
4
5
6
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/tasks/main.yml
---
- include: 01-create-user.yml
- include: 02-copy-code.yml
- include: 03-start-zabbix.yml
- include: 04-add-iptables.yml
|
tasks任务列表说明:
Playbook允许用户将tasks任务细分为多个任务列表,通过一个main任务来调用。当然你也可以将涉及的所有任务全部写到main.yml文件中。
7、定义common任务
该任务为安装一些依赖软件包
1
2
3
4
5
6
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/common/tasks/main.yml
- name: Install initializtion require software
yum: name={{ item }} state=latest
with_items:
- libselinux-python
- libcurl-devel
|
三、安装程序的vars变量文件
在roles/install/vars/目录中创建main.yml文件,并定义安装过程中使用到变量
1
2
3
4
5
6
7
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/vars/main.yml
zabbix_dir:
/usr/local/zabbix
zabbix_version: 3.0.8
zabbix_user: zabbix
zabbix_server_port: 10051
zabbix_agentd_port: 10050
zabbix_server_ip: 10.17.81.120
|
四、安装程序的handlers任务定义
handlers目录中为tasks任务中notify调用的动作(当文件发生改变时,通过notify执行相关的操作,比如修改了配置文件后,需要重启相应的服务)
1
2
3
4
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# vim roles/install/handlers/main.yml
---
- name: restart iptables
service: name=iptables state=restarted
|
五、安装程序的zabbix安装文件
将预先编译好的zabbix打包后,放到files/目录中,Playbook在执行操作过程中会根据tasks任务将文件发送到目标主机上
1
2
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# ls roles/install/files/
ansible-zabbix-3.0.8.
tar
.gz
|
六、安装程序的zabbix配置文件
使用Playbook安装zabbix涉及的所有配置文件都通过templates模块去同步
1
2
|
[root@ansible
/etc/ansible/zabbix_rhel/zabbix_agent
]
# ls roles/install/templates/
zabbix_agentd zabbix_agentd.conf
|
在安装任务中,zabbix_agentd.conf配置文件主要涉及zabbix服务端IP地址的修改,可以通过预定义的变量来替换
zabbix_agentd.conf文件如下
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
# This is a configuration file for Zabbix agent daemon (Unix)
# To get more information about Zabbix, visit http://www.zabbix.com
############ GENERAL PARAMETERS #################
### Option: PidFile
# Name of PID file.
#
# Mandatory: no
# Default:
# PidFile=/tmp/zabbix_agentd.pid
### Option: LogType
# Specifies where log messages are written to:
# system - syslog
# file - file specified with LogFile parameter
# console - standard output
#
# Mandatory: no
# Default:
# LogType=file
### Option: LogFile
# Log file name for LogType 'file' parameter.
#
# Mandatory: no
# Default:
# LogFile=
LogFile=
/tmp/zabbix_agentd
.log
### Option: LogFileSize
# Maximum size of log file in MB.
# 0 - disable automatic log rotation.
#
# Mandatory: no
# Range: 0-1024
# Default:
# LogFileSize=1
### Option: DebugLevel
# Specifies debug level:
# 0 - basic information about starting and stopping of Zabbix processes
# 1 - critical information
# 2 - error information
# 3 - warnings
# 4 - for debugging (produces lots of information)
# 5 - extended debugging (produces even more information)
#
# Mandatory: no
# Range: 0-5
# Default:
# DebugLevel=3
### Option: SourceIP
# Source IP address for outgoing connections.
#
# Mandatory: no
# Default:
# SourceIP=
### Option: EnableRemoteCommands
# Whether remote commands from Zabbix server are allowed.
# 0 - not allowed
# 1 - allowed
#
# Mandatory: no
# Default:
# EnableRemoteCommands=0
### Option: LogRemoteCommands
# Enable logging of executed shell commands as warnings.
# 0 - disabled
# 1 - enabled
#
# Mandatory: no
# Default:
# LogRemoteCommands=0
##### Passive checks related
### Option: Server
# List of comma delimited IP addresses (or hostnames) of Zabbix servers.
# Incoming connections will be accepted only from the hosts listed here.
# If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally.
#
# Mandatory: no
# Default:
# Server=
Server={{ zabbix_server_ip
|