之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是http://www.zzvips.com/article/99548.html),今天介绍一下如何使用playbook来部署zabbix客户端。
ansible服务端的环境为centos 6.5 x86_64系统
ansible客户端环境为centos 6.3 x86_64系统
目前我的playbook只允许centos或redhat 6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6.
下面是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 |
14:29:30 # pwd
/etc/ansible/roles
root@ip-10-10-10-10:/etc/ansible/roles
14:29:37 # tree zabbix_client_*
zabbix_client_delete 删除已经安装的zabbix客户端
├── files 存放文件的
├── handlers 重启的东东
├── meta galaxy_info的信息
│ └── main.yml
├── tasks 操作的任务流程
│ ├── delete.yml
│ └── main.yml
├── templates 模板
└── vars 变量
└── main.yml
zabbix_client_install
├── files
│ └── zabbix-2.0.6.tar.gz
├── handlers
├── meta
│ └── main.yml
├── tasks
│ ├── copy.yml
│ ├── delete.yml
│ ├── install.yml
│ └── main.yml
├── templates
│ ├── zabbix_agentd
│ └── zabbix_agentd.conf
└── vars
└── main.yml
12 directories, 13 files |
下面是先介绍一下安装方面zabbix_client_install的内容
1、galaxy_info的信息
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
14:32:15 # cat /etc/ansible/roles/zabbix_client_install/meta/main.yml
galaxy_info:
author: Deng Lei
description: Install Zabbix Client
license: MIT
min_ansible_version: 1.6
platforms:
- name: CentOS
versions:
- 6
categories:
- Monitor
dependencies: [] |
2、task里的copy.xml信息
?
|
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 |
14:33:35 # cat /etc/ansible/roles/zabbix_client_install/tasks/copy.yml
- name: Stop Exist Zabbix Client Service In Redhat Client
shell: ps -ef|grep zabbix|grep -v grep|awk '{print $2}'|xargs kill -9 >>/dev/null 2>&1
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Delete Exist Zabbix Client Dir In Redhat Client
shell: rm -rf {{ zabbix_dir }}/zabbix
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Install Base Require Software In Redhat Client
yum: name={{ item }} state=latest
with_items:
- telnet
- dmidecode
- tar
- name: Create Zabbix User In Redhat Client
user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Copy Zabbix Client Software To Redhat Client
copy: src=zabbix-{{ zabbix_version }}.tar.gz dest=/tmp/zabbix-{{ zabbix_version }}.tar.gz owner=root group=root
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Uncompression Zabbix Client Software To Redhat Client
shell: tar zxf /tmp/zabbix-{{ zabbix_version }}.tar.gz -C {{ zabbix_dir }}/
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Copy Zabbix Start Script To Redhat Client
template: src=zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Copy Zabbix Config To Redhat Client
template: src=zabbix_agentd.conf dest={{ zabbix_dir }}/zabbix/conf/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6 |
此文件是复制对应的文件到客户端
3、task的install.yml信息
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
14:34:26 # cat /etc/ansible/roles/zabbix_client_install/tasks/install.yml
- name: Modify Zabbix Dir Permission In Redhat Client
file: path={{ zabbix_dir }}/zabbix owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Check Zabbix User Sudo Permission In Redhat Client
shell: grep "{{ zabbix_user }}" /etc/sudoers|wc -l
register: zabbix_sudoer
ignore_errors: True
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Give Sudo Permission To Zabbix User In Redhat Client
shell: echo "{{ zabbix_user }} ALL=(root) NOPASSWD:/bin/netstat, /usr/bin/omreport" >> /etc/sudoers
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6 and zabbix_sudoer|int ==0
- name: Start Zabbix Service In Redhat Client
shell: /etc/init.d/zabbix_agentd start
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
- name: Add Boot Start Zabbix Service In Redhat Client
shell: chkconfig --level 345 zabbix_agentd on
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6 |
此文件主要是安装
4、tasks的delete.yml信息
?
|
1
2
3
4 |
14:35:08 # cat /etc/ansible/roles/zabbix_client_install/tasks/delete.yml
- name: Delete Zabbix compression Software In Redhat Client
shell: rm -rf /tmp/zabbix-{{ zabbix_version }}.tar.gz
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6 |
此文件是安装完成后,删除安装前的文件
5、tasks的mail.yml
?
|
1
2
3
4 |
14:35:37 # cat /etc/ansible/roles/zabbix_client_install/tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml |
此文件是允许运行哪个文件
6、templates的zabbix_agentd
?
|
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 |
15:15:45 # cat /etc/ansible/roles/zabbix_client_install/templates/zabbix_agentd
#!/bin/bash
#
# chkconfig: - 85 15
# description: Zabbix client script.
# processname: Zabbix
. /etc/profile
SERVICE="Zabbix agent"
DAEMON={{ zabbix_dir }}/zabbix/sbin/zabbix_agentd
PIDFILE=/tmp/zabbix_agentd.pid
CONFIG={{ zabbix_dir }}/zabbix/conf/zabbix_agentd.conf
zabbix_agent_status=`ps aux|grep zabbix_agentd.conf|grep -v grep|wc -l`
zabbix_agent_pid=`ps aux|grep zabbix_agentd|grep -v grep|awk 'NR==1{print $2}'`
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
function check()
{
if [ $? -eq 0 ];then
action $"Operating is:" /bin/true
else
action $"Operating is:" /bin/false
fi
}
case $1 in
'start')
if [ -x ${DAEMON} ]
then
$DAEMON -c $CONFIG
# Error checking here would be good...
echo "${SERVICE} started."
else
echo "Can't find file ${DAEMON}."
echo "${SERVICE} NOT started."
fi
check
;;
'stop')
if [ -s ${PIDFILE} ]
then
if kill `cat ${PIDFILE}` >/dev/null 2>&1
then
echo "${SERVICE} terminated."
rm -f ${PIDFILE}
fi
fi
check
;;
'restart')
/bin/bash $0 stop
sleep 5
/bin/bash $0 start
;;
'status')
if [ $zabbix_agent_status -ne 0 ];then
echo "Zabbix Agentd is running ($zabbix_agent_pid)"
else
echo "Zabbix Agentd is not running!"
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;;
esac
exit 0 |
这个文件是启动客户端的脚本
7、templates的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
|
