-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
632 lines (426 loc) · 475 KB
/
atom.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>南汐</title>
<subtitle>勇敢的去做你认为正确的事情!</subtitle>
<link href="https://smallflames.github.io/atom.xml" rel="self"/>
<link href="https://smallflames.github.io/"/>
<updated>2024-04-26T16:14:25.411Z</updated>
<id>https://smallflames.github.io/</id>
<author>
<name>Xia Zheng</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>Python编程基础篇001</title>
<link href="https://smallflames.github.io/2024/02/03/Python%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E7%AF%87001/"/>
<id>https://smallflames.github.io/2024/02/03/Python%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E7%AF%87001/</id>
<published>2024-02-02T16:06:59.000Z</published>
<updated>2024-04-26T16:14:25.411Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Python编程基础篇</center></h1><blockquote><p> ✨ 在pyhton中,经常会用到input()语句,但是input()语句输入的内容只能时字符串类型,而我们经常要输入int类型的数据等,那么就需要用到int()方法给输入的内容强制转换为int类型,但是如果输入的不是数字,而是字母、标点等,这样就会报错,所以我们就要判断一下,输入的内容输入的内容是否为我们的期望类型。</p></blockquote><h2 id="一、判断input-输入的数据的类型">一、判断input()输入的数据的类型</h2><p>str.isdigit()为True表示输入的所有字符都是数字(小数会返回False)<br>str.isalnum()为True表示输入的所有字符都是数字或者字母<br>str.isalpha()为True表示输入的所有字符都是字母<br>str.isdigit()为True表示输入的所有字符都是数字<br>str.islower()为True表示输入的所有字符都是小写<br>str.isupper()为True表示输入的所有字符都是大写<br>str.istitle()为True表示输入的所有单词都是首字母大写,像标题<br>str.isspace()为True表示输入的所有字符都是空白字符、\t、\n、\r</p><h2 id="二、基础编程训练">二、基础编程训练</h2><table><tr><td bgcolor=#000000><font color="white">需求分析</font></td></tr></table><p><font color="#fc5531">▚ 需求 :用Python实现输入三个整数,并判断其最大值。</font></p><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240426234435273.png" alt="image-20240426234435273"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">实现代码</font></td></tr></table><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">if_num</span>(<span class="params">a</span>) :</span><br><span class="line"> <span class="keyword">if</span> a.isdigit():</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'非法参数值,请输入整数!!!'</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">def</span> <span class="title function_">num_max</span>(<span class="params">a,b,c</span>):</span><br><span class="line"> <span class="keyword">if</span> a>b:</span><br><span class="line"> <span class="keyword">if</span> a>c:</span><br><span class="line"> <span class="built_in">max</span>=a</span><br><span class="line"> res=<span class="string">'第一个数值最大'</span></span><br><span class="line"> <span class="keyword">elif</span> a<c:</span><br><span class="line"> <span class="built_in">max</span>=c</span><br><span class="line"> res=<span class="string">'第三个数值最大'</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">max</span>=c</span><br><span class="line"> res=<span class="string">'第一个数和第三个数值最大'</span></span><br><span class="line"> <span class="keyword">elif</span> a<b:</span><br><span class="line"> <span class="keyword">if</span> b>c:</span><br><span class="line"> <span class="built_in">max</span>=b</span><br><span class="line"> res=<span class="string">'第二个数值最大'</span></span><br><span class="line"> <span class="keyword">elif</span> b<c:</span><br><span class="line"> <span class="built_in">max</span>=c</span><br><span class="line"> res=<span class="string">'第三个数值最大'</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> res=<span class="string">'第二个数和第三个数值最大'</span></span><br><span class="line"> <span class="built_in">max</span>=b</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">if</span> a>c:</span><br><span class="line"> <span class="built_in">max</span>=a</span><br><span class="line"> res=<span class="string">'第一个数和第二个数值最大'</span></span><br><span class="line"> <span class="keyword">elif</span> a<c:</span><br><span class="line"> <span class="built_in">max</span>=c</span><br><span class="line"> res=<span class="string">'第三个数值最大'</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">max</span>=a</span><br><span class="line"> res=<span class="string">'三个数值相等'</span></span><br><span class="line"> <span class="keyword">return</span> res,<span class="built_in">max</span></span><br><span class="line"></span><br><span class="line">a=<span class="built_in">input</span>(<span class="string">'请输入第一个数:'</span>)</span><br><span class="line">b=<span class="built_in">input</span>(<span class="string">'请输入第二个数:'</span>)</span><br><span class="line">c=<span class="built_in">input</span>(<span class="string">'请输入第三个数:'</span>)</span><br><span class="line"></span><br><span class="line">i=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> num <span class="keyword">in</span> a,b,c:</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"> <span class="keyword">if</span> if_num(num)==<span class="number">1</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f'第<span class="subst">{i}</span>个参数不合法,终止程序...'</span>)</span><br><span class="line"> os.exit(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">a=<span class="built_in">int</span>(a)</span><br><span class="line">b=<span class="built_in">int</span>(b)</span><br><span class="line">c=<span class="built_in">int</span>(c)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(num_max(a,b,c))</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=2028829287&auto=1&height=66"></iframe><blockquote><p><em><strong>人不能服从自己的人,就要服从他人。这是有生命者的本性。</strong></em></p><p align="right">——尼采</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Python编程基础篇</center></h1>
<blockquote>
<p> ✨ 在pyhton中,经常会用到input()语句,但是input()语句输入的内容只能时字符串类型,而我们经常要输入int类型的数据等,那么就需要用到int()方法给输入的内容强制转换为int类型,但是如果输入的不是数字,而是字母、标点等,这样就会报错,所以我们就要判断一下,输入的内容输入的内容是否为我们的期望类型。</p>
</blockquote>
<h2 id="一、判断input-输入的数据的类型">一、判断input()输入的数据的类型</h2>
<p>str.isdigit()为True表示输入的所有字符都是数字(小数会返回False)<br>
str.isalnum()为True表示输入的所有字符都是数字或者字母<br>
str.isalpha()为True表示输入的所有字符都是字母<br>
str.isdigit()为True表示输入的所有字符都是数字<br>
str.islower()为True表示输入的所有字符都是小写<br>
str.isupper()为True表示输入的所有字符都是大写<br>
str.istitle()为True表示输入的所有单词都是首字母大写,像标题<br>
str.isspace()为True表示输入的所有字符都是空白字符、\t、\n、\r</p>
<h2 id="二、基础编程训练">二、基础编程训练</h2>
<table><tr><td bgcolor=#000000><font color="white">需求分析</font></td></tr></table>
<p><font color="#fc5531">▚ 需求 :用Python实现输入三个整数,并判断其最大值。</font></p>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240426234435273.png" alt="image-20240426234435273"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Python" scheme="https://smallflames.github.io/tags/Python/"/>
</entry>
<entry>
<title>Ubantu搭建内网仓库</title>
<link href="https://smallflames.github.io/2024/02/01/Ubantu%E6%90%AD%E5%BB%BA%E5%86%85%E7%BD%91%E4%BB%93%E5%BA%93/"/>
<id>https://smallflames.github.io/2024/02/01/Ubantu%E6%90%AD%E5%BB%BA%E5%86%85%E7%BD%91%E4%BB%93%E5%BA%93/</id>
<published>2024-02-01T15:30:55.000Z</published>
<updated>2024-04-06T10:49:15.415Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ubantu搭建内网仓库</center></h1><blockquote><p>✨ Ubuntu基于Debian发行版和GNOME桌面环境,与Debian的不同在于它每6个月会发布一个新版本。Ubuntu的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。<br>🎉 Shell 是一个程序,提供一个与用户对话的环境。这个环境只有一个命令提示符,让用户从键盘输入命令,所以又称为命令行环境(command line interface,简写为 CLI)。</p></blockquote><h2 id="font-color-red-▚-u-Step-01-u-:搭建本地源仓库-font"><font color=red>▚ <u>Step 01</u> :搭建本地源仓库</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">apt clean </span><br><span class="line">apt -y install -d nginx 举例</span><br><span class="line"><span class="comment"># /var/cache/apt/archives # deb包的位置</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">mkdir</span> /root/ceshi</span><br><span class="line"><span class="built_in">mv</span> /var/cache/apt/archives/*.deb /root/ceshi</span><br><span class="line"></span><br><span class="line">apt -y install dpkg-dev (生成源所需工具)</span><br><span class="line"><span class="built_in">cd</span> /root/ceshi</span><br><span class="line">apt-ftparchive packages . > Packages <span class="comment"># 安装包信息都读取到 Packages</span></span><br><span class="line">apt-ftparchive release . > Release</span><br><span class="line"><span class="comment"># dpkg-scanpackages ./packages | gzip -9c > Packages.gz</span></span><br><span class="line"><span class="comment"># gpg --clearsign -o InRelease Release # 生成 Release 的明文签名文件</span></span><br><span class="line"><span class="comment"># gpg -ab -o Release.gpg Release # 生成 Release 的文本分离签名文件</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240221115118994.png" alt="image-20240221115118994"></p><span id="more"></span><h2 id="font-color-red-▚-u-Step-02-u-:创建内网源服务器-font"><font color=red>▚ <u>Step 02</u> :创建内网源服务器</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">apt -y install nginx</span><br><span class="line"><span class="comment"># 默认配置文件路径 /etc/nginx/sites-available/default</span></span><br><span class="line"><span class="comment"># 默认首页文件存放路径 /var/www/html</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">mkdir</span> /var/www/html/ceshi</span><br><span class="line"><span class="built_in">cp</span> -r /root/ceshi /var/www/html</span><br><span class="line">vim /etc/nginx/sites-available/default</span><br><span class="line">...</span><br><span class="line">location /ceshi {</span><br><span class="line"> <span class="built_in">alias</span> /var/www/html/ceshi;</span><br><span class="line"> autoindex on;</span><br><span class="line">}</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">systemctl restart nginx</span><br><span class="line">systemctl <span class="built_in">enable</span> nginx</span><br><span class="line"></span><br><span class="line">ss -nlt | grep <span class="string">"80"</span></span><br><span class="line">可以先测试访问一下</span><br><span class="line">http://192.168.10.94/ceshi/</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240221115415513.png" alt="image-20240221115415513"></p><h2 id="font-color-red-▚-u-Step-03-u-:配置并使用内网源-font"><font color=red>▚ <u>Step 03</u> :配置并使用内网源</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">使用apt源(在需要使用内网源的ubuntu上操作)</span><br><span class="line"><span class="built_in">mv</span> /etc/apt/sources.list /etc/apt/sources.list.bak</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"deb [trusted=yes] http://192.168.10.94/ceshi/ ./"</span> > /etc/apt/sources.list</span><br><span class="line"></span><br><span class="line">[root@elk93 ~]<span class="comment"># apt update</span></span><br><span class="line">Ign:1 http://192.168.10.94/ceshi ./ InRelease</span><br><span class="line">Get:2 http://192.168.10.94/ceshi ./ Release [816 B]</span><br><span class="line">Ign:3 http://192.168.10.94/ceshi ./ Release.gpg</span><br><span class="line">Get:4 http://192.168.10.94/ceshi ./ Packages [8,056 B]</span><br><span class="line">Fetched 8,872 B <span class="keyword">in</span> 0s (66.5 kB/s)</span><br><span class="line">Reading package lists... Done</span><br><span class="line">Building dependency tree</span><br><span class="line">Reading state information... Done</span><br><span class="line">All packages are up to <span class="built_in">date</span>.</span><br><span class="line"></span><br><span class="line">[root@elk93 /etc/apt]<span class="comment"># apt install tree</span></span><br><span class="line">Reading package lists... Done</span><br><span class="line">Building dependency tree</span><br><span class="line">Reading state information... Done</span><br><span class="line"></span><br><span class="line">No apt package <span class="string">"tree"</span>, but there is a snap with that name.</span><br><span class="line">Try <span class="string">"snap install tree"</span></span><br><span class="line"></span><br><span class="line">E: Unable to locate package tree</span><br><span class="line"></span><br><span class="line">apt install -y <packages></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240221111314481.png" alt="image-20240221111314481"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p> <div id="aplayer-FAtmhwQi" class="aplayer aplayer-tag-marker" style="margin-bottom: 20px;"> <pre class="aplayer-lrc-content"></pre> </div> <script> var ap = new APlayer({ element: document.getElementById("aplayer-FAtmhwQi"), narrow: false, autoplay: true, showlrc: false, music: { title: "Whale (鲸)", author: "Jannik", url: "http://cdn.zzxe.eu.org/music/Whale%20%28%E9%B2%B8%29.m4a", pic: "https://p2.music.126.net/97-CeOH6aysRsv1TYBA71Q==/109951164727131802.jpg", lrc: "" } }); window.aplayers || (window.aplayers = []); window.aplayers.push(ap); </script><blockquote><p><em><strong>以希望为生的人,将绝食而死。</strong></em></p><p align="right">——富兰克林(美国)</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ubantu搭建内网仓库</center></h1>
<blockquote>
<p>✨ Ubuntu基于Debian发行版和GNOME桌面环境,与Debian的不同在于它每6个月会发布一个新版本。Ubuntu的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。<br>
🎉 Shell 是一个程序,提供一个与用户对话的环境。这个环境只有一个命令提示符,让用户从键盘输入命令,所以又称为命令行环境(command line interface,简写为 CLI)。</p>
</blockquote>
<h2 id="font-color-red-▚-u-Step-01-u-:搭建本地源仓库-font"><font color=red>▚ <u>Step 01</u> :搭建本地源仓库</font></h2>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">apt clean </span><br><span class="line">apt -y install -d nginx 举例</span><br><span class="line"><span class="comment"># /var/cache/apt/archives # deb包的位置</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">mkdir</span> /root/ceshi</span><br><span class="line"><span class="built_in">mv</span> /var/cache/apt/archives/*.deb /root/ceshi</span><br><span class="line"></span><br><span class="line">apt -y install dpkg-dev (生成源所需工具)</span><br><span class="line"><span class="built_in">cd</span> /root/ceshi</span><br><span class="line">apt-ftparchive packages . &gt; Packages <span class="comment"># 安装包信息都读取到 Packages</span></span><br><span class="line">apt-ftparchive release . &gt; Release</span><br><span class="line"><span class="comment"># dpkg-scanpackages ./packages | gzip -9c &gt; Packages.gz</span></span><br><span class="line"><span class="comment"># gpg --clearsign -o InRelease Release # 生成 Release 的明文签名文件</span></span><br><span class="line"><span class="comment"># gpg -ab -o Release.gpg Release # 生成 Release 的文本分离签名文件</span></span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240221115118994.png" alt="image-20240221115118994"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Docker" scheme="https://smallflames.github.io/tags/Docker/"/>
</entry>
<entry>
<title>Dockerfile制作镜像</title>
<link href="https://smallflames.github.io/2024/02/01/Dockerfile%E5%88%B6%E4%BD%9C%E9%95%9C%E5%83%8F/"/>
<id>https://smallflames.github.io/2024/02/01/Dockerfile%E5%88%B6%E4%BD%9C%E9%95%9C%E5%83%8F/</id>
<published>2024-02-01T13:42:21.000Z</published>
<updated>2024-04-06T10:50:01.092Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Dockerfile制作镜像</center></h1><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/docker-architecture.webp" alt="Docker Architecture diagram"></p><h2 id="一、Dockerfile常用指令">一、Dockerfile常用指令</h2><table><thead><tr><th>指令</th><th>释义</th></tr></thead><tbody><tr><td>FROM</td><td>指定基础镜像,必须放在首行。(注意,scratch是保留字,并不是镜像!如果使用了该关键字表示不依赖于任何镜像) && 调用基础镜像触发器(ONBUILD)。</td></tr><tr><td>ONBUILD</td><td>基础镜像触发器。当镜像用作另一个构建的基础时触发。后面可以跟其他的镜像指令运行,所以当我们基于他人镜像构建新镜像时为安全考虑应当特别注意基础镜像是否有不安全的ONBUILD指令。</td></tr><tr><td>MAINTAINER</td><td>管理者(维护者)标识,用于声明作者的信息。(<mark>已弃用</mark>)</td></tr><tr><td>LABEL</td><td>为镜像打标签。基于KEY=VALUE方式打标签。</td></tr><tr><td>USER</td><td>指定容器的运行用户,若不指定,默认为root用户,一些服务可能会用到,比如ES服务…</td></tr><tr><td>RUN</td><td>在容器中运行指令。</td></tr><tr><td>WORKDIR</td><td>指定容器的工作目录,当用户连接到容器时,就默认在该目录,若不指定,则默认在"/"路径。</td></tr><tr><td>VOLUME</td><td>对容器的指定路径生成随机存储卷。</td></tr><tr><td>COPY</td><td>将宿主机的文件拷贝到容器中。如果容器目录不存在,则会自动创建。</td></tr><tr><td>ADD</td><td>与COPY作用不同的是,它可以自动解压tar包文件并删除源文件。</td></tr><tr><td>ENV</td><td>将变量传递给容器,容器运行时会有该变量。在容器启动或创建时可以使用-e,–env选项替换默认值。</td></tr><tr><td>ARG</td><td>在编译阶段生效的变量。容器运行时不会存在该变量!在编译的时候可以使用"–build-arg"选项替换默认值。</td></tr><tr><td>EXPOSE</td><td>暴露容器内端口,外部使用随机端口映射时,可以暴露此处定义的服务端口,通常建议暴露的端口和服务有关。用户启动容器时使用"-P"时会为容器暴露的端口自动做映射</td></tr><tr><td>CMD</td><td>容器启动时执行的指令,我们在制作容器时,通常会在此处阻塞终端。</td></tr><tr><td>ENTRYPOINT</td><td>同CMD,容器启动时执行的指令,但是此处可传入参数,启动指令可能因参数产生变化。</td></tr><tr><td>HEALTHCHECK</td><td>容器健康检查。</td></tr><tr><td>SHELL</td><td>调用系统命令的shell。这对Windows很有用,Linux无需使用,默认就是linux的shell。</td></tr></tbody></table><div class="warning"><p> <strong>相似指令区分</strong><br> 1️⃣ <strong>COPY和ADD:</strong> COPY用于将宿主机文件拷贝至容器,或者在多阶段构建时用于将上一构建阶段的文件拷贝至本阶段构建。ADD指令同样可用于拷贝文件,但是该指令可以自动解压tar包,并删除源文件。</p><p> 2️⃣ <strong>ARG和ENV:</strong> ARG指令和ENV指令的作用域不同,ARG指令指定的便令仅在镜像构建时起作用,镜像构建完成生命周期即结束。ENV指令将环境变量 <code><key></code> 设置为值 <code><value></code>将在构建阶段的所有后续指令的环境中,并且也可以在许多指令中内联替换。</p><p> 3️⃣ <strong>CMD和ENTRYPOINT:</strong> CMD指令设置从镜像运行容器时固定要执行的命令。Dockerfile中只能有一条 <code>CMD</code> 指令。如果您列出多个 <code>CMD</code> ,则只有最后一个生效。如果用户为 <code>docker run</code> 指定参数,那么他们将覆盖 <code>CMD</code> 中指定的默认值。ENTRYPOINT指令设置从镜像运行容器时要执行的命令,但用户可以为该指令传递参数,若CMD指令和ENTRYPOINT指令同时使用则CMD指令将作为ENTRYPOINT指令的参数传递。</p></div><h2 id="二、Dockerfile制作镜像">二、Dockerfile制作镜像</h2><p>1️⃣ 创建Dockerfile文件。 根据个人习惯而言,一般为了文件的可读性,我们需要先创建一个空目录,并在目录下创建Dockerfile文件,(约定俗成我们一般就以Dockerfile为名,编译时也会自动寻找这个文件,也可自定义文件名,但是我们在编译文件时需用 <code>-f</code> 指定需编译文件的绝对路径,自定义文件名语义混乱容易导致文件错落,不建议!)</p><span id="more"></span><p>2️⃣ 准备制作镜像的资源文件。(我们制作镜像需要的压缩包、源码文件、配置文件等等)</p><p>3️⃣ 编写Dockerfile文件。(以alpine为基础镜像打包制作游戏镜像为例)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">[root@nanxi ~/nanxi]<span class="comment"># tree -L 1</span></span><br><span class="line">.</span><br><span class="line">├── build.sh</span><br><span class="line">├── default.conf</span><br><span class="line">├── Dockerfile</span><br><span class="line">└── game.tar.gz</span><br><span class="line"></span><br><span class="line">[root@nanxi ~]<span class="comment"># cat Dockerfile</span></span><br><span class="line">FROM alpine</span><br><span class="line"></span><br><span class="line">MAINTAINER nanxi [email protected]</span><br><span class="line"></span><br><span class="line">LABEL auther=<span class="string">"nanxi"</span> \</span><br><span class="line"> email=<span class="string">"[email protected]"</span> \</span><br><span class="line"> blog=https://zzxe.eu.org</span><br><span class="line"></span><br><span class="line">EXPOSE 80</span><br><span class="line"></span><br><span class="line">VOLUME /code/game</span><br><span class="line"></span><br><span class="line">RUN sed -i <span class="string">'s/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g'</span> /etc/apk/repositories && \</span><br><span class="line"> apk update && apk add nginx && <span class="built_in">rm</span> -rf /var/cache/* && <span class="built_in">mkdir</span> -p /code/game</span><br><span class="line"></span><br><span class="line">COPY default.conf /etc/nginx/http.d/default.conf</span><br><span class="line">ADD game.tar.gz /code/game</span><br><span class="line"></span><br><span class="line">CMD [<span class="string">"nginx"</span>,<span class="string">"-g"</span>,<span class="string">"daemon off;"</span>]</span><br></pre></td></tr></table></figure><p>4️⃣ 编译Dockerfile文件生成镜像</p><p>使用<code>docker build -t <镜像名>:<版本号> .</code> 编译文件生成镜像,或使用shell脚本调用docker命令进行编译</p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240305223411353.png" alt="image-20240305223411353"></p><p>5️⃣ 启动一个容器并测试访问。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@nanxi ~/nanxi/01-nanxi-game]<span class="comment"># docker run -dP nanxi_game:v1.0</span></span><br><span class="line">ac6fb3594b726a9d7a75a74d8d6273f08aa823bcd9bd62ef0f64635cd9c37f58</span><br><span class="line"></span><br><span class="line">[root@nanxi ~/nanxi]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">ac6fb3594b72 nanxi_game:v1.0 <span class="string">"nginx -g 'daemon of…"</span> About an hour ago Up About an hour 0.0.0.0:32770->80/tcp, :::32770->80/tcp intelligent_wu</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[root@nanxi ~/nanxi]<span class="comment"># docker inspect -f '正在运行容器: {{.Name}} | 容器IP地址为: {{.NetworkSettings.IPAddress}} | 容器进程号: {{.State.Pid}} | 挂载点: {{range .Mounts}}{{.Source}}{{end}}' $(docker ps -q)</span></span><br><span class="line">正在运行容器: /intelligent_wu | 容器IP地址为: 172.20.7.2 | 容器进程号: 111403 | 挂载点: /var/lib/docker/volumes/1f3cdca6af933cc8779ccbc4e84c64ff64866e352fb9001f6fa0a0df88b85f8</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240305223846010.png" alt="image-20240305223846010"></p><h2 id="三、Dockerfile优化思路">三、Dockerfile优化思路</h2><h3 id="1、从编译速度上说">1、从编译速度上说</h3><p>① 指定的基础镜像建议是本地存在的或者是国内的镜像;② 将下载源修改为国内的软件源;③ 尽量利用Dockerfile构建的镜像缓存技术(将不经常修改的指令往前放,如:FROM 、MAINTAINER、LABEL、EXPOSE、WORKDIR、VOLUME… 将经常修改的指令往后放,如:RUN、COPY、ADD、CMD、ENTRYPOINT…)④ 使用".dockerignore",忽略我们不需要参与编译的文件(如不忽略,则编译前会当前文件夹所有文件传递给docker deamon进程,但我们真正编译时用不到这些与制作镜像不相关的文件。)</p><h3 id="2、从镜像体积上说">2、从镜像体积上说</h3><p>① 删除无用的缓存;②卸载无用的软件;③ 使用较小的基础镜像,比如将centos基础环境改为Ubuntu或者alpine镜像。</p><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Dockerfile制作镜像</center></h1>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/docker-architecture.webp" alt="Docker Architecture diagram"></p>
<h2 id="一、Dockerfile常用指令">一、Dockerfile常用指令</h2>
<table>
<thead>
<tr>
<th>指令</th>
<th>释义</th>
</tr>
</thead>
<tbody>
<tr>
<td>FROM</td>
<td>指定基础镜像,必须放在首行。(注意,scratch是保留字,并不是镜像!如果使用了该关键字表示不依赖于任何镜像) &amp;&amp; 调用基础镜像触发器(ONBUILD)。</td>
</tr>
<tr>
<td>ONBUILD</td>
<td>基础镜像触发器。当镜像用作另一个构建的基础时触发。后面可以跟其他的镜像指令运行,所以当我们基于他人镜像构建新镜像时为安全考虑应当特别注意基础镜像是否有不安全的ONBUILD指令。</td>
</tr>
<tr>
<td>MAINTAINER</td>
<td>管理者(维护者)标识,用于声明作者的信息。(<mark>已弃用</mark>)</td>
</tr>
<tr>
<td>LABEL</td>
<td>为镜像打标签。基于KEY=VALUE方式打标签。</td>
</tr>
<tr>
<td>USER</td>
<td>指定容器的运行用户,若不指定,默认为root用户,一些服务可能会用到,比如ES服务…</td>
</tr>
<tr>
<td>RUN</td>
<td>在容器中运行指令。</td>
</tr>
<tr>
<td>WORKDIR</td>
<td>指定容器的工作目录,当用户连接到容器时,就默认在该目录,若不指定,则默认在&quot;/&quot;路径。</td>
</tr>
<tr>
<td>VOLUME</td>
<td>对容器的指定路径生成随机存储卷。</td>
</tr>
<tr>
<td>COPY</td>
<td>将宿主机的文件拷贝到容器中。如果容器目录不存在,则会自动创建。</td>
</tr>
<tr>
<td>ADD</td>
<td>与COPY作用不同的是,它可以自动解压tar包文件并删除源文件。</td>
</tr>
<tr>
<td>ENV</td>
<td>将变量传递给容器,容器运行时会有该变量。在容器启动或创建时可以使用-e,–env选项替换默认值。</td>
</tr>
<tr>
<td>ARG</td>
<td>在编译阶段生效的变量。容器运行时不会存在该变量!在编译的时候可以使用&quot;–build-arg&quot;选项替换默认值。</td>
</tr>
<tr>
<td>EXPOSE</td>
<td>暴露容器内端口,外部使用随机端口映射时,可以暴露此处定义的服务端口,通常建议暴露的端口和服务有关。用户启动容器时使用&quot;-P&quot;时会为容器暴露的端口自动做映射</td>
</tr>
<tr>
<td>CMD</td>
<td>容器启动时执行的指令,我们在制作容器时,通常会在此处阻塞终端。</td>
</tr>
<tr>
<td>ENTRYPOINT</td>
<td>同CMD,容器启动时执行的指令,但是此处可传入参数,启动指令可能因参数产生变化。</td>
</tr>
<tr>
<td>HEALTHCHECK</td>
<td>容器健康检查。</td>
</tr>
<tr>
<td>SHELL</td>
<td>调用系统命令的shell。这对Windows很有用,Linux无需使用,默认就是linux的shell。</td>
</tr>
</tbody>
</table>
<div class="warning">
<p> <strong>相似指令区分</strong><br>
1️⃣ <strong>COPY和ADD:</strong> COPY用于将宿主机文件拷贝至容器,或者在多阶段构建时用于将上一构建阶段的文件拷贝至本阶段构建。ADD指令同样可用于拷贝文件,但是该指令可以自动解压tar包,并删除源文件。</p>
<p> 2️⃣ <strong>ARG和ENV:</strong> ARG指令和ENV指令的作用域不同,ARG指令指定的便令仅在镜像构建时起作用,镜像构建完成生命周期即结束。ENV指令将环境变量 <code>&lt;key&gt;</code> 设置为值 <code>&lt;value&gt;</code>将在构建阶段的所有后续指令的环境中,并且也可以在许多指令中内联替换。</p>
<p> 3️⃣ <strong>CMD和ENTRYPOINT:</strong> CMD指令设置从镜像运行容器时固定要执行的命令。Dockerfile中只能有一条 <code>CMD</code> 指令。如果您列出多个 <code>CMD</code> ,则只有最后一个生效。如果用户为 <code>docker run</code> 指定参数,那么他们将覆盖 <code>CMD</code> 中指定的默认值。ENTRYPOINT指令设置从镜像运行容器时要执行的命令,但用户可以为该指令传递参数,若CMD指令和ENTRYPOINT指令同时使用则CMD指令将作为ENTRYPOINT指令的参数传递。</p>
</div>
<h2 id="二、Dockerfile制作镜像">二、Dockerfile制作镜像</h2>
<p>1️⃣ 创建Dockerfile文件。 根据个人习惯而言,一般为了文件的可读性,我们需要先创建一个空目录,并在目录下创建Dockerfile文件,(约定俗成我们一般就以Dockerfile为名,编译时也会自动寻找这个文件,也可自定义文件名,但是我们在编译文件时需用 <code>-f</code> 指定需编译文件的绝对路径,自定义文件名语义混乱容易导致文件错落,不建议!)</p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Docker" scheme="https://smallflames.github.io/tags/Docker/"/>
</entry>
<entry>
<title>Git及gitlab使用简述</title>
<link href="https://smallflames.github.io/2024/01/29/Git%E5%8F%8Agitlab%E4%BD%BF%E7%94%A8%E7%AE%80%E8%BF%B0/"/>
<id>https://smallflames.github.io/2024/01/29/Git%E5%8F%8Agitlab%E4%BD%BF%E7%94%A8%E7%AE%80%E8%BF%B0/</id>
<published>2024-01-29T10:47:09.000Z</published>
<updated>2024-01-30T13:11:51.338Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Git及gitlab使用简述</center></h1><blockquote><p>🎄 官网介绍:Git是一个自由和开源的分布式版本控制系统,旨在快速和高效地处理从小型到大型的所有项目。Git简单易学,占用空间小,性能快如闪电。它比Subversion、CVS、Perforce和ClearCase等SCM工具更有优势,具有廉价的本地分支、方便的登台区和多个工作流等特性。</p></blockquote><h2 id="font-color-red-▚-u-01-u-简述:Git的使用要点-font"><font color=red>▚ <u>01</u> 简述:Git的使用要点</font></h2><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240129192325464.png" alt="image-20240129192325464"></p><div class="tips"><p>一般linux系统会自带由git工具,如果没有需自行下载安装,<code>yum -y install git</code></p></div><span id="more"></span><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Git基本信息配置</span></span><br><span class="line">git config –-global user.name <span class="string">"nanxi"</span> <span class="comment">### 配置Git使用用户</span></span><br><span class="line">git config –-global user.email <span class="string">"[email protected]"</span> <span class="comment">### 配置Git使用邮箱</span></span><br><span class="line">git config –-global color.ui <span class="literal">true</span> <span class="comment">### 开启语法高亮</span></span><br><span class="line"><span class="comment"># 查看Git配置信息</span></span><br><span class="line">git --version <span class="comment">### 查看git版本</span></span><br><span class="line">git config --list <span class="comment">### 查看git配置</span></span><br><span class="line">git remote <span class="comment">### 查看远程仓库名</span></span><br><span class="line">git remote -v <span class="comment">### 查看远程仓库详细信息</span></span><br></pre></td></tr></table></figure><div class="warning"><p><strong>关于工作目录 .git 隐藏文件介绍:</strong></p><table><thead><tr><th>文件或目录</th><th>说明</th></tr></thead><tbody><tr><td>branches</td><td>分支目录</td></tr><tr><td>config</td><td>定义项目特有的配置选项</td></tr><tr><td>description</td><td>仅供git web程序使用</td></tr><tr><td>HEAD</td><td>指示当前的分支</td></tr><tr><td>hooks</td><td>包含git钩子文件</td></tr><tr><td>info</td><td>包含一个全局排除文件(exclude文件)</td></tr><tr><td>objects</td><td>存放所有数据内容,有info和pack两个子文件夹</td></tr><tr><td>refs</td><td>存放指向数据(分支)的提交对象的指针</td></tr><tr><td>index</td><td>保存暂存区信息,在执行git init的时候,这个文件还没有</td></tr></tbody></table><center><font color=red> 注意:工作目录的 .git 文件不能轻易删除!!! </font></center></div><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">步骤简述:</span><br><span class="line">1、配置git基本信息: 用户名 邮箱</span><br><span class="line">2、初始化仓库: <span class="built_in">mkdir</span> nanxi && git init nanxi</span><br><span class="line">3、添加远程仓库: git remote add origin(仓库名字可自定义) 仓库地址</span><br><span class="line">4、在本地工作目录开发,然后提交至本地仓库</span><br><span class="line">5、代码开发完成后提交至远程仓库 git push -u origin(仓库名字) master(远程代码分支) </span><br></pre></td></tr></table></figure><div class='spoiler collapsed'> <div class='spoiler-title'> Git命令了解 </div> <div class='spoiler-content'> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">git init [<span class="built_in">dir</span> & . ] (初始化 git 工作目录)</span><br><span class="line">git config --global user.name <span class="string">"nanxi"</span> (配置git用户)</span><br><span class="line">git config --global user.email <span class="string">"[email protected]"</span> (配置git用户邮箱)</span><br><span class="line">git config --global color.ui <span class="literal">true</span> (开启高亮显示)</span><br><span class="line">git config --list (查看git配置)</span><br><span class="line">git status (查看工作目录的文件状态)</span><br><span class="line">git add [filename & . ] (将文件提交到暂存区域)</span><br><span class="line">git <span class="built_in">rm</span> --cached [file] (将文件撤出暂存区) </span><br><span class="line">git <span class="built_in">rm</span> -f [file] (从暂存区域同工作区域一同删除文件)</span><br><span class="line">git commit -m <span class="string">"描述信息"</span> (将文件提交到本地仓库)</span><br><span class="line">git <span class="built_in">rm</span> (删除文件)</span><br><span class="line">git <span class="built_in">mv</span> filename1 filename (同时修改工作区域和暂存区域的文件名称)</span><br><span class="line">git <span class="built_in">log</span> (查看历史提交) </span><br><span class="line">git <span class="built_in">log</span> --oneline (显示简略历史提交) </span><br><span class="line">git <span class="built_in">log</span> --oneline --decoreate(显示简略历史提交和当前指针)</span><br><span class="line">git diff (工作目录跟暂存区文件对比) </span><br><span class="line">git diff --cached(暂存区和本地仓库文件对比)</span><br><span class="line">git checkout -- [file] (将暂存区文件覆盖本地工作目录文件)</span><br><span class="line">git tag -a v2.0 dbead4c -m <span class="string">"version v2.0"</span> (将某次提交打为标签)</span><br><span class="line">git reset --hard c3cd410 (版本回滚至 c3cd410 )</span><br><span class="line">git reflog (查看所有的历史提交)</span><br><span class="line">git branch (查看分支情况) </span><br><span class="line">git branch dev (创建分支dev)</span><br><span class="line">git checkout dev(切换分支到dev 新版本可用 git switch dev)</span><br><span class="line">git merge dev (在目标分支合并源分支dev)</span><br><span class="line">git branch -d dev (删除分支dev)</span><br></pre></td></tr></table></figure> </div></div><h2 id="font-color-red-▚-u-02-u-简述:Gitlab的安装及使用-font"><font color=red>▚ <u>02</u> 简述:Gitlab的安装及使用</font></h2><p>1️⃣ 软件安装,gitlab的官网源在国外,访问受限,我们可以使用国内权威的镜像站下载 <a href="https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/">gitlab社区版</a></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240129214935952.png" alt="image-20240129214935952"></p><p>2️⃣ 安装到linux后,修改url配置,然后重启配置即可!</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/gitlab/gitlab.rb <span class="comment"># gitlab 配置文件</span></span><br><span class="line">external_url <span class="string">'http://192.168.10.200'</span> <span class="comment"># 更改url地址为本机IP地址或域名</span></span><br><span class="line">gitlab-ctl reconfigure <span class="comment"># 更改配置文件后需重新配置</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/8B5C60B2C7185D5EE25BA445542DBFF8.png" alt="8B5C60B2C7185D5EE25BA445542DBFF8"></p><p>3️⃣ 通过web页面访问gitlab,并进行操作管理;</p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240129215940556.png" alt="image-20240129215940556"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240129220939686.png" alt="image-20240129220939686"></p><p>接下来又到了界面管理方式,大家可自行尝试摸索哦~ gitlab的探索之路南汐已为您打开,加油 👨🚀</p><div class="warning"><p><strong>一般情况下我们会先创建用户组,在用户组下创建项目,为保证代码安全,项目只对相关用户组开放,新用户创建完成后由管理员将其分配至相应的用户组,以获得相应的代码查看和推送权限(在代码拉取同步或推送过程中,为减免繁琐的工作步骤,我们一般采用免密钥登录,这点在各大代码管理平台都是如此哦~)</strong></p></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=2110291360&auto=1&height=66"></iframe><blockquote><p><em><strong>如果错过太阳时你流了泪,那么你也要错过群星。</strong></em></p><p align="right">——泰戈尔</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Git及gitlab使用简述</center></h1>
<blockquote>
<p>🎄 官网介绍:Git是一个自由和开源的分布式版本控制系统,旨在快速和高效地处理从小型到大型的所有项目。Git简单易学,占用空间小,性能快如闪电。它比Subversion、CVS、Perforce和ClearCase等SCM工具更有优势,具有廉价的本地分支、方便的登台区和多个工作流等特性。</p>
</blockquote>
<h2 id="font-color-red-▚-u-01-u-简述:Git的使用要点-font"><font color=red>▚ <u>01</u> 简述:Git的使用要点</font></h2>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240129192325464.png" alt="image-20240129192325464"></p>
<div class="tips">
<p>一般linux系统会自带由git工具,如果没有需自行下载安装,<code>yum -y install git</code></p>
</div></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="软件应用" scheme="https://smallflames.github.io/tags/%E8%BD%AF%E4%BB%B6%E5%BA%94%E7%94%A8/"/>
</entry>
<entry>
<title>Shell脚本篇006</title>
<link href="https://smallflames.github.io/2024/01/28/Shell%E8%84%9A%E6%9C%AC%E7%AF%87006/"/>
<id>https://smallflames.github.io/2024/01/28/Shell%E8%84%9A%E6%9C%AC%E7%AF%87006/</id>
<published>2024-01-28T10:45:09.000Z</published>
<updated>2024-04-26T15:28:57.889Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell脚本篇006</center></h1><table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table><font color="#fc5531">▚ 需求 :写一个脚本实现批量免密钥分发。</font><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240128184852368.png" alt="image-20240128184852368"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">实现脚本</font></td></tr></table><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Author nanxi</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Date 2024-1-28</span></span><br><span class="line"></span><br><span class="line">sshkey=~/.ssh/id_rsa</span><br><span class="line">if [ ! -f $sshkey ];then</span><br><span class="line"> ssh-keygen -P "" -f $sshkey >/dev/null</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if ! which expect>/dev/null;then</span><br><span class="line"> echo "检测到脚本运行需要的交互工具不存在,正在进行安装..."</span><br><span class="line"> yum -y install expect >/dev/null</span><br><span class="line"> [ $? -ne 0 ] && echo "expect交互工具安装失败,请您尝试手动安装~" && exit 1</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">while read line</span><br><span class="line">do</span><br><span class="line"> ip=$(echo $line |awk '{print $1}')</span><br><span class="line"> pass=$(echo $line |awk '{print $2}')</span><br><span class="line"> if ping -c1 -w1 $ip>/dev/null;then</span><br><span class="line"> /usr/bin/expect <<-EOF</span><br><span class="line"> spawn ssh-copy-id $ip -f</span><br><span class="line"> expect {</span><br><span class="line"> "yes/no" {send "yes\r";exp_continue}</span><br><span class="line"> "password" {send "$pass\n"}</span><br><span class="line"> }</span><br><span class="line"> expect eof</span><br><span class="line">EOF</span><br><span class="line"> let i++</span><br><span class="line"> else</span><br><span class="line"> echo -e "\e[1;31m$ip 的机器密钥分发异常,请您前往确保主机状态正常!!!\e[0m"</span><br><span class="line"> fi</span><br><span class="line">done << "END"</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">## IP地址 密码</span></span></span><br><span class="line">192.168.10.2 1</span><br><span class="line">192.168.10.3 1</span><br><span class="line">192.168.10.201 1</span><br><span class="line">END</span><br><span class="line">echo "$i 台机器密钥已分发成功,接下来您可以通过ssh免密钥登录终端"</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=1382592811&auto=1&height=66"></iframe><blockquote><p><em><strong>所谓安宁,就是在我追求自己所想要的生活的时候,能不卑不亢地一边失去,一边寻找。</strong></em></p><p align="right">——《被卡住的生活》</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell脚本篇006</center></h1>
<table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table>
<font color="#fc5531">▚ 需求 :写一个脚本实现批量免密钥分发。</font>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240128184852368.png" alt="image-20240128184852368"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell脚本篇005</title>
<link href="https://smallflames.github.io/2024/01/28/Shell%E8%84%9A%E6%9C%AC%E7%AF%87005/"/>
<id>https://smallflames.github.io/2024/01/28/Shell%E8%84%9A%E6%9C%AC%E7%AF%87005/</id>
<published>2024-01-28T06:48:18.000Z</published>
<updated>2024-01-28T11:07:18.104Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell脚本篇005</center></h1><table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table><font color="#fc5531">▚ 需求 :写一个脚本检测CPU使用情况,当使用CPU使用率大于90%即报警。</font><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240128161507444.png" alt="image-20240128161507444"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">实现脚本</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### vim /etc/mail.rc</span></span><br><span class="line"><span class="built_in">set</span> [email protected]</span><br><span class="line"><span class="built_in">set</span> smtp=smtps://smtp.qq.com:465</span><br><span class="line"><span class="built_in">set</span> [email protected]</span><br><span class="line"><span class="built_in">set</span> smtp-auth-password=/服务授权码</span><br><span class="line"><span class="built_in">set</span> smtp-auth=login</span><br><span class="line"><span class="built_in">set</span> ssl-verify=ignore</span><br><span class="line"><span class="built_in">set</span> nss-config-dir=/etc/pki/nssdb/</span><br></pre></td></tr></table></figure><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Author nanxi</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Date 2024-1-28</span></span><br><span class="line"></span><br><span class="line">sentmail(){</span><br><span class="line">mail -s "CPU使用告警" [email protected] <<EOT</span><br><span class="line">CPU使用率超过90%,请您注意服务异常,并进行及时处理!!</span><br><span class="line">EOT</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line"> used=$(expr 100 - $(top -bn1 | awk -F "ni," '/%Cpu/{print $2}'|cut -d "." -f1|sed "s/ //"))</span><br><span class="line"> if [ $used -gt 90 ];then</span><br><span class="line"> sentmail</span><br><span class="line"> fi</span><br><span class="line"> sleep 10</span><br><span class="line">done</span><br></pre></td></tr></table></figure><div class="warning"><p>注意:检测时间由自己的场景而定,定时任务的最小处理时间是1分钟,如果您可以接受1分钟执行一次,那么定时任务是一个非常完美的选择,如果您接受不了1分钟的时间间隔,那我们就需要用脚本去突破限制!</p></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=507585217&auto=1&height=66"></iframe><blockquote><p><em><strong>不要垂头丧气,即使失去一切,明天仍在你的手里。</strong></em></p><p align="right">——王尔德</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell脚本篇005</center></h1>
<table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table>
<font color="#fc5531">▚ 需求 :写一个脚本检测CPU使用情况,当使用CPU使用率大于90%即报警。</font>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240128161507444.png" alt="image-20240128161507444"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell脚本篇004</title>
<link href="https://smallflames.github.io/2024/01/27/Shell%E8%84%9A%E6%9C%AC%E7%AF%87004/"/>
<id>https://smallflames.github.io/2024/01/27/Shell%E8%84%9A%E6%9C%AC%E7%AF%87004/</id>
<published>2024-01-27T11:07:53.000Z</published>
<updated>2024-01-28T11:07:35.353Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell脚本篇004</center></h1><table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table><font color="#fc5531">▚ 需求 :写一个脚本反向破解由随机数生成的MD5值。</font><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240127191533426.png" alt="image-20240127191533426"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">实现脚本</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment">#Author nanxi</span></span><br><span class="line"><span class="comment">#Date 2024-1-27</span></span><br><span class="line"></span><br><span class="line">file=/tmp/.decipher.log</span><br><span class="line"><span class="function"><span class="title">prepare</span></span>(){</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"<span class="subst">$(echo 0 |md5sum)</span> 0"</span> >><span class="variable">$file</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> $(<span class="built_in">seq</span> 32767)</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"<span class="subst">$(echo $i |md5sum)</span> <span class="variable">$i</span>"</span> >><span class="variable">$file</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span> [ -f <span class="variable">$file</span> ];<span class="keyword">then</span></span><br><span class="line"> <span class="keyword">if</span> [ $(<span class="built_in">wc</span> -l <span class="variable">$file</span> |awk <span class="string">'{print $1}'</span>) -ne 32768 ];<span class="keyword">then</span></span><br><span class="line"> ><span class="variable">$file</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"首次使用本脚本可能需要1分种左右,请您耐心等待一下哦~"</span></span><br><span class="line"> prepare</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="built_in">touch</span> <span class="variable">$file</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"首次使用本脚本可能需要1分种左右,请您耐心等待一下哦~"</span></span><br><span class="line"> prepare</span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"><span class="built_in">read</span> -p <span class="string">"请输入您要破译的MD5值(n为退出): "</span> md5</span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$md5</span> =~ ^[0-z]+$ ]];<span class="keyword">then</span></span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$md5</span> <span class="keyword">in</span></span><br><span class="line"> no|n)</span><br><span class="line"> <span class="built_in">exit</span></span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="keyword">if</span> grep <span class="string">"<span class="variable">$md5</span>"</span> <span class="variable">$file</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> 本次破译结果为: $(grep <span class="string">"<span class="variable">$md5</span>"</span> <span class="variable">$file</span> |awk <span class="string">'{print $NF}'</span>)</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"抱歉,本次破译失败,您可重新尝试其他值哦~"</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m警告: 您输入的参数值不合法!!!\e[0m"</span></span><br><span class="line"> <span class="built_in">exit</span> 1</span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=1906301400&auto=1&height=66"></iframe><blockquote><p><em><strong>人生不就是这样,哪怕雨雪霏霏也要去追寻阳光。</strong></em></p><p align="right">——人民日报</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell脚本篇004</center></h1>
<table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table>
<font color="#fc5531">▚ 需求 :写一个脚本反向破解由随机数生成的MD5值。</font>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240127191533426.png" alt="image-20240127191533426"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell编程知识补充(四)</title>
<link href="https://smallflames.github.io/2024/01/26/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E5%9B%9B%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/26/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E5%9B%9B%EF%BC%89/</id>
<published>2024-01-26T07:33:08.000Z</published>
<updated>2024-01-27T08:52:42.030Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell编程基础知识补充</center></h1><h2 id="font-color-red-▚-u-01-u-补充:普通数组和关联数组-font"><font color=red>▚ <u>01</u> 补充:普通数组和关联数组</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 普通数组示例</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a=(a b c d)</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${a[*]}</span></span><br><span class="line">a b c d</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${a[@]}</span></span><br><span class="line">a b c d</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${!a[@]}</span></span><br><span class="line">0 1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${!a[*]}</span></span><br><span class="line">0 1 2 3</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 关联数组示例</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#unset a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#declare -A a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[a]=1</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[b]=2</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[c]=3</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${a[*]}</span></span><br><span class="line">1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${a[@]}</span></span><br><span class="line">1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${!a[@]}</span></span><br><span class="line">a b c</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${!a[*]}</span></span><br><span class="line">a b c</span><br><span class="line">-----------------------------------</span><br><span class="line">[root@ceshi ~]<span class="comment">#unset a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#declare -A a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a=([nanxi]="i" [love]="love" [you]="you")</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${a[*]}</span></span><br><span class="line">you love i</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo ${!a[*]}</span></span><br><span class="line">you love nanxi</span><br></pre></td></tr></table></figure><div class="warning"><p>shell中的普通数组和C的一维数组一样,但是关联数组有所不同,关联数组可以用任意的文本作为数组索引,而普通数组中的索引都是整数,且关联数组使用之前需要声明:<code>declare -A ass_array</code></p></div><span id="more"></span><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240127160621720.png" alt="image-20240127160621720"></p><h2 id="font-color-red-▚-u-02-u-补充:Shell中变量的使用-font"><font color=red>▚ <u>02</u> 补充:Shell中变量的使用</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">[root@ceshi ~]<span class="comment">#sh ceshi.sh</span></span><br><span class="line">libai</span><br><span class="line">nanxi</span><br><span class="line">nanxi</span><br><span class="line">[root@ceshi ~]<span class="comment">#cat ceshi.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">name=nanxi</span><br><span class="line"><span class="function"><span class="title">fun1</span></span>(){</span><br><span class="line"><span class="comment">### local声名的变量只在本函数(fun1)中生效,其他函数不生效</span></span><br><span class="line"> <span class="built_in">local</span> name=libai</span><br><span class="line"> <span class="built_in">echo</span> <span class="variable">$name</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="title">fun2</span></span>(){</span><br><span class="line"> <span class="built_in">echo</span> <span class="variable">$name</span></span><br><span class="line">}</span><br><span class="line">fun1</span><br><span class="line">fun2</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$name</span></span><br></pre></td></tr></table></figure><h2 id="font-color-red-▚-u-03-u-提醒:关于变量和函数调用-font"><font color=red>▚ <u>03</u> 提醒:关于变量和函数调用</font></h2><div class="warning"><p>注意⚠️ : 函数不调用不执行,变量定义时(解释器收到变量定义的指令)即执行!变量和函数一旦定义,在此shell中会一直生效,如:<code>. /etc/init.d/funtouns</code> 是用当前shell执行调用,当前shell没有退出,则里面的函数变量定义就会一直起作用,所以无论我们想调用几次action函数,我们必须在第一次调用action函数之前先调用<code>. /etc/init.d/funtouns</code>并保持环境一直生效。</p></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=1381084455&auto=1&height=66"></iframe><blockquote><p><em><strong>其实人跟树是一样的,越是向往高处的阳光,根就越要深入黑暗的地底。</strong></em></p><p align="right">——尼采</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell编程基础知识补充</center></h1>
<h2 id="font-color-red-▚-u-01-u-补充:普通数组和关联数组-font"><font color=red>▚ <u>01</u> 补充:普通数组和关联数组</font></h2>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 普通数组示例</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a=(a b c d)</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;a[*]&#125;</span></span><br><span class="line">a b c d</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;a[@]&#125;</span></span><br><span class="line">a b c d</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;!a[@]&#125;</span></span><br><span class="line">0 1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;!a[*]&#125;</span></span><br><span class="line">0 1 2 3</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 关联数组示例</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#unset a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#declare -A a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[a]=1</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[b]=2</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a[c]=3</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;a[*]&#125;</span></span><br><span class="line">1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;a[@]&#125;</span></span><br><span class="line">1 2 3</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;!a[@]&#125;</span></span><br><span class="line">a b c</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;!a[*]&#125;</span></span><br><span class="line">a b c</span><br><span class="line">-----------------------------------</span><br><span class="line">[root@ceshi ~]<span class="comment">#unset a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#declare -A a</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#a=([nanxi]=&quot;i&quot; [love]=&quot;love&quot; [you]=&quot;you&quot;)</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;a[*]&#125;</span></span><br><span class="line">you love i</span><br><span class="line">[root@ceshi ~]<span class="comment">#echo $&#123;!a[*]&#125;</span></span><br><span class="line">you love nanxi</span><br></pre></td></tr></table></figure>
<div class="warning">
<p>shell中的普通数组和C的一维数组一样,但是关联数组有所不同,关联数组可以用任意的文本作为数组索引,而普通数组中的索引都是整数,且关联数组使用之前需要声明:<code>declare -A ass_array</code></p>
</div></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell编程知识补充(三)</title>
<link href="https://smallflames.github.io/2024/01/25/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%B8%89%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/25/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%B8%89%EF%BC%89/</id>
<published>2024-01-25T11:24:57.000Z</published>
<updated>2024-01-26T07:39:42.448Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell编程基础知识补充</center></h1><blockquote><p>🍟 sed替换文本中某行的某个字符 <code>sed -i '/^admin/s/100/22/' nanxi.txt</code> 把nanxi.txt中以admin开头的行里面的100替换成22。</p></blockquote><h2 id="一、while读取文本">一、while读取文本</h2><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240125205335929.png" alt="image-20240125205335929"></p><span id="more"></span><h2 id="二、数值计算">二、数值计算</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># expr 只支持整数运算</span></span><br><span class="line">root@nanxi:~<span class="comment"># expr 1 + 1</span></span><br><span class="line">2</span><br><span class="line">root@nanxi:~<span class="comment"># expr 21 - 1</span></span><br><span class="line">20</span><br><span class="line">root@nanxi:~<span class="comment"># expr 21 \* 2</span></span><br><span class="line">42</span><br><span class="line">root@nanxi:~<span class="comment"># expr 21 / 2</span></span><br><span class="line">10</span><br><span class="line">root@nanxi:~<span class="comment"># expr 22 / 2</span></span><br><span class="line">11</span><br><span class="line">root@nanxi:~<span class="comment"># expr 22 % 2</span></span><br><span class="line">0</span><br><span class="line">-------------------------------</span><br><span class="line"><span class="comment"># $(()) 只支持整数运算效率最高</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $((2+4))</span></span><br><span class="line">6</span><br><span class="line">root@nanxi:~<span class="comment"># echo $((4-2))</span></span><br><span class="line">2</span><br><span class="line">root@nanxi:~<span class="comment"># echo $((2*4))</span></span><br><span class="line">8</span><br><span class="line">root@nanxi:~<span class="comment"># echo $((2/4))</span></span><br><span class="line">0</span><br><span class="line">root@nanxi:~<span class="comment"># echo $((4/2))</span></span><br><span class="line">2</span><br><span class="line">root@nanxi:~<span class="comment"># echo $((4%2))</span></span><br><span class="line">0</span><br><span class="line"></span><br><span class="line">-------------------------------</span><br><span class="line"><span class="comment"># $[] 只支持整数运算</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $[10+10]</span></span><br><span class="line">20</span><br><span class="line">root@nanxi:~<span class="comment"># echo $[10-10]</span></span><br><span class="line">0</span><br><span class="line">root@nanxi:~<span class="comment"># echo $[10*10]</span></span><br><span class="line">100</span><br><span class="line">root@nanxi:~<span class="comment"># echo $[10/10]</span></span><br><span class="line">1</span><br><span class="line">root@nanxi:~<span class="comment"># echo $[10%10]</span></span><br><span class="line">0</span><br><span class="line"></span><br><span class="line">-------------------------------</span><br><span class="line"><span class="comment"># let运算(必须通过变量才能计算)</span></span><br><span class="line">root@nanxi:~<span class="comment"># let 1+1</span></span><br><span class="line">root@nanxi:~<span class="comment"># let n=1+1</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $n</span></span><br><span class="line">2</span><br><span class="line">root@nanxi:~<span class="comment"># let n=2*3</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $n</span></span><br><span class="line">6</span><br><span class="line">root@nanxi:~<span class="comment"># let n=2/1</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $n</span></span><br><span class="line">2</span><br><span class="line">root@nanxi:~<span class="comment"># let n=2-1</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $n</span></span><br><span class="line">1</span><br><span class="line"></span><br><span class="line">-------------------------------</span><br><span class="line"><span class="comment"># bc命令</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo 1+1*2-1*3|bc</span></span><br><span class="line">0</span><br><span class="line">root@nanxi:~<span class="comment"># echo 3+(1+1)*2-1*3|bc</span></span><br><span class="line">4</span><br><span class="line"></span><br><span class="line">或者使用awk……</span><br></pre></td></tr></table></figure><h2 id="三、巧用seq命令">三、巧用seq命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用方法</span></span><br><span class="line">1. <span class="built_in">seq</span> + 选项 + 尾数(默认输出从1开始的整数)</span><br><span class="line">2. <span class="built_in">seq</span> + 选项 + 首数 + 尾数</span><br><span class="line">3. <span class="built_in">seq</span> + 选项 + 首数 + 增量 + 尾数</span><br><span class="line"></span><br><span class="line">-f, --format=FORMAT use <span class="built_in">printf</span> style floating-point FORMAT</span><br><span class="line">-s, --separator=STRING use STRING to separate numbers (default: \n)</span><br><span class="line">-w, --equal-width equalize width by padding with leading zeroes</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 实例</span></span><br><span class="line">root@nanxi:~<span class="comment"># seq 5</span></span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">root@nanxi:~<span class="comment"># seq -s + 5</span></span><br><span class="line">1+2+3+4+5</span><br><span class="line">root@nanxi:~<span class="comment"># seq -s '' 5</span></span><br><span class="line">12345</span><br><span class="line">root@nanxi:~<span class="comment"># seq -s ' ' 5</span></span><br><span class="line">1 2 3 4 5</span><br><span class="line">root@nanxi:~<span class="comment"># seq -f "%2g" 8 12</span></span><br><span class="line"> 8</span><br><span class="line"> 9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">root@nanxi:~<span class="comment"># seq -f "%02g" 8 12</span></span><br><span class="line">08</span><br><span class="line">09</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">root@nanxi:~<span class="comment"># seq -s ' ' -f "%02g" 8 12</span></span><br><span class="line">08 09 10 11 12</span><br><span class="line">root@nanxi:~<span class="comment"># seq -f "nanxi%02g" 8 12</span></span><br><span class="line">nanxi08</span><br><span class="line">nanxi09</span><br><span class="line">nanxi10</span><br><span class="line">nanxi11</span><br><span class="line">nanxi12</span><br><span class="line">root@nanxi:~<span class="comment"># seq -f "%.2f" 9 11</span></span><br><span class="line">9.00</span><br><span class="line">10.00</span><br><span class="line">11.00</span><br><span class="line">root@nanxi:~<span class="comment"># seq -w 2 2 10</span></span><br><span class="line">02</span><br><span class="line">04</span><br><span class="line">06</span><br><span class="line">08</span><br><span class="line">10</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 从1加到100</span></span><br><span class="line">root@nanxi:~<span class="comment"># seq -s + 100|bc</span></span><br><span class="line">5050</span><br><span class="line"></span><br><span class="line"><span class="comment"># 批量创建删除用户</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#seq -f 'useradd nanxi%g' 5|bash</span></span><br><span class="line">[root@ceshi ~]<span class="comment">#seq -f 'userdel -r nanxi%g' 5|bash</span></span><br><span class="line"></span><br><span class="line">妙用有很多哦……</span><br></pre></td></tr></table></figure><h2 id="四、wait命令">四、wait命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># wait命令表示等待,脚本里面的wait表示等待上面所有命令执行的进程结束后方可继续</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `<span class="built_in">seq</span> 254`</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">{</span><br><span class="line">ip=10.0.0.<span class="variable">$i</span></span><br><span class="line">ping -c1 -W1 <span class="variable">$ip</span> &>/dev/null</span><br><span class="line">[ $? -eq 0 ] && <span class="built_in">echo</span> <span class="string">"<span class="variable">$ip</span> 在线"</span></span><br><span class="line">} &</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="built_in">wait</span></span><br><span class="line"><span class="built_in">echo</span> <span class="keyword">done</span>............</span><br></pre></td></tr></table></figure><h2 id="五、trap命令">五、trap命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># trap命令的使用</span></span><br><span class="line"><span class="comment"># 使用方法: trap [command] [signal]</span></span><br><span class="line"></span><br><span class="line">[root@ceshi ~]<span class="comment">#trap -l</span></span><br><span class="line"> 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP</span><br><span class="line"> 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL10) SIGUSR1</span><br><span class="line">11) SIGSEGV12) SIGUSR213) SIGPIPE14) SIGALRM15) SIGTERM</span><br><span class="line">16) SIGSTKFLT17) SIGCHLD18) SIGCONT19) SIGSTOP20) SIGTSTP</span><br><span class="line">21) SIGTTIN22) SIGTTOU23) SIGURG24) SIGXCPU25) SIGXFSZ</span><br><span class="line">26) SIGVTALRM27) SIGPROF28) SIGWINCH29) SIGIO30) SIGPWR</span><br><span class="line">31) SIGSYS34) SIGRTMIN35) SIGRTMIN+136) SIGRTMIN+237) SIGRTMIN+3</span><br><span class="line">38) SIGRTMIN+439) SIGRTMIN+540) SIGRTMIN+641) SIGRTMIN+742) SIGRTMIN+8</span><br><span class="line">43) SIGRTMIN+944) SIGRTMIN+1045) SIGRTMIN+1146) SIGRTMIN+1247) SIGRTMIN+13</span><br><span class="line">48) SIGRTMIN+1449) SIGRTMIN+1550) SIGRTMAX-1451) SIGRTMAX-1352) SIGRTMAX-12</span><br><span class="line">53) SIGRTMAX-1154) SIGRTMAX-1055) SIGRTMAX-956) SIGRTMAX-857) SIGRTMAX-7</span><br><span class="line">58) SIGRTMAX-659) SIGRTMAX-560) SIGRTMAX-461) SIGRTMAX-362) SIGRTMAX-2</span><br><span class="line">63) SIGRTMAX-164) SIGRTMAX</span><br><span class="line"></span><br><span class="line">使用方式如下</span><br><span class="line">1、 <span class="built_in">trap</span> <span class="string">"commands"</span> signal-list</span><br><span class="line"><span class="comment"># 当脚本收到signal-list清单内列出的信号时,trap命令执行双引号中的命令。</span></span><br><span class="line">2、 <span class="built_in">trap</span> signal-list</span><br><span class="line"><span class="comment"># trap不指定任何命令,接受信号的默认操作,默认操作是结束进程的运行。</span></span><br><span class="line">3、 <span class="built_in">trap</span> <span class="string">" "</span> signal-list</span><br><span class="line"><span class="comment"># trap命令指定一个空命令串,允许忽视信号,我们最常用到的就是这一种。</span></span><br></pre></td></tr></table></figure><p><font color=red><strong>注意: 脚本程序通常是以从上到下的顺序解释执行的,所以必须在你想保护的那部分代码以前指定trap命令。</strong></font></p><div class='spoiler collapsed'> <div class='spoiler-title'> SIGNAL了解 </div> <div class='spoiler-content'> <p>SIGHUP------------------------>终止进程 终端线路挂断</p><p>SIGINT------------------------->终止进程 中断进程</p><p>SIGQUIT----------------------->建立CORE文件 终止进程,并且生成core文件</p><p>SIGILL-------------------------->建立CORE文件 非法指令</p><p>SIGTRAP---------------------->建立CORE文件 跟踪自陷</p><p>SIGBUS------------------------>建立CORE文件 总线错误</p><p>SIGSEGV---------------------->建立CORE文件 段非法错误</p><p>SIGFPE------------------------->建立CORE文件 浮点异常</p><p>SIGIOT------------------------->建立CORE文件 执行I/O自陷</p><p>SIGKILL------------------------>终止进程 杀死进程</p><p>SIGPIPE------------------------>终止进程 向一个没有读进程的管道写数据</p><p>SIGALARM-------------------->终止进程 计时器到时</p><p>SIGTERM---------------------->终止进程 软件终止信号</p><p>SIGSTOP----------------------->停止进程 非终端来的停止信号</p><p>SIGTSTP------------------------>停止进程 终端来的停止信号</p><p>SIGCONT----------------------->忽略信号 继续执行一个停止的进程</p><p>SIGURG------------------------->忽略信号 I/O紧急信号</p><p>SIGIO---------------------------->忽略信号 描述符上可以进行I/O</p><p>SIGCHLD----------------------->忽略信号 当子进程停止或退出时通知父进程</p><p>SIGTTOU----------------------->停止进程 后台进程写终端</p><p>SIGTTIN------------------------>停止进程 后台进程读终端</p><p>SIGXGPU----------------------->终止进程 CPU时限超时</p><p>SIGXFSZ------------------------>终止进程 文件长度过长</p><p>SIGWINCH--------------------->忽略信号 窗口大小发生变化</p><p>SIGPROF----------------------->终止进程 统计分布图用计时器到时</p><p>SIGUSR1----------------------->终止进程 用户定义信号1</p><p>SIGUSR2----------------------->终止进程 用户定义信号2</p><p>SIGVTALRM------------------->终止进程 虚拟计时器到时</p> </div></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><blockquote><p><em><strong>优秀的公司不相信卓越,只相信不断改进和不断变化。</strong></em></p><p align="right">——汤姆.彼得斯</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell编程基础知识补充</center></h1>
<blockquote>
<p>🍟 sed替换文本中某行的某个字符 <code>sed -i '/^admin/s/100/22/' nanxi.txt</code> 把nanxi.txt中以admin开头的行里面的100替换成22。</p>
</blockquote>
<h2 id="一、while读取文本">一、while读取文本</h2>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240125205335929.png" alt="image-20240125205335929"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>南汐的云端小铺</title>
<link href="https://smallflames.github.io/2024/01/25/%E5%8D%97%E6%B1%90%E7%9A%84%E4%BA%91%E7%AB%AF%E5%B0%8F%E9%93%BA/"/>
<id>https://smallflames.github.io/2024/01/25/%E5%8D%97%E6%B1%90%E7%9A%84%E4%BA%91%E7%AB%AF%E5%B0%8F%E9%93%BA/</id>
<published>2024-01-25T10:03:10.000Z</published>
<updated>2024-01-28T11:07:56.220Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>南汐的云端小铺(Shell版)</center></h1><blockquote><p>🎃 突发奇想,怎么样让大家快速的掌握编程里面的判断和循环呢,理论知识只是基础,关键在于自己的代码手感和编程的思维,无他,学习编程我们只需要记住一点:<font color=red><strong>百炼成神</strong></font>。其余,底层的原理和架构是我们高纬度的空间,天梯是不可僭越的,需要一步一步攀登!</p></blockquote><table><tr><td bgcolor=#000000><font color="white">功能介绍</font></td></tr></table><font color="#fc5531">▚ 介绍 :南汐的云端小铺暂时暂时只写了一个板块(今日上新),其他板块大家可自行拓展,或者南汐改天可以再完善一下,目前实现的功能有:菜单显示、商品购买、用户登录验证与注册、账户余额充值与显示、购物账单打印。细节大家可自行测试,金无足赤,人无完人,如有Bug,欢迎大家在评论区反馈,或者分享建议哦~ヾ(•ω•`)o</font><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxi_shell.gif" alt="nanxi_shell"></p><div class="tips"><p>提示:脚本默认会创建初始用户 <code>nanxi</code> 密码:<code>nanxi123</code>,当然大家也可以自行注册试玩,或者拓展~</p></div><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">脚本实现</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment">#Author: nanxi</span></span><br><span class="line"><span class="comment">#Date: 2024-1-24</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#显示菜单</span></span><br><span class="line"><span class="function"><span class="title">home</span></span>(){</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"------------------------------------------------"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 1) 今日上新 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 2) 独家好物 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 3) 云端小屋 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"----------- 欢迎来到南汐的云端小铺 -----------"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">new</span></span>(){</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 1)月亮种子 2)寒冰宝剑 3)星辰纱衣 4)青丘幼狐 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| ¥:666 ¥:777 ¥:888 ¥:999 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">cloud</span></span>(){</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 1)秋叶原 2)时光海 3)吟雪界 4)彩虹桥 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">exclusive</span></span>(){</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| 1)时光机器 2)九龙蟠桃 3)青春不老液 4)忘忧草 |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"| |"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-----------------------------------------------"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">back=<span class="string">"(0返回店铺首页)"</span></span><br><span class="line">flag=0</span><br><span class="line">logfile=/tmp/.nanxi.log</span><br><span class="line">username=null</span><br><span class="line">password=null</span><br><span class="line"></span><br><span class="line"><span class="comment">#功能实现</span></span><br><span class="line"><span class="function"><span class="title">shop</span></span>(){</span><br><span class="line"> <span class="built_in">sum</span>=0</span><br><span class="line"> array=()</span><br><span class="line"> i=0</span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"1、2、3、4为商品指令,6为购买指令,0返回店铺首页"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"亲亲,请输入您的购买指令<span class="variable">$back</span>: "</span> num</span><br><span class="line"> check_in</span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$num</span> <span class="keyword">in</span></span><br><span class="line"> 0)</span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> ;;</span><br><span class="line"> 1)</span><br><span class="line"> <span class="built_in">let</span> i++</span><br><span class="line"> <span class="built_in">sum</span>=$(<span class="built_in">expr</span> <span class="variable">$sum</span> + 666)</span><br><span class="line"> array[<span class="variable">$i</span>]=<span class="string">"月亮种子"</span></span><br><span class="line"> ;;</span><br><span class="line"> 2)</span><br><span class="line"> <span class="built_in">let</span> i++</span><br><span class="line"> <span class="built_in">sum</span>=$(<span class="built_in">expr</span> <span class="variable">$sum</span> + 777)</span><br><span class="line"> array[<span class="variable">$i</span>]=<span class="string">"寒冰宝剑"</span></span><br><span class="line"> ;;</span><br><span class="line"> 3)</span><br><span class="line"> <span class="built_in">let</span> i++</span><br><span class="line"> <span class="built_in">sum</span>=$(<span class="built_in">expr</span> <span class="variable">$sum</span> + 888)</span><br><span class="line"> array[<span class="variable">$i</span>]=<span class="string">"星辰纱衣"</span></span><br><span class="line"> ;;</span><br><span class="line"> 4)</span><br><span class="line"> <span class="built_in">let</span> i++</span><br><span class="line"> <span class="built_in">sum</span>=$(<span class="built_in">expr</span> <span class="variable">$sum</span> + 999)</span><br><span class="line"> array[<span class="variable">$i</span>]=<span class="string">"青丘幼狐"</span></span><br><span class="line"> ;;</span><br><span class="line"> 6)</span><br><span class="line"> <span class="keyword">if</span> [ <span class="variable">$flag</span> -ne 1 ];<span class="keyword">then</span></span><br><span class="line"> logon</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> pay</span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="built_in">continue</span></span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">check_in</span></span>(){</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="keyword">if</span> ! [[ <span class="variable">$num</span> =~ ^[012346]$ ]];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"购买指令异常,您需要重新输入!!!"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">elif</span> [ <span class="variable">$num</span> -gt 6 ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"购买指令异常,您需要重新输入!!!"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">logon</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> [ ! -f <span class="variable">$logfile</span> ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">touch</span> <span class="variable">$logfile</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"nanxi\t\tnanxi123\t\t9999999999"</span> > <span class="variable">$logfile</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">if</span> [ <span class="variable">$flag</span> -eq 0 ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"此操作需要您登录账户,是否登录(y/n): "</span> <span class="built_in">log</span></span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$log</span> <span class="keyword">in</span></span><br><span class="line"> <span class="built_in">yes</span>|y)</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"请输入您的账户: "</span> username</span><br><span class="line"> <span class="built_in">read</span> -s -p <span class="string">"请输入您的密码: "</span> password</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> check_user</span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="built_in">break</span> 1</span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">check_user</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> ! egrep -w <span class="string">"<span class="variable">$username</span>|<span class="variable">$password</span>"</span> <span class="variable">$logfile</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"未检测到当前用户,或当前账户状态异常!!!"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"您是否需要注册用户(y/n): "</span> sta</span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$sta</span> <span class="keyword">in</span></span><br><span class="line"> <span class="built_in">yes</span>|y)</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"请输入您要注册的账户名: "</span> username</span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"请设置您账户的对应密码: "</span> password</span><br><span class="line"> <span class="keyword">if</span> [[ <span class="variable">$username</span> =~ ^[0-Z_]+$ ]] && [ <span class="variable">${#username}</span> -ge 8 -a <span class="variable">${#username}</span> -le 12 ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"用户只能由数字、字母、下划线组合,且长度为8-12位~"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">continue</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">if</span> awk <span class="string">'{print $1}'</span> <span class="variable">$logfile</span> |grep <span class="variable">$username</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m账户名已存在,请重新输入...\e[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">continue</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"<span class="variable">$username</span>\t\t<span class="variable">$password</span>\t\t0"</span> >> <span class="variable">$logfile</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="built_in">break</span> 2</span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">if</span> egrep -w <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="keyword">if</span> egrep -w <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|grep -w <span class="string">"<span class="variable">$password</span>"</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> logon_success</span><br><span class="line"> flag=1</span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m\033[05m密码输入错误!!!\e[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">if</span> egrep -w <span class="string">"<span class="variable">$password</span>"</span> <span class="variable">$logfile</span>|grep -w <span class="string">"<span class="variable">$username</span> "</span> >/dev/null;<span class="keyword">then</span></span><br><span class="line"> logon_success</span><br><span class="line"> flag=1</span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m\033[05m用户名输入错误!!!\e[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">logon_success</span></span>(){</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t登录成功\t\t---------"</span></span><br><span class="line"> egrep -w <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|awk <span class="string">'{print $0}'</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t龙年大吉\t\t---------"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">pay</span></span>(){</span><br><span class="line"> value=$(egrep -w <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|awk <span class="string">'{print $3}'</span>)</span><br><span class="line"> <span class="keyword">if</span> [ <span class="variable">$value</span> -ge <span class="variable">$sum</span> ];<span class="keyword">then</span></span><br><span class="line"> bill</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m您的账户余额不足,请及时充值!!!\e[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"是否选择充值(y/n): "</span> recharge</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$recharge</span> <span class="keyword">in</span></span><br><span class="line"> <span class="built_in">yes</span>|y)</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"请输入充值金额,建议账户余额至少¥<span class="variable">$sum</span>: "</span> coin</span><br><span class="line"> <span class="keyword">if</span> [[ <span class="variable">$coin</span> =~ ^[0-9]+$ ]];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> surplus=$(<span class="built_in">expr</span> $(grep <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|awk <span class="string">'{print $3}'</span>) + <span class="variable">$coin</span>)</span><br><span class="line"> line=$(grep <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span> -n |awk -F: <span class="string">'{print $1}'</span>)</span><br><span class="line"> sed -i <span class="string">"<span class="variable">${line}</span>c <span class="variable">$username</span> <span class="variable">$password</span> <span class="variable">$surplus</span>"</span> <span class="variable">$logfile</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"充值成功,本次充值金额为: \033[1;32m ¥<span class="variable">$coin</span> \033[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[1;31m参数不合法,请重新输入!!!\e[0m"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">continue</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> pay</span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">bill</span></span>(){</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> price=null</span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t您的账单\t\t---------"</span></span><br><span class="line"> <span class="keyword">for</span> a <span class="keyword">in</span> <span class="variable">${array[@]}</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="keyword">if</span> [ <span class="string">"<span class="variable">$a</span>"</span> = <span class="string">"月亮种子"</span> ];<span class="keyword">then</span></span><br><span class="line"> price=666</span><br><span class="line"> <span class="keyword">elif</span> [ <span class="string">"<span class="variable">$a</span>"</span> = <span class="string">"寒冰宝剑"</span> ];<span class="keyword">then</span></span><br><span class="line"> price=777</span><br><span class="line"> <span class="keyword">elif</span> [ <span class="string">"<span class="variable">$a</span>"</span> = <span class="string">"星辰纱衣"</span> ];<span class="keyword">then</span></span><br><span class="line"> price=888</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> price=999</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"| <span class="variable">$a</span> \t\t\t\t\t ¥<span class="variable">$price</span>|"</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"| \t\t\t\t\t |"</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"| 本次消费总计: \t\t\t\t ¥<span class="variable">$sum</span>|"</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t感谢购买\t\t---------"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"检测到支付成功,是否显示您当前的账户余额(y/n): "</span> remainder</span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$remainder</span> <span class="keyword">in</span></span><br><span class="line"> <span class="built_in">yes</span>|y)</span><br><span class="line"> show</span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">show</span></span>(){</span><br><span class="line"> surplus=$(<span class="built_in">expr</span> $(grep <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|awk <span class="string">'{print $3}'</span>) - <span class="variable">$sum</span>)</span><br><span class="line"> line=$(grep <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span> -n |awk -F: <span class="string">'{print $1}'</span>)</span><br><span class="line"> sed -i <span class="string">"<span class="variable">${line}</span>c <span class="variable">$username</span><span class="variable">$password</span><span class="variable">$surplus</span>"</span> <span class="variable">$logfile</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t当前余额\t\t---------"</span></span><br><span class="line"> egrep -w <span class="string">"<span class="variable">$username</span>"</span> <span class="variable">$logfile</span>|awk <span class="string">'{print $0}'</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"---------\t\t再次感谢\t\t---------"</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">main</span></span>(){</span><br><span class="line"><span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"> home</span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">read</span> -p <span class="string">"亲亲,输入您的指令<span class="variable">$back</span>: "</span> num</span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$num</span> <span class="keyword">in</span></span><br><span class="line"> 0)</span><br><span class="line"> home</span><br><span class="line"> ;;</span><br><span class="line"> 1)</span><br><span class="line"> new</span><br><span class="line"> shop</span><br><span class="line"> ;;</span><br><span class="line"> 2)</span><br><span class="line"> exclusive</span><br><span class="line"> ;;</span><br><span class="line"> 3)</span><br><span class="line"> cloud</span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> <span class="built_in">continue</span></span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line">}</span><br><span class="line">main</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=519136840&auto=1&height=66"></iframe><blockquote><p><em><strong>在这个世界上,不是所有合理的和美好的都能按照自己的愿望存在或实现。</strong></em></p><p align="right">——路遥</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>南汐的云端小铺(Shell版)</center></h1>
<blockquote>
<p>🎃 突发奇想,怎么样让大家快速的掌握编程里面的判断和循环呢,理论知识只是基础,关键在于自己的代码手感和编程的思维,无他,学习编程我们只需要记住一点:<font color=red><strong>百炼成神</strong></font>。其余,底层的原理和架构是我们高纬度的空间,天梯是不可僭越的,需要一步一步攀登!</p>
</blockquote>
<table><tr><td bgcolor=#000000><font color="white">功能介绍</font></td></tr></table>
<font color="#fc5531">▚ 介绍 :南汐的云端小铺暂时暂时只写了一个板块(今日上新),其他板块大家可自行拓展,或者南汐改天可以再完善一下,目前实现的功能有:菜单显示、商品购买、用户登录验证与注册、账户余额充值与显示、购物账单打印。细节大家可自行测试,金无足赤,人无完人,如有Bug,欢迎大家在评论区反馈,或者分享建议哦~ヾ(•ω•`)o</font>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxi_shell.gif" alt="nanxi_shell"></p>
<div class="tips">
<p>提示:脚本默认会创建初始用户 <code>nanxi</code> 密码:<code>nanxi123</code>,当然大家也可以自行注册试玩,或者拓展~</p>
</div></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell脚本篇003</title>
<link href="https://smallflames.github.io/2024/01/24/Shell%E8%84%9A%E6%9C%AC%E7%AF%87003/"/>
<id>https://smallflames.github.io/2024/01/24/Shell%E8%84%9A%E6%9C%AC%E7%AF%87003/</id>
<published>2024-01-24T08:55:47.000Z</published>
<updated>2024-01-28T11:08:51.795Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell脚本篇003</center></h1><table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table><font color="#fc5531">▚ 需求 :写一个网卡检测脚本,用来监测网卡状态是否正常,并以内容的方式输出至屏幕。</font><p><font color="#7183ff">▚ 提示 :网卡的状态正常与否,可以检测单位时间内网卡的进出流量来判断。</font></p><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240124165816497.png" alt="image-20240124165816497"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">脚本实现</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment">#Author: nanxi</span></span><br><span class="line"><span class="comment">#Date: 2024-1-24</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#判断命令是否存在</span></span><br><span class="line"><span class="keyword">if</span> ! <span class="built_in">which</span> sar > /dev/null;<span class="keyword">then</span></span><br><span class="line"> yum -y install sysstat > /dev/null</span><br><span class="line"> [ $? -ne 0 ] && <span class="built_in">echo</span> <span class="string">"抱歉,sar 命令安装失败,您可以尝试手动安装~"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#服务状态判断</span></span><br><span class="line"><span class="function"><span class="title">check_net</span></span>(){</span><br><span class="line"> <span class="comment">#监测网卡流量</span></span><br><span class="line"> <span class="built_in">echo</span> -ne <span class="string">"网卡状态监测中...\t\t"</span></span><br><span class="line"> sar -n DEV 1 10|awk <span class="string">'/^Average/'</span>|grep <span class="string">'eth0'</span> > /tmp/eth0.<span class="built_in">log</span></span><br><span class="line"> n1=$(awk <span class="string">'{print $5}'</span> /tmp/eth0.<span class="built_in">log</span> | sed <span class="string">'s/\.//g'</span>)</span><br><span class="line"> n2=$(awk <span class="string">'{print $6}'</span> /tmp/eth0.<span class="built_in">log</span> | sed <span class="string">'s/\.//g'</span>)</span><br><span class="line"> <span class="built_in">rm</span> -rf /tmp/eth0.<span class="built_in">log</span></span><br><span class="line"> <span class="comment">#监测结果输出</span></span><br><span class="line"> <span class="keyword">if</span> [ <span class="variable">$n1</span> = 000 ] && [ <span class="variable">$n2</span> = 000 ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[41;37m\033[05m网卡状态异常\e[0m"</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span> -e <span class="string">"\e[42;37m网卡状态正常\e[0m"</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">check</span></span>(){</span><br><span class="line"> count=<span class="variable">$1</span></span><br><span class="line"> <span class="keyword">if</span> [[ <span class="variable">$count</span> =~ ^[0-9]+$ ]];<span class="keyword">then</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> $(<span class="built_in">seq</span> <span class="variable">$count</span>)</span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> check_net</span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">case</span> <span class="variable">$count</span> <span class="keyword">in</span></span><br><span class="line"> loop)</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="built_in">let</span> i++</span><br><span class="line"> max=10</span><br><span class="line"> check_net</span><br><span class="line"> <span class="keyword">if</span> [ <span class="variable">$i</span> -ge <span class="variable">$max</span> ];<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">break</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"> <span class="keyword">done</span></span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> check_net</span><br><span class="line"> <span class="keyword">esac</span></span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"><span class="built_in">read</span> -p <span class="string">"默认执行一次,您可以指定检测次数: "</span> count</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"------------- 开始进行网卡检测 -------------"</span></span><br><span class="line">check <span class="variable">$count</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"---- 本次检测结束,感谢您使用南汐的脚本 ----"</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><blockquote><p><em><strong>了解面对逆境,远比如何接受顺境重要得多。</strong></em></p><p align="right">——马丁·赛力格曼</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell脚本篇003</center></h1>
<table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table>
<font color="#fc5531">▚ 需求 :写一个网卡检测脚本,用来监测网卡状态是否正常,并以内容的方式输出至屏幕。</font>
<p><font color="#7183ff">▚ 提示 :网卡的状态正常与否,可以检测单位时间内网卡的进出流量来判断。</font></p>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240124165816497.png" alt="image-20240124165816497"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell脚本篇002</title>
<link href="https://smallflames.github.io/2024/01/23/Shell%E8%84%9A%E6%9C%AC%E7%AF%87002/"/>
<id>https://smallflames.github.io/2024/01/23/Shell%E8%84%9A%E6%9C%AC%E7%AF%87002/</id>
<published>2024-01-23T12:43:03.000Z</published>
<updated>2024-01-25T11:30:22.278Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell脚本篇002</center></h1><table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table><font color="#fc5531">▚ 需求 :写一个巡检脚本,用来监测系统各项服务的状态情况,并以内容的方式输出至屏幕。</font><table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123215829275.png" alt="image-20240123215829275"></p><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">实现脚本</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment">#Author: nanxi</span></span><br><span class="line"><span class="comment">#Date: 2024-1-23</span></span><br><span class="line"></span><br><span class="line">. /etc/init.d/functions</span><br><span class="line"><span class="comment">#检查使用命令是否存在</span></span><br><span class="line"><span class="function"><span class="title">check_com</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> ! <span class="built_in">which</span> pgrep > /dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"抱歉,脚本执行所需的命令(pgrep)不存在!"</span></span><br><span class="line"> <span class="built_in">exit</span> 1</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> ! <span class="built_in">which</span> ss > /dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"抱歉,脚本执行所需的命令(ss)不存在!"</span></span><br><span class="line"> <span class="built_in">exit</span> 1</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">#检查服务进程是否存在</span></span><br><span class="line"><span class="function"><span class="title">check_ps</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> pgrep <span class="variable">$1</span> > /dev/null;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">return</span> 0</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">return</span> 1</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">#检查端口是否处于监听状态</span></span><br><span class="line"><span class="function"><span class="title">check_port</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> ss -lnp | grep <span class="string">":<span class="variable">$1</span> "</span> > /dev/null ;<span class="keyword">then</span></span><br><span class="line"> <span class="built_in">return</span> 0</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">return</span> 1</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">#判断服务状态</span></span><br><span class="line"><span class="function"><span class="title">check_srv</span></span>(){</span><br><span class="line"> <span class="keyword">if</span> check_ps <span class="variable">$1</span> && check_port <span class="variable">$2</span> ;<span class="keyword">then</span></span><br><span class="line"> action <span class="string">"<span class="variable">$1</span> 服务状态"</span> /bin/true</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> action <span class="string">"<span class="variable">$1</span> 服务状态"</span> /bin/false</span><br><span class="line"> <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">check_com</span><br><span class="line">check_srv nginx 80</span><br><span class="line">check_srv sshd 22</span><br><span class="line">check_srv java 8080</span><br><span class="line">check_srv php 9000</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><blockquote><p><em><strong>当人们感到自己没有能力获得巨大的成功时,他们会鄙视伟大的目标。</strong></em></p><p align="right">——沃维纳格</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell脚本篇002</center></h1>
<table><tr><td bgcolor=#000000><font color="white">脚本需求</font></td></tr></table>
<font color="#fc5531">▚ 需求 :写一个巡检脚本,用来监测系统各项服务的状态情况,并以内容的方式输出至屏幕。</font>
<table><tr><td bgcolor=#000000><font color="white">实现效果</font></td></tr></table>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123215829275.png" alt="image-20240123215829275"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Shell编程知识补充(二)</title>
<link href="https://smallflames.github.io/2024/01/23/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%BA%8C%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/23/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%BA%8C%EF%BC%89/</id>
<published>2024-01-23T06:38:57.000Z</published>
<updated>2024-01-25T14:02:49.254Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell编程基础知识补充</center></h1><blockquote><p>✨ Shell 这个单词的原意是“外壳”,跟 kernel(内核)相对应,比喻内核外面的一层,即用户跟内核交互的对话界面。<br>🎉 Shell 是一个程序,提供一个与用户对话的环境。这个环境只有一个命令提示符,让用户从键盘输入命令,所以又称为命令行环境(command line interface,简写为 CLI)。<br>🎁 Shell 是一个命令解释器,解释用户输入的命令。其次,Shell 是一个工具箱,提供了各种小工具,供用户方便地使用操作系统的功能。</p></blockquote><h2 id="font-color-red-▚-u-01-u-拓展:echo趣味玩法-font"><font color=red>▚ <u>01</u> 拓展:echo趣味玩法</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># echo 命令概述</span></span><br><span class="line">默认将输入的字符串送往标准输出,输出的字符串间以空白字符隔开, 并在最后加上换行符;</span><br><span class="line"><span class="comment"># echo 命令语法</span></span><br><span class="line"><span class="built_in">echo</span> 【选项参数】 【字符串】</span><br><span class="line">-n <span class="comment"># 取消自动换行</span></span><br><span class="line">-e <span class="comment"># 打开反斜杠ESC转义</span></span><br><span class="line">-E <span class="comment"># 取消转义 (默认)</span></span><br><span class="line"></span><br><span class="line">关于反斜杠的转义,详细参数如下:</span><br></pre></td></tr></table></figure><span id="more"></span><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123163236254.png" alt="image-20240123163236254"></p><blockquote><p>---------------------------背景颜色范围:40-49------------------------<br>[root@localhost ~]# echo -e “\e[40;37m黑底白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:black;">黑底白字</span></span><br>[root@localhost ~]# echo -e “\e[41;37m深红白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:red;">深红白字</span></span><br>[root@localhost ~]# echo -e “\e[42;37m绿底白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:green;">绿底白字</span></span><br>[root@localhost ~]# echo -e “\e[43;37m黄底白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:yellow;">黄底白字</span></span><br>[root@localhost ~]# echo -e “\e[44;37m蓝底白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:blue;">蓝底白字</span></span><br>[root@localhost ~]# echo -e “\e[45;37m紫底白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:purple;">紫底白字</span></span><br>[root@localhost ~]# echo -e “\e[46;37m深绿白字 \e[0m”<br><span style="color:#FFFFFF;"><span style="background-color:#003221;">深绿白字</span></span><br>[root@localhost ~]# echo -e “\e[47;30m白底黑字 \e[0m”<br><span style="color:#000000;"><span style="background-color:#FFFFFF;">白底黑字</span></span><br>------------------------字体颜色范围:30-39------------------------<br>[root@localhost ~]# echo -e “\e[1;30m黑色字体 \e[0m”<br><font color="black">黑色字体</font><br>[root@localhost ~]# echo -e “\e[1;31m红色字体 \e[0m”<br><font color="red">红色字体</font><br>[root@localhost ~]# echo -e “\e[1;32m绿色字体 \e[0m”<br><font color="green">绿色字体</font><br>[root@localhost ~]# echo -e “\e[1;33m黄色字体 \e[0m”<br><font color="yellow">黄色字体</font><br>[root@localhost ~]# echo -e “\e[1;34m蓝色字体 \e[0m”<br><font color="blue">蓝色字体</font><br>[root@localhost ~]# echo -e “\e[1;35m紫色字体 \e[0m”<br><font color="purple">紫色字体</font><br>[root@localhost ~]# echo -e “\e[1;36m深绿字体 \e[0m”<br><font color="#003221">深绿字体</font><br>[root@localhost ~]# echo -e “\e[1;37m白色字体 \e[0m”<br><font color="white">白色字体</font></p></blockquote><h2 id="font-color-red-▚-u-02-u-拓展:action系统显示-font"><font color=red>▚ <u>02</u> 拓展:action系统显示</font></h2><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123170224054.png" alt="image-20240123170224054"></p><div class="warning"><p><font color=blue><strong>注意:本次演示使用的是CentOS 7.9 ,Ubantu版本里面并没有/etc/init.d/functions这个相关文件</strong></font></p></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123171538833.png" alt="image-20240123171538833"></p><h2 id="font-color-red-▚-u-03-u-拓展:变量子串妙用-font"><font color=red>▚ <u>03</u> 拓展:变量子串妙用</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># url=www.baidu.com</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $url</span></span><br><span class="line">www.baidu.com</span><br><span class="line"></span><br><span class="line"><span class="comment"># "#" 表示从前向后匹配</span></span><br><span class="line">-------------------------------</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url#www.}</span></span><br><span class="line">baidu.com</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url#*.}</span></span><br><span class="line">baidu.com</span><br><span class="line"><span class="comment"># 两个#表示贪婪匹配,一直匹配到最后一个(点).</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url##*.}</span></span><br><span class="line">com</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url#*.*.}</span></span><br><span class="line">com</span><br><span class="line"></span><br><span class="line"><span class="comment"># "%" 表示从前向后匹配</span></span><br><span class="line">-------------------------------</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url%.*}</span></span><br><span class="line">www.baidu</span><br><span class="line"><span class="comment"># 两个%表示贪婪匹配,一直匹配到第一个(点).</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo ${url%%.*}</span></span><br><span class="line">www</span><br></pre></td></tr></table></figure><div class="tips"><p><strong>取值小技巧</strong></p><p>[nanxi@web1 ~]#df -h<br>Filesys tem Size Used Avail Use% Mounted on<br>devtmpfs 911M 0 911M 0% /dev<br>tmpfs 922M 0 922M 0% /dev/shm<br>tmpfs 922M 18M 904M 2% /run<br>tmpfs 922M 0 922M 0% /sys/fs/cgroup<br>/dev/sda3 18G 3.2G 15G 18% /<br>/dev/sda1 197M 110M 88M 56% /boot<br>tmpfs 185M 0 185M 0% /run/user/1000<br>[nanxi@web1 ~]#used_disk=$(df -h |awk ‘//$/{print $(NF-1)}’)<br>[nanxi@web1 ~]#echo $used_disk<br>18%<br>[nanxi@web1 ~]#echo ${used_disk%%}<br>18</p></div><h2 id="font-color-red-▚-u-04-u-补充:匹配正则判断-font"><font color=red>▚ <u>04</u> 补充:匹配正则判断</font></h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[nanxi@web1 ~]<span class="comment">#[[ user =~ ^u ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [[ user =~ r$ ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [[ user =~ o$ ]] && echo 成立 || echo 不成立</span></span><br><span class="line">不成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [[ 100 =~ ^1 ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment">#[[ 100 =~ ^[0-9] ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [[ 12343242 =~ ^[0-9]+$ ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line"><span class="comment"># 多个正则比对使用 && (与) || (或)</span></span><br><span class="line">[nanxi@web1 ~]<span class="comment">#[[ 10 -eq 20 && 200 -gt 100 ]] && echo 成立 || echo 不成立</span></span><br><span class="line">不成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [[ 10 -eq 20 || 200 -gt 100 ]] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 多个整数比较使用 -a (and) -o (or)</span></span><br><span class="line">[nanxi@web1 ~]<span class="comment"># [ 10 -eq 10 -a 100 -gt 20 ] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br><span class="line">[nanxi@web1 ~]<span class="comment">#[ 10 -eq 11 -o 100 -gt 20 ] && echo 成立 || echo 不成立</span></span><br><span class="line">成立</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p> <div id="aplayer-CnFlTxac" class="aplayer aplayer-tag-marker" style="margin-bottom: 20px;"> <pre class="aplayer-lrc-content"></pre> </div> <script> var ap = new APlayer({ element: document.getElementById("aplayer-CnFlTxac"), narrow: false, autoplay: true, showlrc: false, music: { title: "Whale (鲸)", author: "Jannik", url: "http://cdn.zzxe.eu.org/music/Whale%20%28%E9%B2%B8%29.m4a", pic: "https://p2.music.126.net/97-CeOH6aysRsv1TYBA71Q==/109951164727131802.jpg", lrc: "" } }); window.aplayers || (window.aplayers = []); window.aplayers.push(ap); </script><blockquote><p><em><strong>以希望为生的人,将绝食而死。</strong></em></p><p align="right">——富兰克林(美国)</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell编程基础知识补充</center></h1>
<blockquote>
<p>✨ Shell 这个单词的原意是“外壳”,跟 kernel(内核)相对应,比喻内核外面的一层,即用户跟内核交互的对话界面。<br>
🎉 Shell 是一个程序,提供一个与用户对话的环境。这个环境只有一个命令提示符,让用户从键盘输入命令,所以又称为命令行环境(command line interface,简写为 CLI)。<br>
🎁 Shell 是一个命令解释器,解释用户输入的命令。其次,Shell 是一个工具箱,提供了各种小工具,供用户方便地使用操作系统的功能。</p>
</blockquote>
<h2 id="font-color-red-▚-u-01-u-拓展:echo趣味玩法-font"><font color=red>▚ <u>01</u> 拓展:echo趣味玩法</font></h2>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># echo 命令概述</span></span><br><span class="line">默认将输入的字符串送往标准输出,输出的字符串间以空白字符隔开, 并在最后加上换行符;</span><br><span class="line"><span class="comment"># echo 命令语法</span></span><br><span class="line"><span class="built_in">echo</span> 【选项参数】 【字符串】</span><br><span class="line">-n <span class="comment"># 取消自动换行</span></span><br><span class="line">-e <span class="comment"># 打开反斜杠ESC转义</span></span><br><span class="line">-E <span class="comment"># 取消转义 (默认)</span></span><br><span class="line"></span><br><span class="line">关于反斜杠的转义,详细参数如下:</span><br></pre></td></tr></table></figure></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>JumpServer开源堡垒机</title>
<link href="https://smallflames.github.io/2024/01/22/JumpServer%E7%AE%80%E8%BF%B0/"/>
<id>https://smallflames.github.io/2024/01/22/JumpServer%E7%AE%80%E8%BF%B0/</id>
<published>2024-01-22T12:48:23.000Z</published>
<updated>2024-01-23T11:54:03.795Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>JumpServer简述</center></h1><h1>一、JumpServer介绍</h1><p> JumpServer开源堡垒机是一款运维安全审计系统产品,提供身份验证、授权控制、账号管理、安全审计等功能支持,帮助企业快速构建运维安全审计能力。底层同样使用ansible管理其他服务主机,但是对于企业运维人员来说,我们最常用的便是它的管理、监察和审计功能。</p><p><img src="https://docs.jumpserver.org/zh/v3/img/index_02.png" alt="jumpserver.org)"></p><span id="more"></span><h1>二、JumpServer安装</h1><p> 关于软件的安装,南汐的习惯是无论Windows还是Linux安装软件均在官网进行(<a href="https://www.jumpserver.org/">JumpServer官网地址</a>),因为官网的资源是实时更新的,会不停的对出现的bug进行修复,这能为我们避免很多不必要的麻烦,而且官网一般情况下均提供帮助手册或者我们称官方文档,会大大的提高我们对软件的使用效率。<a href="https://docs.jumpserver.org/zh/v3/">JumpServer官方文档地址</a></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240122212330586.png" alt="image-20240122212330586"></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 一键在线安装,默认是最新版本,特殊需求可查询官方文档</span></span><br><span class="line"><span class="comment"># 安装时注意服务器配置尽可能高一些,官方也有关于硬件配置的简单说明,自己测试的话2核4G即可</span></span><br><span class="line">curl -sSL https://resource.fit2cloud.com/jumpserver/jumpserver/releases/latest/download/quick_start.sh | bash</span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装过程可能比较缓慢,请耐心等待~</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123190345095.png" alt="nanxijunmpserver"></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 默认下载到/opt,cd /opt/jumpserver-installer-v3.10.2</span></span><br><span class="line"><span class="comment"># 安装完成后会有很多的提示,比如服务管理方式、web访问地址、docker运行信息……</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动成功后,通过浏览器访问登录 JumpServer</span></span><br><span class="line">web页面地址: http://<JumpServer服务器IP地址>:<服务运行端口></span><br><span class="line"></span><br><span class="line"><span class="comment"># 默认管理员账户密码</span></span><br><span class="line">-----------------</span><br><span class="line">用户名: admin</span><br><span class="line">密 码: admin</span><br><span class="line">-----------------</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240123190926279.png" alt="image-20240123190926279"></p><h1>三、jumpServer界面</h1><p><img src="https://docs.jumpserver.org/zh/v3/img/online_install_01.png" alt="jumpserverweb)"></p><div class="warning"><p> <font color=red><strong>注意⚠️ :刚刚安装好JumpServer并进行第一次启动后,可能无法访问JumpServer的web页面,显示红色的502错误,不要着急,此时可能是cpu负载过高导致的,等待cpu负载降下来之后即可正常访问。</strong></font></p></div><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/Snipaste_2024-01-23_19-24-48.png" alt="Snipaste_2024-01-23_19-24-48"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/Snipaste_2024-01-23_19-30-21.png" alt="Snipaste_2024-01-23_19-30-21"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoot.png" alt="nanxiblogfoot"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=479598964&auto=1&height=66"></iframe><blockquote><p><em><strong>大多数人失去工作,是因为这些工作披着"辛勤"的外衣。</strong></em></p><p align="right">——爱迪生</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>JumpServer简述</center></h1>
<h1>一、JumpServer介绍</h1>
<p> JumpServer开源堡垒机是一款运维安全审计系统产品,提供身份验证、授权控制、账号管理、安全审计等功能支持,帮助企业快速构建运维安全审计能力。底层同样使用ansible管理其他服务主机,但是对于企业运维人员来说,我们最常用的便是它的管理、监察和审计功能。</p>
<p><img src="https://docs.jumpserver.org/zh/v3/img/index_02.png" alt="jumpserver.org)"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="软件应用" scheme="https://smallflames.github.io/tags/%E8%BD%AF%E4%BB%B6%E5%BA%94%E7%94%A8/"/>
</entry>
<entry>
<title>Shell编程知识补充(一)</title>
<link href="https://smallflames.github.io/2024/01/22/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%B8%80%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/22/Shell%E7%BC%96%E7%A8%8B%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%88%E4%B8%80%EF%BC%89/</id>
<published>2024-01-22T08:46:42.000Z</published>
<updated>2024-01-23T07:02:12.318Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Shell编程基础知识补充</center></h1><h1>一、执行脚本的方式</h1><p>1️⃣ 指定解释器执行 --------> <code>/bin/bash nanxi.sh</code> & <code>sh nanxi.sh</code> 默认解释器即为/bin/bash</p><p>2️⃣ 使用路径执行(需要执行权限+x)----> <code>./nanxi.sh</code> & <code>/scripts/nanxi.sh</code> 两种方式均可</p><p>3️⃣ 命令调用执行 -------------------------> <code>source /scripts/nanxi.sh</code> & <code>. nanxi.sh</code></p><div class="warning"><p><font color=red><strong>区别:方式1️⃣和方式2️⃣执行脚本调用的是子shell,而方式3️⃣执行脚本调用的是当前shell</strong></font></p></div><h1>二、环境变量的执行顺序</h1><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240122182252168.png" alt="image-20240122182252168"></p><span id="more"></span><h1>三、定义变量(特例)</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 时间是固定的</span></span><br><span class="line">root@nanxi:~<span class="comment"># time=$(date +%F-%H-%M-%S)</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $time</span></span><br><span class="line">2024-01-22-10-10-14</span><br><span class="line">root@nanxi:~<span class="comment"># echo $time</span></span><br><span class="line">2024-01-22-10-10-14</span><br><span class="line"><span class="comment"># 时间是不固定</span></span><br><span class="line">root@nanxi:~<span class="comment"># time="date +%F-%H-%M-%S"</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $time</span></span><br><span class="line"><span class="built_in">date</span> +%F-%H-%M-%S</span><br><span class="line">root@nanxi:~<span class="comment"># $time</span></span><br><span class="line">2024-01-22-10-10-39</span><br><span class="line">root@nanxi:~<span class="comment"># $time</span></span><br><span class="line">2024-01-22-10-10-40</span><br><span class="line">root@nanxi:~<span class="comment"># $time</span></span><br><span class="line">2024-01-22-10-10-41</span><br></pre></td></tr></table></figure><h1>四、Shell核心位置变量</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$0</span> <span class="comment"># 表示Shell脚本的名称</span></span><br><span class="line"><span class="variable">$n</span> <span class="comment"># 表示Shell脚本引用的第n个参数 从$1开始表示脚本的第1个参数 ${10} ${11}</span></span><br><span class="line"><span class="variable">$#</span> <span class="comment"># 表示Shell脚本传参的个数</span></span><br><span class="line">$? <span class="comment"># 表示上一个命令执行的结果,0表示成功,非0表示失败</span></span><br><span class="line"></span><br><span class="line">$$ <span class="comment"># 表示本次执行脚本的进程PID号,脚本内使用</span></span><br><span class="line">$! <span class="comment"># 表示上一次执行脚本的进程PID号,一般调试脚本时使用</span></span><br><span class="line">$* <span class="comment"># 表示所有参数,在循环体中加双引号表示将原参数作为一个参数输出</span></span><br><span class="line"><span class="variable">$@</span> <span class="comment"># 表示所有参数,在循环体中加双引号表示以原参数个数依次输出</span></span><br><span class="line"><span class="variable">$_</span> <span class="comment"># 表示上一条命令的最后一个参数</span></span><br></pre></td></tr></table></figure><h1>五、脚本传参方式</h1><h2 id="1、直接传参">1、直接传参</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># cat nanxi.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$1</span></span><br><span class="line">root@nanxi:~<span class="comment"># source nanxi.sh nanxi</span></span><br><span class="line">nanxi</span><br></pre></td></tr></table></figure><h2 id="2、赋值传参">2、赋值传参</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># cat nanxi.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">name=<span class="variable">$1</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"I love <span class="variable">$name</span>"</span></span><br><span class="line">root@nanxi:~<span class="comment"># source nanxi.sh nanxi</span></span><br><span class="line">I love nanxi</span><br></pre></td></tr></table></figure><h2 id="3、read读入">3、read读入</h2><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240122185148924.png" alt="image-20240122185148924"></p><div class='spoiler collapsed'> <div class='spoiler-title'> read命令简述 </div> <div class='spoiler-content'> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用方式:</span></span><br><span class="line"><span class="built_in">read</span> 【选项】 【变量名】(非固定可选)</span><br><span class="line"><span class="comment"># 参数选项</span></span><br><span class="line">-p 提示信息 <span class="comment">#等待read输入时,输出提示信息;</span></span><br><span class="line">-t 秒 数 <span class="comment">#read命令会一直等待用户输入,使用此选项可以指定等待时间;</span></span><br><span class="line">-n 字符数 <span class="comment">#read命令只接受指定的字符数量,然后就会执行;</span></span><br><span class="line">-s 隐藏输入 <span class="comment">#隐藏输入内容,适用于机密信息的输入;</span></span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># read使用案例 </span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">read</span> -t 5 -p <span class="string">"Enter your name : "</span></span><br><span class="line"><span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"hello <span class="variable">$name</span>, welcome to BeiJing"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"sorry, Output timeout, please execute the command again !"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"------------------"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">read</span> -s -t 5 -p <span class="string">"please enter your password : "</span></span><br><span class="line"><span class="keyword">then</span></span><br><span class="line"> <span class="built_in">echo</span> -n <span class="string">"status : $? , Ok"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="built_in">echo</span></span><br><span class="line"> <span class="built_in">echo</span> <span class="string">"sorry, Output timeout, please execute the command again !"</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure><div class="warning"><p><font color=red><strong>注意:read读取的参数,不能再用$n的方式进行调用!!!</strong></font></p></div> </div></div><h1>六、变量的字串</h1><h2 id="1、字符串切片">1、字符串切片</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># name="nanxi i love you"</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo $name</span></span><br><span class="line">nanxi i love you</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${name:0:5}</span></span><br><span class="line">nanxi</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${name:8:-4}</span></span><br><span class="line">love</span><br><span class="line">root@nanxi:~<span class="comment"># echo ${name:12:15}</span></span><br><span class="line">you</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># name="nanxi i love you"</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo ${name} | cut -c 1-5,8-16</span></span><br><span class="line">nanxi love you</span><br></pre></td></tr></table></figure><div class="tips"><p><strong>cut命令的简单介绍</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">基本语法: cut [选项参数] filename</span><br><span class="line"></span><br><span class="line">选项与参数:</span><br><span class="line">-d:分隔符,按照指定分隔符分割列。与 -f 一起使用</span><br><span class="line">-f:依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思(列号,提取第几列)</span><br><span class="line">-c:以字符 (characters) 的单位取出固定字符区间</span><br><span class="line">-b:以字节为单位进行分割</span><br></pre></td></tr></table></figure><p><em>注意⚠️: 如果文件里面的某些域是由<strong>若干个空格来间隔的</strong>,那么用cut就有点麻烦了,因为<strong>cut只擅长处理“以一个字符间隔”的文本内容</strong>。</em></p></div><h2 id="2、变量长度统计">2、变量长度统计</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># echo nanxi | wc -L</span></span><br><span class="line">5</span><br><span class="line">root@nanxi:~<span class="comment"># expr length "nanxi"</span></span><br><span class="line">5</span><br><span class="line">root@nanxi:~<span class="comment"># echo nanxi | awk '{print length}'</span></span><br><span class="line">5</span><br><span class="line">root@nanxi:~<span class="comment"># name=nanxi</span></span><br><span class="line">root@nanxi:~<span class="comment"># echo ${#name}</span></span><br><span class="line">5</span><br></pre></td></tr></table></figure><div class='spoiler collapsed'> <div class='spoiler-title'> wc命令回顾 </div> <div class='spoiler-content'> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 命令使用</span></span><br><span class="line"><span class="built_in">wc</span> [OPTION]... [FILE]...</span><br><span class="line"><span class="comment"># 常用参数</span></span><br><span class="line">-l , --lines : 显示行数;</span><br><span class="line">-w , --words : 显示字数;</span><br><span class="line">-m , --chars : 显示字符数;</span><br><span class="line">-c , --bytes : 显示字节数;</span><br><span class="line">-L , --max-line-length : 显示最长行的长度;</span><br></pre></td></tr></table></figure> </div></div><h2 id="3、统计字符串长度小于3的并输出到屏幕">3、统计字符串长度小于3的并输出到屏幕</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">root@nanxi:~<span class="comment"># vim select.sh</span></span><br><span class="line">root@nanxi:~<span class="comment"># cat select.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> I am nanxi and i love you</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"> [ <span class="variable">${#i}</span> -lt 3 ] && <span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line">root@nanxi:~<span class="comment"># source select.sh</span></span><br><span class="line">I</span><br><span class="line">am</span><br><span class="line">i</span><br><span class="line"></span><br><span class="line">root@nanxi:~<span class="comment"># echo I am nanxi I am 18 |xargs -n1|awk '{if(length<3)print}'I</span></span><br><span class="line">am</span><br><span class="line">I</span><br><span class="line">am</span><br><span class="line">18</span><br><span class="line"></span><br><span class="line">root@nanxi:~<span class="comment"># echo I am nanxi I am 18 |xargs -n1|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'</span></span><br><span class="line">I</span><br><span class="line">am</span><br><span class="line">I</span><br><span class="line">am</span><br><span class="line">18</span><br></pre></td></tr></table></figure><h1>七、判断表达式</h1><h2 id="1、文件比较运算符">1、文件比较运算符</h2><table><thead><tr><th style="text-align:left">表达式</th><th style="text-align:left">说明</th><th style="text-align:left">案例</th></tr></thead><tbody><tr><td style="text-align:left">-e filename</td><td style="text-align:left">如果filename存在,则为真</td><td style="text-align:left">[ –e /etc/hosts ]</td></tr><tr><td style="text-align:left">-d filename</td><td style="text-align:left">如果filename为目录,则为真</td><td style="text-align:left">[ –e /etc ]</td></tr><tr><td style="text-align:left">-f filename</td><td style="text-align:left">如果filename为常规文件,则为真</td><td style="text-align:left">[ –f /usr/bin/grep]</td></tr><tr><td style="text-align:left">-L filename</td><td style="text-align:left">如果filename为符号链接,则为真</td><td style="text-align:left">[ –L /usr/bin/grep]</td></tr><tr><td style="text-align:left">-r filename</td><td style="text-align:left">如果filename可读,则为真</td><td style="text-align:left">[ –r /etc/hosts ]</td></tr><tr><td style="text-align:left">-w filename</td><td style="text-align:left">如果filename可写,则为真</td><td style="text-align:left">[ –w /etc/hosts ]</td></tr><tr><td style="text-align:left">-x filename</td><td style="text-align:left">如果filename可执行,则为真</td><td style="text-align:left">[ –x /etc/hosts ]</td></tr><tr><td style="text-align:left">filename1 –nt filename2</td><td style="text-align:left">如果filename1比filename2新,则为真</td><td style="text-align:left">[/usr/test/file1.txt –nt /usr/test/file2.txt]</td></tr><tr><td style="text-align:left">filename1 –ot filename2</td><td style="text-align:left">如果filename1比filename2旧,则为真</td><td style="text-align:left">[/usr/test/file1.txt –ot /usr/test/file2.txt]</td></tr></tbody></table><h2 id="2、字符串比较运算符">2、字符串比较运算符</h2><table><thead><tr><th style="text-align:left">表达式</th><th style="text-align:left">说明</th><th style="text-align:left">案例</th></tr></thead><tbody><tr><td style="text-align:left">-z string</td><td style="text-align:left">如果string长度为0,则为真</td><td style="text-align:left">[ –z “$var”]</td></tr><tr><td style="text-align:left">-n string</td><td style="text-align:left">如果string长度非0,则为真</td><td style="text-align:left">[ –n “$var”]</td></tr><tr><td style="text-align:left">str1=str2</td><td style="text-align:left">如果str1与str2相同,则为真</td><td style="text-align:left">[ “$var” = ”hello world”]</td></tr><tr><td style="text-align:left">str1!=str2</td><td style="text-align:left">如果str1与str2不相同,则为真</td><td style="text-align:left">[ ”$var“ != ”hello world“]</td></tr></tbody></table><h2 id="3、算术比较运算符">3、算术比较运算符</h2><table><thead><tr><th style="text-align:left">表达式</th><th style="text-align:left">说明</th><th style="text-align:left">案例</th></tr></thead><tbody><tr><td style="text-align:left">num1-eq num2</td><td style="text-align:left">等于</td><td style="text-align:left">[ 6 -eq $num ]</td></tr><tr><td style="text-align:left">num1-ne num2</td><td style="text-align:left">不等于</td><td style="text-align:left">[ 6 -ne $num ]</td></tr><tr><td style="text-align:left">num1-lt num2</td><td style="text-align:left">小于</td><td style="text-align:left">[ 6 -lt $num ]</td></tr><tr><td style="text-align:left">num1-le num2</td><td style="text-align:left">小于等于</td><td style="text-align:left">[ 6 -le $num ]</td></tr><tr><td style="text-align:left">num1-gt num2</td><td style="text-align:left">大于</td><td style="text-align:left">[ 6 -gt $num ]</td></tr><tr><td style="text-align:left">num1-ge num2</td><td style="text-align:left">大于等于</td><td style="text-align:left">[ 6 -ge $num ]</td></tr></tbody></table><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/nanxiblogfoota.png" alt="nanxiblogfoota"></p><iframe frameborder="no" border="0" marginwidth="0" allow = "autoplay" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=1488212875&auto=1&height=66"></iframe><blockquote><p><em><strong>自由是上帝赐给人类的最大的幸福之一。</strong></em></p><p align="right">——塞万提斯</p></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Shell编程基础知识补充</center></h1>
<h1>一、执行脚本的方式</h1>
<p>1️⃣ 指定解释器执行 --------&gt; <code>/bin/bash nanxi.sh</code> &amp; <code>sh nanxi.sh</code> 默认解释器即为/bin/bash</p>
<p>2️⃣ 使用路径执行(需要执行权限+x)----&gt; <code>./nanxi.sh</code> &amp; <code>/scripts/nanxi.sh</code> 两种方式均可</p>
<p>3️⃣ 命令调用执行 -------------------------&gt; <code>source /scripts/nanxi.sh</code> &amp; <code>. nanxi.sh</code></p>
<div class="warning">
<p><font color=red><strong>区别:方式1️⃣和方式2️⃣执行脚本调用的是子shell,而方式3️⃣执行脚本调用的是当前shell</strong></font></p>
</div>
<h1>二、环境变量的执行顺序</h1>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240122182252168.png" alt="image-20240122182252168"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="Shell编程" scheme="https://smallflames.github.io/tags/Shell%E7%BC%96%E7%A8%8B/"/>
</entry>
<entry>
<title>Ansible一键部署实例(三)</title>
<link href="https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%B8%89%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%B8%89%EF%BC%89/</id>
<published>2024-01-21T10:36:01.000Z</published>
<updated>2024-01-21T11:35:17.643Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ansible一键部署实例(三)</center></h1><table><tr><td bgcolor=#000000><font color="white">业务部署代码</font></td></tr></table><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># wordpress业务部署</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/wordpress</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">wordpress.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">wordpress.sql</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">wordpress.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">6</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/wordpress</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for wordpress</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">nginx</span> <span class="string">default.conf</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/etc/nginx/conf.d/default.conf</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">wordpress</span> <span class="string">nginx</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">web</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">wordpress</span> <span class="string">code</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/wordpress</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">wordpress.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/code</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/code/wordpress</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modify</span> <span class="string">directory</span> <span class="string">permissions</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">chown</span> <span class="string">-R</span> <span class="string">www.www</span> <span class="string">/code</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">sent</span> <span class="string">wordpress.sql</span> <span class="string">to</span> <span class="string">db</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.sql</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">import</span> <span class="string">wordpress.sql</span></span><br><span class="line"> <span class="attr">mysql_db:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">all</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">import</span></span><br><span class="line"> <span class="attr">target:</span> <span class="string">/tmp/wordpress.sql</span></span><br><span class="line"> <span class="attr">register:</span> <span class="string">result</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">nginx</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">mariadb</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">mariadb</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">mount</span> <span class="string">dir</span> <span class="string">to</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">mount:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="number">172.16</span><span class="number">.1</span><span class="number">.31</span><span class="string">:/data/wordpress</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/wordpress/wp-content/uploads</span></span><br><span class="line"> <span class="attr">fstype:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">mounted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><span id="more"></span><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># kod业务部署</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/kod</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">kod.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">kod.sql</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">kod.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">6</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/kod</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for kod</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">nginx</span> <span class="string">default.conf</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/etc/nginx/conf.d/default.conf</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">kod</span> <span class="string">nginx</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">kod.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">web</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">kod</span> <span class="string">code</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/kod</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">kod.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/code</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">kod.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/code/kod</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modify</span> <span class="string">directory</span> <span class="string">permissions</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">chown</span> <span class="string">-R</span> <span class="string">www.www</span> <span class="string">/code</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">sent</span> <span class="string">kod.sql</span> <span class="string">to</span> <span class="string">db</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">kod.sql</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">import</span> <span class="string">kod.sql</span></span><br><span class="line"> <span class="attr">mysql_db:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">all</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">import</span></span><br><span class="line"> <span class="attr">target:</span> <span class="string">/tmp/kod.sql</span></span><br><span class="line"> <span class="attr">register:</span> <span class="string">result</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">nginx</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">mariadb</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">mariadb</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">mount</span> <span class="string">dir</span> <span class="string">to</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">mount:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="number">172.16</span><span class="number">.1</span><span class="number">.31</span><span class="string">:/data/kod</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/kod/data/files</span></span><br><span class="line"> <span class="attr">fstype:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">mounted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 部署tomcat服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/tomcat</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">context.xml</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">jdk.rpm</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">redis-data-cache.properties</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">tomcat.service</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">tomcat_session.tar.gz</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">tomcat.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">9</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/tomcat</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for tomcat</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">sent</span> <span class="string">jdk.rpm</span> <span class="string">and</span> <span class="string">tomcat.service</span> <span class="string">to</span> <span class="string">web</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">jdk.rpm</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">tomcat.service</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">java</span> <span class="string">environment</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">yum</span> <span class="string">-y</span> <span class="string">localinstall</span> <span class="string">/tmp/jdk.rpm</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">systemctl</span> <span class="string">for</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">tomcat.service</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/usr/lib/systemd/system</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">soft</span> <span class="string">dir</span> <span class="string">for</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/soft</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">tomcat.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/soft</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">tomcat.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/soft</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">tomcat</span> <span class="string">link</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">/soft/apache-tomcat-9.0.84</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/soft/tomcat</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">link</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">tomcat_session.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">tomcat</span> <span class="string">for</span> <span class="string">session</span> <span class="string">share</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">tomcat_session.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/soft/tomcat/lib</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">cp</span> <span class="string">redis-data-cache.properties</span> <span class="string">and</span> <span class="string">context.xml</span> <span class="string">to</span> <span class="string">tomcat</span> <span class="string">for</span> <span class="string">session</span> <span class="string">share</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/soft/tomcat/conf</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">redis-data-cache.properties</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">context.xml</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">reload</span> <span class="string">by</span> <span class="string">systemctl</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">systemctl</span> <span class="string">daemon-reload</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">systemctl</span> <span class="string">restart</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">systemctl</span> <span class="string">enable</span> <span class="string">tomcat</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">remains</span> <span class="string">for</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/tmp/jdk.rpm</span> <span class="string">/tmp/sys_tomcat</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># zrlog服务部署</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/zrlog</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">server.xml</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">zrlog.sql</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">zrlog.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">6</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/zrlog</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for zrlog</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">zrlog</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">server.xml</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/soft/tomcat/conf</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">zrlog</span> <span class="string">code</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/zrlog/ROOT</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">sent</span> <span class="string">zrlog.sql</span> <span class="string">to</span> <span class="string">db</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">zrlog.sql</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">import</span> <span class="string">zrlog.sql</span></span><br><span class="line"> <span class="attr">mysql_db:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">all</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">import</span></span><br><span class="line"> <span class="attr">target:</span> <span class="string">/tmp/zrlog.sql</span></span><br><span class="line"> <span class="attr">register:</span> <span class="string">result</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">mariadb</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">mariadb</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">zrlog.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/code/zrlog/ROOT</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">zrlog.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/code/zrlog/ROOT</span></span><br><span class="line"> <span class="attr">ignore_errors:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">tomcat</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">systemctl</span> <span class="string">restart</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">mount</span> <span class="string">dir</span> <span class="string">to</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">mount:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="number">172.16</span><span class="number">.1</span><span class="number">.31</span><span class="string">:/data/zrlog</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/zrlog/ROOT/attached</span></span><br><span class="line"> <span class="attr">fstype:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">mounted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># phpmyadmin部署</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/phpadmin</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">admin.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">phpadmin.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">5</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/phpadmin</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for phpadmin</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">nginx</span> <span class="string">default.conf</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/etc/nginx/conf.d/default.conf</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">admin</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">admin.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">nginx</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">admin</span> <span class="string">code</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/phpadmin</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">phpadmin.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">code_dir</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">phpadmin.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/code/phpadmin</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modify</span> <span class="string">directory</span> <span class="string">permissions</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">chown</span> <span class="string">-R</span> <span class="string">www.www</span> <span class="string">/code</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><table><tr><td bgcolor=#000000><font color="white">服务部署代码</font></td></tr></table><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 负载均衡配置</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/lb_nginx</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">proxy.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">proxy_params</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">5</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/lb_nginx</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for lb_nginx</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">nginx</span> <span class="string">default.conf</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/etc/nginx/conf.d/default.conf</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Reverse</span> <span class="string">proxy</span> <span class="string">proxy_params</span> <span class="string">and</span> <span class="string">Proxy</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">"<span class="template-variable">{{ item.src }}</span>"</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">"<span class="template-variable">{{ item.dest }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">src:</span> <span class="string">proxy_params</span>,<span class="attr">dest:</span> <span class="string">/etc/nginx</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">src:</span> <span class="string">proxy.conf</span>,<span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span> }</span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Restart</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 负载均衡高可用</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/keepalived</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">keep_nginxcheck.sh</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">keepalived.conf.j2</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">5</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/keepalived</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for keepalived</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Istall</span> <span class="string">keepalived</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">keepalived</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">shell</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/script</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Sent</span> <span class="string">nginx</span> <span class="string">check</span> <span class="string">shell</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">keep_nginxcheck.sh</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/script</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="string">'0755'</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">keepalived</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">template:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">keepalived.conf.j2</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/keepalived/keepalived.conf</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">Restart</span> <span class="string">keepalived</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">keepalived</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">keepalived</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全网定时备份</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/backup</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">backup.sh</span></span><br><span class="line"><span class="string">└──</span> <span class="string">tasks</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">2</span> <span class="string">directories,</span> <span class="number">2</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/backup</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for backup</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Create</span> <span class="string">shell</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/script/autobackup</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Sent</span> <span class="string">backup.sh</span> <span class="string">all</span> <span class="string">except</span> <span class="string">backup</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">backup.sh</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/script/autobackup</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="string">'0755'</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Write</span> <span class="string">in</span> <span class="string">cron</span> <span class="string">tasks</span> <span class="string">for</span> <span class="string">web</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"<span class="template-variable">{{ item.name}}</span>"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/5"</span></span><br><span class="line"> <span class="comment">#hour: "02"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"<span class="template-variable">{{ item.job }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="string">"Code scheduled backup"</span>,<span class="attr">job:</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/code</span> <span class="string">></span> <span class="string">/dev/null</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="string">"Nginx scheduled backup"</span>,<span class="attr">job:</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/etc/nginx</span> <span class="string">></span> <span class="string">/dev/null</span> }</span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Write</span> <span class="string">in</span> <span class="string">cron</span> <span class="string">tasks</span> <span class="string">for</span> <span class="string">lb</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"Nginx scheduled backup"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/5"</span></span><br><span class="line"> <span class="comment">#hour: "02"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/etc/nginx</span> <span class="string">></span> <span class="string">/dev/null</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Write</span> <span class="string">in</span> <span class="string">cron</span> <span class="string">tasks</span> <span class="string">for</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"Data share scheduled backup"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/5"</span></span><br><span class="line"> <span class="comment">#hour: "02"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/data</span> <span class="string">></span> <span class="string">/dev/null</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Write</span> <span class="string">in</span> <span class="string">cron</span> <span class="string">tasks</span> <span class="string">for</span> <span class="string">db</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"<span class="template-variable">{{ item.name }}</span>"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/5"</span></span><br><span class="line"> <span class="comment">#hour: "02"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"<span class="template-variable">{{ item.job }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="string">"Mysql conf scheduled backup"</span>,<span class="attr">job:</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/etc/my.cnf.d</span> <span class="string">></span> <span class="string">/dev/null</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="string">"Mysql file scheduled backup"</span>,<span class="attr">job:</span> <span class="string">/bin/bash</span> <span class="string">/script/autobackup/backup.sh</span> <span class="string">/var/lib/mysql</span> <span class="string">></span> <span class="string">/dev/null</span> }</span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置https</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/https</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">proxy.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">server.crt</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">server.key</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">6</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/https</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for https</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Create</span> <span class="string">ssl_key</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/nginx/ssl_key</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Key</span> <span class="string">distribution</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/ssl_key</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">server.crt</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">server.key</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modifying</span> <span class="string">agent</span> <span class="string">configuration</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">proxy.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">Restart</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modifying</span> <span class="string">web</span> <span class="string">configuration</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">"<span class="template-variable">{{ item.path }}</span>"</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">"<span class="template-variable">{{ item.regex }}</span>"</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">"<span class="template-variable">{{ item.line }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">path:</span> <span class="string">/etc/nginx/conf.d/wordpress.conf</span>,<span class="attr">regex:</span> <span class="string">'^#fastcgi_param'</span>,<span class="attr">line:</span> <span class="string">"fastcgi_param HTTPS on;"</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">path:</span> <span class="string">/etc/nginx/conf.d/admin.conf</span>,<span class="attr">regex:</span> <span class="string">'^#fastcgi_param'</span>,<span class="attr">line:</span> <span class="string">"fastcgi_param HTTPS on;"</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">path:</span> <span class="string">/etc/nginx/conf.d/kod.conf</span>,<span class="attr">regex:</span> <span class="string">'^#fastcgi_param'</span>,<span class="attr">line:</span> <span class="string">"fastcgi_param HTTPS on;"</span> }</span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Restart</span> <span class="string">web</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">port</span> <span class="number">443</span> <span class="string">for</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/soft/tomcat/conf/server.xml</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">'redirectPort="8443"'</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">' redirectPort="443" proxyPort="443" scheme="https" secure="true" SSLEnabled="true" '</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置防火墙策略</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/iptables</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">└──</span> <span class="string">templates</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">iptables_strategy.j2</span></span><br><span class="line"></span><br><span class="line"><span class="number">2</span> <span class="string">directories,</span> <span class="number">2</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/iptables</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for iptables</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Stop</span> <span class="string">local</span> <span class="string">firewalld</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">firewalld</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">stopped</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Prohibit</span> <span class="string">self</span> <span class="string">start</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">systemctl</span> <span class="string">disable</span> <span class="string">firewalld</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">iptables-services</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">iptables-services</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">iptables</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">iptables.service</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Insert</span> <span class="string">configuration</span> <span class="string">in</span> <span class="string">rc.local</span></span><br><span class="line"> <span class="attr">blockinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/rc.d/rc.local</span></span><br><span class="line"> <span class="attr">block:</span> <span class="string">|</span></span><br><span class="line"><span class="string"> modprobe ip_tables</span></span><br><span class="line"><span class="string"> modprobe iptable_filter</span></span><br><span class="line"><span class="string"> modprobe iptable_nat</span></span><br><span class="line"><span class="string"> modprobe ip_contrack</span></span><br><span class="line"><span class="string"> modprobe ip_conntrack_ftp</span></span><br><span class="line"><span class="string"> modprobe ip_nat_ftp</span></span><br><span class="line"><span class="string"> modprobe ipt_state</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Execute</span> <span class="string">permission</span> <span class="string">for</span> <span class="string">rc.local</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">chmod</span> <span class="number">0755</span> <span class="string">/etc/rc.d/rc.local</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Sent</span> <span class="string">iptables_strategy</span> <span class="string">to</span> <span class="string">lb</span></span><br><span class="line"> <span class="attr">template:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">iptables_strategy.j2</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp/iptables_strategy</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Import</span> <span class="string">iptables_strategy</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">iptables-restore</span> <span class="string"><</span> <span class="string">/tmp/iptables_strategy</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Remove</span> <span class="string">/tmp/iptables_strategy</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/tmp/iptables_strategy</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 内部服务器访问公网</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/access_net</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">└──</span> <span class="string">tasks</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span> <span class="string">directory,</span> <span class="number">1</span> <span class="string">file</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/access_net</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for access_net</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modifying</span> <span class="string">eth1</span> <span class="string">network</span> <span class="string">configuration</span></span><br><span class="line"> <span class="attr">blockinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/sysconfig/network-scripts/ifcfg-eth1</span></span><br><span class="line"> <span class="attr">block:</span> <span class="string">|</span></span><br><span class="line"><span class="string"> GATEWAY=172.16.1.6</span></span><br><span class="line"><span class="string"></span> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Restart</span> <span class="string">the</span> <span class="string">network</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">systemctl</span> <span class="string">restart</span> <span class="string">network</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">switch</span> <span class="string">on</span> <span class="string">kernel</span> <span class="string">forward</span></span><br><span class="line"> <span class="attr">blockinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/sysctl.conf</span></span><br><span class="line"> <span class="attr">block:</span> <span class="string">|</span></span><br><span class="line"><span class="string"> net.ipv4.ip_forward = 1</span></span><br><span class="line"><span class="string"></span> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">load</span> <span class="string">configuration</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">sysctl</span> <span class="string">-p</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#- name: switch off eth0</span></span><br><span class="line"><span class="comment"># shell: ifdown eth0 warn=false</span></span><br><span class="line"><span class="comment"># when: ansible_hostname is not match "lb"</span></span><br><span class="line"><span class="comment">#- name: Modifying eth0 network configuration</span></span><br><span class="line"><span class="comment"># lineinfile:</span></span><br><span class="line"><span class="comment"># path: /etc/sysconfig/network-scripts/ifcfg-eth0</span></span><br><span class="line"><span class="comment"># regexp: '^ONBOOT'</span></span><br><span class="line"><span class="comment"># line: 'ONBOOT=yes'</span></span><br><span class="line"><span class="comment"># when: ansible_hostname is not match "lb"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 时间服务器配置</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/ntpdate</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">└──</span> <span class="string">tasks</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">2</span> <span class="string">directories,</span> <span class="number">2</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/ntpdate</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for ntpdata</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">ntp</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">ntp</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">cron</span> <span class="string">task</span> <span class="string">in</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"TimeSync as Server"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/10"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"/usr/sbin/ntpdate ntp1.aliyun.com && hwclock -w > /dev/null"</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modifying</span> <span class="string">configure</span> <span class="string">for</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">blockinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/ntp.conf</span></span><br><span class="line"> <span class="attr">block:</span> <span class="string">|</span></span><br><span class="line"><span class="string"> SYNC_HWCLOCK=yes</span></span><br><span class="line"><span class="string"></span> <span class="attr">notify:</span> <span class="string">Restart</span> <span class="string">ntpd</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Remove</span> <span class="string">server</span> <span class="string">on</span> <span class="string">client</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/ntp.conf</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">'^server'</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">absent</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modifying</span> <span class="string">configure</span> <span class="string">for</span> <span class="string">client</span></span><br><span class="line"> <span class="attr">blockinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/ntp.conf</span></span><br><span class="line"> <span class="attr">block:</span> <span class="string">|</span></span><br><span class="line"><span class="string"> server 172.16.1.32 prefer</span></span><br><span class="line"><span class="string"> server 172.16.1.31</span></span><br><span class="line"><span class="string"></span> <span class="attr">notify:</span> <span class="string">Restart</span> <span class="string">ntpd</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">ntpd</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">ntpd</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">cron</span> <span class="string">task</span> <span class="string">in</span> <span class="string">client</span></span><br><span class="line"> <span class="attr">cron:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"TimeSync as Server"</span></span><br><span class="line"> <span class="attr">minute:</span> <span class="string">"*/10"</span></span><br><span class="line"> <span class="attr">job:</span> <span class="string">"/usr/sbin/ntpdate 172.16.1.32 && hwclock -w > /dev/null"</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ansible一键部署实例(三)</center></h1>
<table><tr><td bgcolor=#000000><font color="white">业务部署代码</font></td></tr></table>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># wordpress业务部署</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/wordpress</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">wordpress.conf</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">wordpress.sql</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">wordpress.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">6</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/wordpress</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for wordpress</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">nginx</span> <span class="string">default.conf</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/etc/nginx/conf.d/default.conf</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">wordpress</span> <span class="string">nginx</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/conf.d</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">web</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">wordpress</span> <span class="string">code</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/wordpress</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">wordpress.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/code</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/code/wordpress</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modify</span> <span class="string">directory</span> <span class="string">permissions</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">chown</span> <span class="string">-R</span> <span class="string">www.www</span> <span class="string">/code</span> <span class="string">warn=false</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">sent</span> <span class="string">wordpress.sql</span> <span class="string">to</span> <span class="string">db</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">wordpress.sql</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;db&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">import</span> <span class="string">wordpress.sql</span></span><br><span class="line"> <span class="attr">mysql_db:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">all</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">import</span></span><br><span class="line"> <span class="attr">target:</span> <span class="string">/tmp/wordpress.sql</span></span><br><span class="line"> <span class="attr">register:</span> <span class="string">result</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;db&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">nginx</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">mariadb</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">mariadb</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;db&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">mount</span> <span class="string">dir</span> <span class="string">to</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">mount:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="number">172.16</span><span class="number">.1</span><span class="number">.31</span><span class="string">:/data/wordpress</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/code/wordpress/wp-content/uploads</span></span><br><span class="line"> <span class="attr">fstype:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">mounted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">&quot;web&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="服务架构" scheme="https://smallflames.github.io/tags/%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/"/>
</entry>
<entry>
<title>Ansible一键部署实例(二)</title>
<link href="https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%BA%8C%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%BA%8C%EF%BC%89/</id>
<published>2024-01-21T10:35:40.000Z</published>
<updated>2024-01-21T11:35:06.680Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ansible一键部署实例(二)</center></h1><table><tr><td bgcolor=#000000><font color="white">基本环境准备</font></td></tr></table><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 免密钥分发</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat ssh.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">key=<span class="string">"/root/.ssh/id_rsa.pub"</span></span><br><span class="line">pass=<span class="string">"密码"</span></span><br><span class="line">nodeip=<span class="string">"ipfile"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> ip</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"> sshpass -p <span class="variable">$pass</span> ssh-copy-id -i <span class="variable">$key</span> <span class="variable">$ip</span> -o StrictHostKeyChecking=no &> /dev/null && <span class="built_in">echo</span> <span class="string">"<span class="variable">${ip}</span> sshkey is success."</span></span><br><span class="line"><span class="keyword">done</span> < <span class="variable">$nodeip</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ipfile</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat ipfile</span></span><br><span class="line">172.16.1.5</span><br><span class="line">172.16.1.6</span><br><span class="line">172.16.1.7</span><br><span class="line">172.16.1.8</span><br><span class="line">172.16.1.9</span><br><span class="line">172.16.1.51</span><br><span class="line">172.16.1.31</span><br><span class="line">172.16.1.32</span><br><span class="line">172.16.1.41</span><br><span class="line"></span><br><span class="line"><span class="comment"># hosts</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat hosts</span></span><br><span class="line">[web]</span><br><span class="line">172.16.1.7</span><br><span class="line">172.16.1.8</span><br><span class="line">172.16.1.9</span><br><span class="line">[nfs]</span><br><span class="line">172.16.1.31</span><br><span class="line">172.16.1.32</span><br><span class="line">[db]</span><br><span class="line">172.16.1.51</span><br><span class="line">[lb]</span><br><span class="line">172.16.1.5</span><br><span class="line">172.16.1.6</span><br><span class="line">[backup]</span><br><span class="line">172.16.1.41</span><br></pre></td></tr></table></figure><span id="more"></span><table><tr><td bgcolor=#000000><font color="white">全局部署文件</font></td></tr></table><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles</span>]<span class="comment">#cat site.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">all</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">The</span> <span class="string">configuration</span> <span class="string">of</span> <span class="string">this</span> <span class="string">script</span> <span class="string">is</span> <span class="string">as</span> <span class="string">follows</span></span><br><span class="line"> <span class="attr">roles:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">basic</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">basic</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">local_repo</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">local_repo</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">rsync</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">rsync</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">nfs</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span><span class="string">)</span> <span class="string">or</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span><span class="string">)</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">php</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">php</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">mysql</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">mysql</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"db"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">sersync</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">sersync</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">global_nginx</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">global_nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span><span class="string">)</span> <span class="string">or</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span><span class="string">)</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">wordpress</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">wordpress</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">kod</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">kod</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">tomcat</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">zrlog</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">zrlog</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">phpadmin</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">phpadmin</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">lb_nginx</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">lb_nginx</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">keepalived</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">keepalived</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">backup</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">backup</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">https</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">https</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span><span class="string">)</span> <span class="string">or</span> <span class="string">(ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"web"</span><span class="string">)</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">iptables</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">iptables</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"lb"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">access_net</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">access_net</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">role:</span> <span class="string">ntpdate</span></span><br><span class="line"> <span class="attr">tags:</span> <span class="string">ntpdate</span></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><table><tr><td bgcolor=#000000><font color="white">基础优化项</font></td></tr></table><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 为各项服务统一用户准备</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/basic</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nified</span> <span class="string">Users</span> <span class="string">create</span> <span class="string">www</span> <span class="string">group</span></span><br><span class="line"> <span class="attr">group:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">gid:</span> <span class="number">666</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">www</span> <span class="string">user</span></span><br><span class="line"> <span class="attr">user:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">uid:</span> <span class="number">666</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">create_home:</span> <span class="literal">false</span></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><table><tr><td bgcolor=#000000><font color="white">服务环境搭建</font></td></tr></table><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置本地YUM仓库</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/local_repo</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">soft.repo</span></span><br><span class="line"><span class="string">└──</span> <span class="string">tasks</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">2</span> <span class="string">directories,</span> <span class="number">2</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/local_repo</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for local_repo</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Create</span> <span class="string">repo</span> <span class="string">dir</span> <span class="string">for</span> <span class="string">backup</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/repo</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Mv</span> <span class="string">repo</span> <span class="string">to</span> <span class="string">backupdir</span> <span class="string">/repo</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">mv</span> <span class="string">/etc/yum.repos.d/*</span> <span class="string">/repo</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">local</span> <span class="string">yum</span> <span class="string">repo</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">soft.repo</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/yum.repos.d</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#- name: Install yum-plugin-priorities</span></span><br><span class="line"><span class="comment"># yum:</span></span><br><span class="line"><span class="comment"># name: yum-plugin-priorities</span></span><br><span class="line"><span class="comment"># state: present</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#- name: Mv repo to backupdir /repo</span></span><br><span class="line"><span class="comment"># shell: mv /repo/* /etc/yum.repos.d warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装配置rsync服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/rsync</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">rsyncd.conf</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">4</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/rsync</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for rsync</span></span><br><span class="line"><span class="comment"># backup为服务端,其余全部为客户端</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">rsync</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">rsync</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">rsyncd.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">backup</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/backup/lb</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/backup/web</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/backup/nfs</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">/backup/msq</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">passwd</span> <span class="string">file</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">content:</span> <span class="string">rsync_backup:nanxi</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/rsync.passwd</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="number">0600</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">start</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">rsyncd</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">rsync</span> <span class="string">client</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">content:</span> <span class="string">nanxi</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/rsync.pass</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="number">0600</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">not</span> <span class="string">match</span> <span class="string">"backup"</span></span><br><span class="line"><span class="comment"># nfs2为服务端,nfs1为客户端</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nfs2</span> <span class="string">configure</span> <span class="string">rsync</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">rsyncd.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nfs2</span> <span class="string">create</span> <span class="string">backup</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/backup/nfs</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nfs2</span> <span class="string">create</span> <span class="string">passwd</span> <span class="string">file</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">content:</span> <span class="string">rsync_backup:nanxi</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/rsync.passwd</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="number">0600</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">rsyncd</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nfs2</span> <span class="string">start</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">rsyncd</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装nfs服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/nfs</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">exports.j2</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">4</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/nfs</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for nfs</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">nfs</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nfs-utils</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">create</span> <span class="string">nfs</span> <span class="string">share</span> <span class="string">dir</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/data/{{</span> <span class="string">item</span> <span class="string">}}</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">wordpress</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">kod</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">zrlog</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">phpadmin</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">nfs</span> <span class="string">servser</span></span><br><span class="line"> <span class="attr">template:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">exports.j2</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/exports</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">nfs</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">start</span> <span class="string">nfs</span> <span class="string">server</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">rpcbind</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">nfs-server</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs"</span></span><br><span class="line"> </span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装配置PHP服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/php</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">php.tar.gz</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">4</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/php</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for php</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Extract</span> <span class="string">php.tar.gz</span> <span class="string">into</span> <span class="string">web</span> <span class="string">/tmp</span></span><br><span class="line"> <span class="attr">unarchive:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">php.tar.gz</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/tmp</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">php</span> <span class="string">into</span> <span class="string">web</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">yum</span> <span class="string">-y</span> <span class="string">localinstall</span> <span class="string">/tmp/*.rpm</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">remove</span> <span class="string">php.*.rpm</span> <span class="string">in</span> <span class="string">web</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">rm</span> <span class="string">-rf</span> <span class="string">/tmp/*.rpm</span> <span class="string">warn=false</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">php</span> <span class="string">start</span> <span class="string">user</span> <span class="string">and</span> <span class="string">prepare</span> <span class="string">for</span> <span class="string">redis</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/php-fpm.d/www.conf</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">"<span class="template-variable">{{ item.regex }}</span>"</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">"<span class="template-variable">{{ item.line }}</span>"</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">php-fpm</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^user'</span>,<span class="attr">line:</span> <span class="string">user</span> <span class="string">=</span> <span class="string">www</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^group'</span>,<span class="attr">line:</span> <span class="string">group</span> <span class="string">=</span> <span class="string">www</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^php_value\[session.save_handler\]'</span>,<span class="attr">line:</span> <span class="string">";php_value[session.save_handler] = files"</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^php_value\[session.save_path\]'</span>,<span class="attr">line:</span> <span class="string">";php_value[session.save_path] = /var/lib/php/session"</span> }</span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">php</span> <span class="string">for</span> <span class="string">redis</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/php.ini</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">"<span class="template-variable">{{ item.regex }}</span>"</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">"<span class="template-variable">{{ item.line }}</span>"</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">php-fpm</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^session\.save_handler'</span>,<span class="attr">line:</span> <span class="string">session.save_handler</span> <span class="string">=</span> <span class="string">redis</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^session\.save_path'</span>,<span class="attr">line:</span> <span class="string">'session.save_path = "tcp://172.16.1.51:6379"'</span> }</span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">restart</span> <span class="string">php-fpm</span> <span class="string">web</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">php-fpm</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装数据库、redis及依赖</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/mysql</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">3</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/mysql</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for mysql</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">mariadb</span> <span class="string">Server</span> <span class="string">redis</span> <span class="string">and</span> <span class="string">dependent</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">mariadb-server</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">MySQL-python</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">redis</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configure</span> <span class="string">/etc/redis.conf</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/redis.conf</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">'^bind'</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">bind</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">172.16</span><span class="number">.1</span><span class="number">.51</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">mysql</span> <span class="string">and</span> <span class="string">redis</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">mariadb</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">redis</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 为nfs配置sersync服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/sersync</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">│</span> <span class="string">├──</span> <span class="string">confxml.xml</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">sersync2</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">└──</span> <span class="string">templates</span></span><br><span class="line"></span><br><span class="line"><span class="number">3</span> <span class="string">directories,</span> <span class="number">3</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/sersync</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for sersync</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Cteate</span> <span class="string">sersync</span> <span class="string">dir</span> <span class="string">in</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/sersync</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">directory</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs1"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Sent</span> <span class="string">sersync</span> <span class="string">to</span> <span class="string">nfs1</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">"<span class="template-variable">{{ item }}</span>"</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/sersync</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="string">'0755'</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">confxml.xml</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">sersync2</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs1"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Modify</span> <span class="string">backup</span> <span class="string">module</span> <span class="string">in</span> <span class="string">nfs2</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/rsyncd.conf</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">"<span class="template-variable">{{ item.regex }}</span>"</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">"<span class="template-variable">{{ item.line }}</span>"</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^\[backup\]'</span>,<span class="attr">line:</span> <span class="string">"[data]"</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">regex:</span> <span class="string">'^path'</span>,<span class="attr">line:</span> <span class="string">path</span> <span class="string">=</span> <span class="string">/data</span> }</span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Restart</span> <span class="string">rsync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">rsyncd</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs2"</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">sersync</span> <span class="string">service</span></span><br><span class="line"> <span class="attr">shell:</span></span><br><span class="line"> <span class="attr">cmd:</span> <span class="string">/etc/sersync/sersync2</span> <span class="string">-dro</span> <span class="string">/etc/sersync/confxml.xml</span></span><br><span class="line"> <span class="attr">ignore_errors:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">is</span> <span class="string">match</span> <span class="string">"nfs1"</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置官方nginx服务</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/global_nginx</span>]<span class="comment">#tree</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├──</span> <span class="string">files</span></span><br><span class="line"><span class="string">├──</span> <span class="string">handlers</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">tasks</span></span><br><span class="line"><span class="string">│</span> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"><span class="string">├──</span> <span class="string">templates</span></span><br><span class="line"><span class="string">└──</span> <span class="string">vars</span></span><br><span class="line"> <span class="string">└──</span> <span class="string">main.yml</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="string">directories,</span> <span class="number">3</span> <span class="string">files</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~/roles/global_nginx</span>]<span class="comment">#cat tasks/main.yml</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># tasks file for nginx</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">nginx</span> <span class="string">official</span> <span class="string">yum</span></span><br><span class="line"> <span class="attr">yum_repository:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx-stable</span></span><br><span class="line"> <span class="attr">description:</span> <span class="string">nginx</span> <span class="string">yum</span> <span class="string">repo</span></span><br><span class="line"> <span class="attr">baseurl:</span> <span class="string">http://nginx.org/packages/centos/$releasever/$basearch/</span></span><br><span class="line"> <span class="attr">gpgcheck:</span> <span class="literal">no</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">official</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">unified</span> <span class="string">users</span> <span class="string">by</span> <span class="string">www</span></span><br><span class="line"> <span class="attr">lineinfile:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">/etc/nginx/nginx.conf</span></span><br><span class="line"> <span class="attr">regexp:</span> <span class="string">'^user'</span></span><br><span class="line"> <span class="attr">line:</span> <span class="string">user</span> <span class="string">www;</span></span><br><span class="line"> <span class="attr">notify:</span> <span class="string">restart</span> <span class="string">nginx</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Start</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">started</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ansible一键部署实例(二)</center></h1>
<table><tr><td bgcolor=#000000><font color="white">基本环境准备</font></td></tr></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 免密钥分发</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat ssh.sh</span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">key=<span class="string">&quot;/root/.ssh/id_rsa.pub&quot;</span></span><br><span class="line">pass=<span class="string">&quot;密码&quot;</span></span><br><span class="line">nodeip=<span class="string">&quot;ipfile&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> ip</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"> sshpass -p <span class="variable">$pass</span> ssh-copy-id -i <span class="variable">$key</span> <span class="variable">$ip</span> -o StrictHostKeyChecking=no &amp;&gt; /dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;ip&#125;</span> sshkey is success.&quot;</span></span><br><span class="line"><span class="keyword">done</span> &lt; <span class="variable">$nodeip</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ipfile</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat ipfile</span></span><br><span class="line">172.16.1.5</span><br><span class="line">172.16.1.6</span><br><span class="line">172.16.1.7</span><br><span class="line">172.16.1.8</span><br><span class="line">172.16.1.9</span><br><span class="line">172.16.1.51</span><br><span class="line">172.16.1.31</span><br><span class="line">172.16.1.32</span><br><span class="line">172.16.1.41</span><br><span class="line"></span><br><span class="line"><span class="comment"># hosts</span></span><br><span class="line">[root@ansible ~/roles]<span class="comment">#cat hosts</span></span><br><span class="line">[web]</span><br><span class="line">172.16.1.7</span><br><span class="line">172.16.1.8</span><br><span class="line">172.16.1.9</span><br><span class="line">[nfs]</span><br><span class="line">172.16.1.31</span><br><span class="line">172.16.1.32</span><br><span class="line">[db]</span><br><span class="line">172.16.1.51</span><br><span class="line">[lb]</span><br><span class="line">172.16.1.5</span><br><span class="line">172.16.1.6</span><br><span class="line">[backup]</span><br><span class="line">172.16.1.41</span><br></pre></td></tr></table></figure></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="服务架构" scheme="https://smallflames.github.io/tags/%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/"/>
</entry>
<entry>
<title>Ansible一键部署实例(一)</title>
<link href="https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%B8%80%EF%BC%89/"/>
<id>https://smallflames.github.io/2024/01/21/Ansible%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2%E5%AE%9E%E4%BE%8B%EF%BC%88%E4%B8%80%EF%BC%89/</id>
<published>2024-01-21T09:25:32.000Z</published>
<updated>2024-01-21T15:04:35.484Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ansible一键部署实例(一)</center></h1><p> 我们今天要用Ansible实现一键部署的框架大致如下:</p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240121173432410.png" alt="image-20240121173432410"></p><p> 南汐在这里对本次框架做一个简单的介绍,这是一个非常基础的架构,虽然可能不如实际应用中的复杂,但是麻雀虽小,亦五脏俱全。从用户角度来看,当我们的用户访问我们公司的网站时,一般情况下会经由DNS智能调度系统给用户返回距离用户地域距离最近的服务器地址,这就是我们的CDN服务器,它会默认存储我们web服务上基本所有的静态资源(或动态内容、流媒体文件)以提高用户的访问体验,如果CDN上没有用户所请求的资源,此时会出现回源。<span id="more"></span>当我们的用户直接访问源站的时候,会经由我们前端的网络防火墙过滤非法流量,然会到达我们的负载,本次架构中我们使用两台服务器应用NGINX作为负载均衡,并在两台服务器之间通过keepalived实现高可用。请求经由负载均衡的调度算法后转发至我们真实的web服务器,这里web服务器在实际中基本都是多节点的,我们称之集群,南汐这里使用三台服务器来进行模拟。web端的静态文件由后端的两台NFS服务器存储,他们之间的数据实时同步的,并且具备高可用,以避免单点故障,同时前端所产生的业务数据数据库文件都会存放在后端的数据库。最后与业务直接相关的服务器(本此架构中有8台)均会定时将重要文件备份至backup服务器。从运维管理人员的角度来看,我们会经由VPN进入公司的jumpserver服务器(直接越过用户路径的防火墙进入内部),底层通过Ansible自动化管理工具来对公司的服务器进行管理,监控和维护。为了提高脚本安装服务的执行效率,我们额外搭建了一个本地仓库为提供内部局域网安装服务使用。</p><p> (南汐并没有实现架构内的所有功能,本次展示只为帮助大家提高对Ansible自动化管理工具的应用)详细Playbook角色分布如下:</p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240121182411299.png" alt="image-20240121182411299"></p><p> 接下来南汐会用几篇文章依次开放源码展示,大家仅供参考,可自行实践练习……</p><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ansible一键部署实例(一)</center></h1>
<p> 我们今天要用Ansible实现一键部署的框架大致如下:</p>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240121173432410.png" alt="image-20240121173432410"></p>
<p> 南汐在这里对本次框架做一个简单的介绍,这是一个非常基础的架构,虽然可能不如实际应用中的复杂,但是麻雀虽小,亦五脏俱全。从用户角度来看,当我们的用户访问我们公司的网站时,一般情况下会经由DNS智能调度系统给用户返回距离用户地域距离最近的服务器地址,这就是我们的CDN服务器,它会默认存储我们web服务上基本所有的静态资源(或动态内容、流媒体文件)以提高用户的访问体验,如果CDN上没有用户所请求的资源,此时会出现回源。</summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="服务架构" scheme="https://smallflames.github.io/tags/%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/"/>
</entry>
<entry>
<title>Ansible Jinja2模板</title>
<link href="https://smallflames.github.io/2024/01/04/Jinja2%E6%A8%A1%E6%9D%BF/"/>
<id>https://smallflames.github.io/2024/01/04/Jinja2%E6%A8%A1%E6%9D%BF/</id>
<published>2024-01-04T12:49:49.000Z</published>
<updated>2024-01-04T13:22:34.674Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ansible Jinja2模板</center></h1><p> 如果我们的需求是在100台主机上安装nginx,每台nginx的端口都不一样,我们如何解决呢?很棘手的问题,但是jinja2可以,Ansible通常会使用jinja2模板来修改被管理主机的配置文件,尤其在批量修改多台主机的配置文件时,很便捷。Jinja2文件后缀通常为.j2,是一个里面包含变量的模板文件,使用template模块调用。<font color="gray"><em>(template模块和copy模块一样,都是将文件复制到远端主机上去,区别在于template模块可以获取到文件中的变量,而copy则是原封不动的把文件内容复制过去)</em></font></p><span id="more"></span><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">[root@ansible ~]#cat keepalived.j2</span><br><span class="line">global_defs {</span><br><span class="line"> router_id {{ ansible_fqdn }}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 {</span><br><span class="line">{% if ansible_fqdn == "web01" %}</span><br><span class="line"> state MASTER</span><br><span class="line"> priority 150</span><br><span class="line">{% else %}</span><br><span class="line"> state BACKUP</span><br><span class="line"> priority 100</span><br><span class="line">{% endif %}</span><br><span class="line"> interface eth0</span><br><span class="line"> virtual_router_id 50</span><br><span class="line"> advert_int 1</span><br><span class="line"> authentication {</span><br><span class="line"> auth_type PASS</span><br><span class="line"> auth_pass 1111</span><br><span class="line"> }</span><br><span class="line"> virtual_ipaddress { </span><br><span class="line"> 1.1.1.1</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104211506521.png" alt="image-20240104211506521"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104211849054.png" alt="image-20240104211849054"></p><blockquote><p>Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#循环表达式</span></span><br><span class="line">{% <span class="keyword">for</span> i <span class="keyword">in</span> EXPR %}</span><br><span class="line">{% endfor %}</span><br><span class="line"></span><br><span class="line"><span class="comment">#条件判断</span></span><br><span class="line">{% <span class="keyword">if</span> EXPR %}</span><br><span class="line">{% <span class="keyword">elif</span> EXPR %}</span><br><span class="line">{% <span class="keyword">else</span> %}</span><br><span class="line">{% endif %}</span><br><span class="line"></span><br><span class="line"><span class="comment">#注释</span></span><br><span class="line">{<span class="comment"># COMMENT #}</span></span><br></pre></td></tr></table></figure></blockquote><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ansible Jinja2模板</center></h1>
<p> 如果我们的需求是在100台主机上安装nginx,每台nginx的端口都不一样,我们如何解决呢?很棘手的问题,但是jinja2可以,Ansible通常会使用jinja2模板来修改被管理主机的配置文件,尤其在批量修改多台主机的配置文件时,很便捷。Jinja2文件后缀通常为.j2,是一个里面包含变量的模板文件,使用template模块调用。<font color="gray"><em>(template模块和copy模块一样,都是将文件复制到远端主机上去,区别在于template模块可以获取到文件中的变量,而copy则是原封不动的把文件内容复制过去)</em></font></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="服务架构" scheme="https://smallflames.github.io/tags/%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/"/>
</entry>
<entry>
<title>Ansible流程控制</title>
<link href="https://smallflames.github.io/2024/01/04/Ansible%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/"/>
<id>https://smallflames.github.io/2024/01/04/Ansible%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/</id>
<published>2024-01-04T10:42:51.000Z</published>
<updated>2024-01-04T12:37:44.110Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1><center>Ansible流程控制</center></h1><p> 不管是shell还是各大编程语言中,流程控制、条件判断、循环这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用频率同样极其的高,是我们必须掌握的技能。</p><h2 id="一、when判断">一、when判断</h2><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">webs</span></span><br><span class="line"> <span class="attr">tasks:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">when</span> <span class="string">ceshi</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">htop</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">tree</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">"web01"</span> <span class="comment">### 当主机名为web01时执行本条任务</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104191250989.png" alt="image-20240104191250989"></p><span id="more"></span><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">when判断语法:</span><br><span class="line">when: ansible_hostname == <span class="string">"web01"</span> <span class="comment"># 完全匹配</span></span><br><span class="line">when: ansible_hostname != <span class="string">"web01"</span> <span class="comment"># 取反,当主机名不为web01时使用</span></span><br><span class="line">when: ansible_hostname is not match <span class="string">"web01"</span> <span class="comment"># 同上</span></span><br><span class="line">when: ansible_hostname is match <span class="string">"web"</span> <span class="comment"># 匹配主机名包含web的客户端</span></span><br><span class="line">when: ansible_hostname is search <span class="string">"web"</span> <span class="comment"># 搜索主机名包含web的客户端</span></span><br><span class="line"> </span><br><span class="line">when: ansible_default_ipv4.address is match <span class="string">"10.0.0.7"</span> <span class="comment"># 匹配IP地址</span></span><br><span class="line">when: (ansible_default_ipv4.address is match <span class="string">"10.0.0.7"</span>) or (ansible_hostname == <span class="string">"web02"</span>) <span class="comment"># 或者</span></span><br><span class="line">(ansible_default_ipv4.address is match <span class="string">"10.0.0.7"</span>) and (ansible_hostname == <span class="string">"web02"</span>) <span class="comment"># 并且</span></span><br><span class="line">when: <span class="comment"># 并且关系</span></span><br><span class="line"> - ansible_default_ipv4.address is match <span class="string">"10.0.0.7"</span> </span><br><span class="line"> - ansible_hostname == <span class="string">"web02"</span></span><br><span class="line">when: ansible_facts.distribution_major_version > <span class="string">"6"</span> <span class="comment"># 使用数字进行判断比较</span></span><br></pre></td></tr></table></figure><p>案例. 根据nginx的返回结果来判断是否重启nginx</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">webs</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">install</span> <span class="string">web</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">tasks:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nginx</span> <span class="string">offical</span> <span class="string">yum</span></span><br><span class="line"> <span class="attr">yum_repository:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">description:</span> <span class="string">nginx</span> <span class="string">stable</span> <span class="string">repo</span></span><br><span class="line"> <span class="attr">gpgcheck:</span> <span class="literal">no</span></span><br><span class="line"> <span class="attr">baseurl:</span> <span class="string">http://nginx.org/packages/centos/$releasever/$basearch/</span></span><br><span class="line"></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">install</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">configer</span> <span class="string">nginx.conf</span></span><br><span class="line"> <span class="attr">copy:</span></span><br><span class="line"> <span class="attr">src:</span> <span class="string">./nginx.conf</span></span><br><span class="line"> <span class="attr">dest:</span> <span class="string">/etc/nginx/nginx.conf</span></span><br><span class="line"></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">check</span> <span class="string">nginx</span> <span class="string">syntax</span></span><br><span class="line"> <span class="attr">shell:</span> <span class="string">nginx</span> <span class="string">-t</span></span><br><span class="line"> <span class="attr">register:</span> <span class="string">result</span></span><br><span class="line"></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">start</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">systemd:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">restarted</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">yes</span></span><br><span class="line"> <span class="attr">when:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">result</span> <span class="string">is</span> <span class="string">search</span> <span class="string">"ok"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">"web01"</span></span><br><span class="line"><span class="string">...</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104192956127.png" alt="image-20240104192956127"></p><h2 id="二、字典循环">二、字典循环</h2><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 注意:item是固定的变量名称,不能自定义</span></span><br><span class="line"><span class="comment">### 有了item变量,我们就可以字典循环一次安装多个服务,有很高的可拓展性</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">webs</span></span><br><span class="line"> <span class="attr">tasks:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">loop</span> <span class="string">ceshi</span></span><br><span class="line"> <span class="attr">file:</span></span><br><span class="line"> <span class="attr">path:</span> <span class="string">"<span class="template-variable">{{ item.name }}</span>"</span></span><br><span class="line"> <span class="attr">owner:</span> <span class="string">"<span class="template-variable">{{ item.owner }}</span>"</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">"<span class="template-variable">{{ item.group }}</span>"</span></span><br><span class="line"> <span class="attr">mode:</span> <span class="string">"<span class="template-variable">{{ item.mode }}</span>"</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">touch</span></span><br><span class="line"> <span class="attr">loop:</span></span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="number">1.</span><span class="string">txt</span>,<span class="attr">owner:</span> <span class="string">root</span>,<span class="attr">group:</span> <span class="string">root</span>,<span class="attr">mode:</span> <span class="string">'0666'</span> }</span><br><span class="line"> <span class="bullet">-</span> { <span class="attr">name:</span> <span class="number">2.</span><span class="string">txt</span>,<span class="attr">owner:</span> <span class="string">www</span>,<span class="attr">group:</span> <span class="string">www</span>,<span class="attr">mode:</span> <span class="string">'0600'</span> }</span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">"web01"</span></span><br></pre></td></tr></table></figure><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104194111250.png" alt="image-20240104194111250"></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@web01 ~]<span class="comment">#ll</span></span><br><span class="line">total 0</span><br><span class="line">-rw-rw-rw- 1 root root 0 Jan 4 19:40 1.txt</span><br><span class="line">-rw------- 1 www www 0 Jan 4 19:40 2.txt</span><br></pre></td></tr></table></figure><h2 id="三、handlers事件触发模块">三、handlers事件触发模块</h2><p><strong>同样以安装nginx为例,根据配置文件是否修改来触发是否重启nginx</strong></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104200922643.png" alt="image-20240104200922643"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104200156161.png" alt="image-20240104200156161"></p><p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104200053928.png" alt="image-20240104200053928"></p><div class="warning"><p><strong>handlers模块使用注意事项</strong></p><ol><li class="lvl-3">无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次;</li><li class="lvl-3">handlers只有在其所在的任务被执行时,才会被运行;</li><li class="lvl-3">handlers只会在每一个play的末尾运行一次,想在一个playbook中间运行handlers,则需要使用meta模块来实现,如: <code>-meta: flush_handlers</code> ;</li><li class="lvl-3">不能使用handlers替代tasks,handlers和tasks是同级的,在handlers下写的任务只用被调用才能运行</li></ol></div><h2 id="四、playbook文件复用">四、playbook文件复用</h2><blockquote><p>一个yaml文件执行(引用)多个palybook</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[<span class="string">root@ansible</span> <span class="string">~</span>]<span class="comment"># cat site.yml</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">webs</span></span><br><span class="line"> <span class="attr">tasks:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">include_tasks:</span> <span class="string">install_nginx.yml</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">"web01"</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">include_tasks:</span> <span class="string">install_nfs.yml</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">"web02"</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~</span>]<span class="comment"># cat install_nginx.yml </span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">Nginx</span> <span class="string">Server</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line">[<span class="string">root@ansible</span> <span class="string">~</span>]<span class="comment"># cat install_nfs.yml </span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">nfs</span> <span class="string">Server</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nfs-utils</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br></pre></td></tr></table></figure></blockquote><div class='spoiler collapsed'> <div class='spoiler-title'> Playbook任务标签 </div> <div class='spoiler-content'> <p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104203741222.png" alt="image-20240104203741222"></p> </div></div><link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>]]></content>
<summary type="html"><h1><center>Ansible流程控制</center></h1>
<p> 不管是shell还是各大编程语言中,流程控制、条件判断、循环这些都是必不可少的,在我们使用Ansible的过程中,条件判断的使用频率同样极其的高,是我们必须掌握的技能。</p>
<h2 id="一、when判断">一、when判断</h2>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">hosts:</span> <span class="string">webs</span></span><br><span class="line"> <span class="attr">tasks:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">when</span> <span class="string">ceshi</span></span><br><span class="line"> <span class="attr">yum:</span></span><br><span class="line"> <span class="attr">name:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">htop</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">tree</span></span><br><span class="line"> <span class="attr">state:</span> <span class="string">present</span></span><br><span class="line"> <span class="attr">when:</span> <span class="string">ansible_hostname</span> <span class="string">==</span> <span class="string">&quot;web01&quot;</span> <span class="comment">### 当主机名为web01时执行本条任务</span></span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/XZ6606/image-bed/raw/master/img/image-20240104191250989.png" alt="image-20240104191250989"></p></summary>
<category term="运维之路" scheme="https://smallflames.github.io/tags/%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/"/>
<category term="服务架构" scheme="https://smallflames.github.io/tags/%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/"/>
</entry>
</feed>