Saltstack之系统初始化

我使用了我自己制作的saltstack-minion的docker镜像,进行saltstack自动实现系统初始化。

PS:处理过程中发现docker的ntpd 是无法更新时间的。需要映射宿主的时间,使用docker -v参数。

docker获取方法:

1
docker pull docker.io/516249940/saltsatack-minion

系统初始化过程:每一个sls文件测试一遍 >> 然后写入到初始化目录的sls >> 最后加入到top.sls

1
2
3
4
5
6
#vim /etc/salt/master   
file_roots:
  base:
    /srv/salt/base
  prod:
    /srv/salt/prod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#分别创建yum.sls
#vim /srv/salt/base/init/yum.sls
[root@salt-master init]# cat yum.sls
yum-list-init:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      man
      - vim-enhanced
      - wget
      - telnet
      lsof
      - sysstat
      - openssh-clients
      - lrzsz
      - tree
      - hdparm
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master init]# cat dns.sls 
/etc/resolv.conf:
 
  file.managed:
 
    source: salt://init/config/resolv.conf
 
    - user: root
 
    - group: root
 
    - mode: 644
    - backup: '*'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@salt-master init]# cat cron.sls 
ntpdate-init:
  pkg.installed:
    - name: ntpdate
 
crontab-init:
  pkg.installed:
    - name: crontabs
  cmd.run:
    - name: /etc/init.d/crond restart
    
 
set-crontab:
  cron.present:
    - name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
    - user: root
    - minute: '*5'
    - require:
      - pkg: ntpdate-init
      - pkg: crontab-init
1
2
3
4
5
6
#再创建一个/srv/salt/base/init/env_init.sls
[root@salt-master init]# cat env_init.sls 
include:
  - init.dns
  - init.cron
  - init.yum
1
2
3
4
5
最后创建top.sls
[root@salt-master base]# cat top.sls 
base:
  '*minion*':                  #匹配id含有minion的主机
    - init.env_init
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master base]# salt-key 
Accepted Keys:
docker-minion01
docker-minion02
hddcluster1
hddcluster2
hddcluster3
hddcluster4
salt-master
salt-minion01
Denied Keys:
Unaccepted Keys:
Rejected Keys:
1
2
3
4
5
分别执行下面命令测试上面的编写的文件
salt 'docker-minion0*' state.sls init.yum test=true
salt 'docker-minion0*' state.sls init.dns test=true
salt 'docker-minion0*' state.sls init.cron test=true
全部通过测试
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
最后测试top.sls
[root@salt-master base]# salt 'docker-minion0*'   state.highstate test=True
docker-minion02:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: The file /etc/resolv.conf is in the correct state
     Started: 11:00:15.272191
    Duration: 16.812 ms
     Changes:   
----------
          ID: ntpdate-init
    Function: pkg.installed
        Name: ntpdate
      Result: True
     Comment: Package ntpdate is already installed
     Started: 11:00:16.425816
    Duration: 504.677 ms
     Changes:   
----------
          ID: crontab-init
    Function: pkg.installed
        Name: crontabs
      Result: True
     Comment: Package crontabs is already installed
     Started: 11:00:16.930610
    Duration: 0.357 ms
     Changes:   
----------
          ID: crontab-init
    Function: cmd.run
        Name: /etc/init.d/crond restart
      Result: None
     Comment: Command "/etc/init.d/crond restart" would have been executed
     Started: 11:00:16.931733
    Duration: 0.254 ms
     Changes:   
