saltstack的探索-演示部署文件和脚本安装zabbix-agent服务
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
|
1. 配置salt-master
# yum -y install salt-master
# service salt-master start
防火墙放行端口:4505:4506
2. 配置其他主机test1-test9为minion
# yum -y install salt-minion
调整配置文件
# vim /etc/salt/minion
master: ip
id
:
hostname
以其中一个minion为例:
# cp -a /etc/salt/minion /etc/salt/minion.bak && s_ip=10.10.10.1 && s_host=$(hostname) && sed -i -e "s/#master: salt/master: ${s_ip}/" -e "s/#id:/id: ${s_host}/" /etc/salt/minion && cat /etc/salt/minion |grep ^[^#]
master: 10.10.10.1
id
: test1.company.com
# service salt-minion start
3. master接受minion
[root@master ~]
# salt-key -L
Accepted Keys:
Unaccepted Keys:
test1.company.com
test2.company.com
test3.company.com
test4.company.com
test5.company.com
test6.company.com
test7.company.com
test8.company.com
test9.company.com
Rejected Keys:
[root@master ~]
# salt-key -a *.company.com
The following keys are going to be accepted:
Unaccepted Keys:
test1.company.com
test2.company.com
test3.company.com
test4.company.com
test5.company.com
test6.company.com
test7.company.com
test8.company.com
test9.company.com
Proceed? [n
/Y
] y
Key
for
minion test1.company.com accepted.
Key
for
minion test2.company.com accepted.
Key
for
minion test3.company.com accepted.
Key
for
minion test4.company.com accepted.
Key
for
minion test5.company.com accepted.
Key
for
minion test6.company.com accepted.
Key
for
minion test7.company.com accepted.
Key
for
minion test8.company.com accepted.
Key
for
minion test9.company.com accepted.
[root@master ~]
# salt "*.company.com" test.ping
test1.company.com:
True
test2.company.com:
True
test3.company.com:
True
test4.company.com:
True
test5.company.com:
True
test6.company.com:
True
test7.company.com:
True
test8.company.com:
True
test9.company.com:
True
4. 【更新vim配置】
[root@master salt]
# salt '*.company.com' state.sls edit.vim
salt的配置如下:
-----------
[root@master srv]
# ls
pillar salt
[root@master srv]
# pwd
/srv
[root@master srv]
# cat salt/edit/vim.sls
vim:
pkg:
- installed
- name: {{ pillar[
'pkgs'
][
'vim'
] }}
/root/
.vimrc:
file
.managed:
-
source
: salt:
//edit/conf/vimrc
- mode: 644
- uesr: root
- group: root
- require:
- pkg: vim
[root@master srv]
# ls salt/edit/conf/
vimrc
[root@master srv]
# cat pillar/top.sls
base:
'*'
:
- pkg
[root@master srv]
# cat pillar/pkg/init.sls
pkgs:
{%
if
grains[
'os_family'
] ==
'RedHat'
%}
vim: vim-enhanced
{%
elif
grains[
'os_family'
] ==
'Debian'
%}
vim: vim
{%
elif
grains[
'os'
] ==
'Arch'
%}
vim: vim
{% endif %}
-----------
5. 【增加zabbix-agent】
目录和脚本:
[root@master salt]
# ls zabbix/
agent.sls bin/ sbin/
[root@master salt]
# ls zabbix/bin/
install_agent.sh zabbix-agent
[root@master salt]
# ls zabbix/sbin/
zabbix_agent zabbix_agentd
[root@master salt]
# cat zabbix/bin/install_agent.sh
#!/bin/bash
#
# 2015/4/10
cip=$(ip a s dev em2 |
grep
"global"
|
awk
'{print $2}'
|
cut
-d
'/'
-f1)
sip=
'10.10.10.10'
#cp ./zabbix-agent /etc/init.d/
#cp ../sbin/zabbix* /usr/sbin/
mkdir
-p
/etc/zabbix/zabbix_agentd
.conf.d
cat
<<_CFG >>
/etc/zabbix/zabbix_agentd
.conf
PidFile=
/tmp/zabbix_agentd
.pid
LogFile=
/tmp/zabbix_agentd
.log
SourceIP=$cip
Server=$sip
ListenIP=$cip
ServerActive=$sip
Hostname=$cip
Timeout=30
Include=
/etc/zabbix/zabbix_agentd
.conf.d/
_CFG
useradd
-s
/sbin/nologin
-d
/var/lib/zabbix
-c
"Zabbix Monitoring System"
zabbix
chmod
+x
/etc/init
.d
/zabbix-agent
service zabbix-agent start
chkconfig zabbix-agent on
chkconfig --list |
grep
zabbix
salt配置:
[root@master salt]
# cat zabbix/agent.sls
/usr/sbin/zabbix_agent
:
file
.managed:
-
source
: salt:
//zabbix/sbin/zabbix_agent
- mode: 755
/usr/sbin/zabbix_agentd
:
file
.managed:
-
source
: salt:
//zabbix/sbin/zabbix_agentd
- mode: 755
/etc/rc
.d
/init
.d
/zabbix-agent
:
file
.managed:
-
source
: salt:
//zabbix/bin/zabbix-agent
- mode: 755
/data/ops/bin/install_agent
.sh:
file
.managed:
-
source
: salt:
//zabbix/bin/install_agent
.sh
- mode: 755
require:
-
file
:
/etc/rc
.d
/init
.d
/zabbix-agent
install
-agent:
cmd.run:
- require:
-
file
:
/data/ops/bin/install_agent
.sh
- name:
/bin/bash
/data/ops/bin/install_agent
.sh
在其中一台上测试执行这个sls:
[root@master salt]
# salt 'test1.company.com' state.sls zabbix.agent
test1.company.com:
----------
ID:
/usr/sbin/zabbix_agent
Function:
file
.managed
Result: True
Comment: File
/usr/sbin/zabbix_agent
updated
Started: 17:01:15.356802
Duration: 352.254 ms
Changes:
----------
diff
:
New
file
mode:
0755
----------
ID:
/usr/sbin/zabbix_agentd
Function:
file
.managed
Result: True
Comment: File
/usr/sbin/zabbix_agentd
updated
Started: 17:01:15.709238
Duration: 93.603 ms
Changes:
----------
diff
:
New
file
mode:
0755
----------
ID:
/etc/rc
.d
/init
.d
/zabbix-agent
Function:
file
.managed
Result: True
Comment: File
/etc/rc
.d
/init
.d
/zabbix-agent
updated
Started: 17:01:15.802999
Duration: 8.472 ms
Changes:
----------
diff
:
New
file
mode:
0755
----------
ID:
/data/ops/bin/install_agent
.sh
Function:
file
.managed
Result: True
Comment: File
/data/ops/bin/install_agent
.sh updated
Started: 17:01:15.811627
Duration: 7.134 ms
Changes:
----------
diff
:
New
file
mode:
0755
----------
ID:
install
-agent
Function: cmd.run
Name:
/bin/bash
/data/ops/bin/install_agent
.sh
Result: True
Comment: Command
"/bin/bash /data/ops/bin/install_agent.sh"
run
Started: 17:01:15.819710
Duration: 118.255 ms
Changes:
----------
pid:
3524
retcode:
0
stderr:
stdout:
Starting Zabbix agent: [ OK ]
zabbix-agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Summary
------------
Succeeded: 5 (changed=5)
Failed: 0
------------
Total states run: 5
确认无误后,批量执行。
[root@master salt]
# salt '*.company.com' state.sls zabbix.agent
注:后续再采用rpm包来安装,此处只是简单的做法,演示部署文件和脚本。
|
本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1631034,如需转载请自行联系原作者