----------
          ID: set-crontab
    Function: cron.present
        Name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
      Result: True
     Comment: Cron /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1 already present
     Started: 11:00:16.932840
    Duration: 253.346 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed
     Started: 11:00:17.186312
    Duration: 0.422 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed
     Started: 11:00:17.186803
    Duration: 0.285 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lsof
      Result: True
     Comment: Package lsof is already installed
     Started: 11:00:17.187151
    Duration: 0.287 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed
     Started: 11:00:17.187501
    Duration: 0.277 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: openssh-clients
      Result: True
     Comment: Package openssh-clients is already installed
     Started: 11:00:17.187840
    Duration: 0.275 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: telnet
      Result: True
     Comment: Package telnet is already installed
     Started: 11:00:17.188176
    Duration: 0.288 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: hdparm
      Result: True
     Comment: Package hdparm is already installed
     Started: 11:00:17.188528
    Duration: 0.274 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: sysstat
      Result: True
     Comment: Package sysstat is already installed
     Started: 11:00:17.188865
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: wget
      Result: True
     Comment: Package wget is already installed
     Started: 11:00:17.189199
    Duration: 0.28 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: man
      Result: True
     Comment: Package man is already installed
     Started: 11:00:17.189542
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc-c++
      Result: True
     Comment: Package gcc-c++ is already installed
     Started: 11:00:17.189876
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: vim-enhanced
      Result: True
     Comment: Package vim-enhanced is already installed
     Started: 11:00:17.190211
    Duration: 0.291 ms
     Changes:   
 
Summary for docker-minion02
-------------
Succeeded: 17 (unchanged=1)
Failed:     0
-------------
Total states run:     17
Total run time:  778.941 ms
docker-minion01:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: The file /etc/resolv.conf is in the correct state
     Started: 11:00:15.249261
    Duration: 34.726 ms
     Changes:   
----------
          ID: ntpdate-init
    Function: pkg.installed
        Name: ntpdate
      Result: True
     Comment: Package ntpdate is already installed
     Started: 11:00:16.442680
    Duration: 501.913 ms
     Changes:   
----------
          ID: crontab-init
    Function: pkg.installed
        Name: crontabs
      Result: True
     Comment: Package crontabs is already installed
     Started: 11:00:16.944708
    Duration: 0.357 ms
     Changes:   
----------
          ID: crontab-init
    Function: cmd.run
        Name: /etc/init.d/crond restart
      Result: None
     Comment: Command "/etc/init.d/crond restart" would have been executed
     Started: 11:00:16.945815
    Duration: 0.255 ms
     Changes:   
----------
          ID: set-crontab
    Function: cron.present
        Name: /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1
      Result: True
     Comment: Cron /usr/bin/ntpdate times.aliyun.com >> /dev/null 2>&1 already present
     Started: 11:00:16.946927
    Duration: 258.464 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed
     Started: 11:00:17.205522
    Duration: 0.496 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lrzsz
      Result: True
     Comment: Package lrzsz is already installed
     Started: 11:00:17.206094
    Duration: 0.317 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: lsof
      Result: True
     Comment: Package lsof is already installed
     Started: 11:00:17.206480
    Duration: 0.283 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed
     Started: 11:00:17.206848
    Duration: 0.281 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: openssh-clients
      Result: True
     Comment: Package openssh-clients is already installed
     Started: 11:00:17.207192
    Duration: 0.313 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: telnet
      Result: True
     Comment: Package telnet is already installed
     Started: 11:00:17.207570
    Duration: 0.273 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: hdparm
      Result: True
     Comment: Package hdparm is already installed
     Started: 11:00:17.207906
    Duration: 0.275 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: sysstat
      Result: True
     Comment: Package sysstat is already installed
     Started: 11:00:17.208242
    Duration: 0.279 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: wget
      Result: True
     Comment: Package wget is already installed
     Started: 11:00:17.208584
    Duration: 0.272 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: man
      Result: True
     Comment: Package man is already installed
     Started: 11:00:17.208917
    Duration: 0.271 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: gcc-c++
      Result: True
     Comment: Package gcc-c++ is already installed
     Started: 11:00:17.209248
    Duration: 0.279 ms
     Changes:   
----------
          ID: yum-list-init
    Function: pkg.installed
        Name: vim-enhanced
      Result: True
     Comment: Package vim-enhanced is already installed
     Started: 11:00:17.209589
    Duration: 0.278 ms
     Changes:   
 
Summary for docker-minion01
-------------
Succeeded: 17 (unchanged=1)
Failed:     0
-------------
Total states run:     17
Total run time:  799.332 ms
[root@salt-master base]#
1
2
最后一步初始化系统:
salt 'docker-minion0*'   state.highstate


写着写着发觉自己的写作水平真差劲········


本文转自 yanconggod 51CTO博客,原文链接:http://blog.51cto.com/yanconggod/1883139


上一篇:额一个东西


下一篇:Java 实现九九乘法表