Saltstack_使用指南03_配置管理

 

1. 主機規劃

 

注意事項html

修改了master或者minion的配置文件,那麼必須重啓對應的服務。

 

2. 瞭解YAML

具體地址nginx

https://docs.saltstack.com/en/latest/topics/yaml/index.html    

 

YAML:三板斧web

1、縮進:  2個空格,不能使用Tab
2、冒號:  key: value   注意有空格
3、短橫線:  - list1     注意有空格
          - list2

 

3. 配置管理路徑指定

3.1. master指定配置管理基本路徑

 1 [root@salt100 salt]# pwd
 2 /etc/salt
 3 [root@salt100 salt]# vim master
 4 ………………
 5 # Example:
 6 # file_roots:
 7 #   base:
 8 #     - /srv/salt/
 9 #   dev:
10 #     - /srv/salt/dev/services
11 #     - /srv/salt/dev/states
12 #   prod:
13 #     - /srv/salt/prod/services
14 #     - /srv/salt/prod/states
15 # 
16 # 將以下的註釋放開便可
17 # 也能夠不變動配置,由於這是個默認配置★★
18 file_roots:
19   base:
20     - /srv/salt
21 ………………
22 [root@salt100 salt]# systemctl restart salt-master.service  # 修改了配置文件,必須重啓服務 

 

3.2. 建立對應的目錄

[root@salt100 ~]# mkdir -p /srv/salt

 

4. 測試案例--單個狀態管理

4.1. 編寫測試案例----安裝apache

一、爲了方便後期維護指定一個特定目錄apache

1 [root@salt100 salt]# pwd
2 /srv/salt
3 [root@salt100 salt]# mkdir web
4 [root@salt100 salt]# cd web/
5 [root@salt100 web]# pwd
6 /srv/salt/web

 

二、編寫sls文件vim

 1 [root@salt100 web]# pwd
 2 /srv/salt/web
 3 [root@salt100 web]# cat apache.sls  # 後綴名 sls,salt執行時會找sls文件
 4 # sls文件容許註釋存在,內容能夠直接複製使用
 5 # 自定義的一個ID,惟一標識 ★★★★★
 6 apache-install:
 7   # pkg 是一個執行模塊   . 引用   installed 使用方法
 8   pkg.installed:
 9     # - names 參數 支持多個列表
10     - names:
11       - httpd
12       - httpd-devel
13 
14 # enable: True 表示:開機自啓動
15 apache-service:
16   service.running:
17     - name: httpd
18     - enable: True

 

4.2. 在master機器salt100上執行

給salt0一、salt0二、salt03部署安裝httpdcentos

 1 # 在master機器操做
 2 [root@salt100 ~]# salt 'salt0*' test.ping  # 看salt0一、salt0二、salt03是否可通訊
 3 salt01:
 4     True
 5 salt03:
 6     True
 7 salt02:
 8     True
 9 [root@salt100 ~]# salt 'salt0*' state.sls web.apache  # 給 salt0一、salt0二、salt03 部署httpd
10 # 說明:
11 # 1、master 配置管理基本目錄爲 /srv/salt
12 # 2、apache.sls 的路徑爲:/srv/salt/web/apache.sls
13 # 3、state.sls web.apache  說明 state模塊,調用 sls方法, 調用的文件是web路徑下的apache.sls文件【只是最後的 sls後綴名省略了】

 

備註:作了哪些事情測試

一、  將/srv/salt/web/apache.sls文件從master發送給minion;spa

二、  minion獲得文件後,根據master指令執行apache.sls中的內容3d

 

master將文件發送到minion的那個位置rest

 1 # 在minion端查看
 2 [root@salt01 salt]# pwd
 3 /var/cache/salt
 4 [root@salt01 salt]# ll
 5 total 0
 6 drwxr-xr-x 6 root root 103 Dec 11 23:52 minion
 7 [root@salt01 salt]# tree
 8 .
 9 └── minion
10     ├── accumulator
11     ├── extmods
12     ├── files
13     │   └── base
14     │       └── web
15     │           └── apache.sls
16     ├── highstate.cache.p
17     ├── proc
18     └── sls.p
19 
20 7 directories, 3 files

 

4.3. 執行結果信息講解

  1 # 執行返回結果是無序的
  2 [root@salt100 ~]# salt 'salt0*' state.sls web.apache 
  3 salt02:  # salt02執行結果信息
  4 ----------
  5           ID: apache-install  # apache.sls 中自定的ID【名稱】
  6     Function: pkg.installed
  7         Name: httpd
  8       Result: True
  9      Comment: The following packages were installed/updated: httpd
 10      Started: 23:51:46.604986
 11     Duration: 30335.469 ms
 12      Changes:   
 13               ----------
 14               httpd:
 15                   ----------
 16                   new:
 17                       2.4.6-88.el7.centos
 18                   old:
 19               httpd-tools:
 20                   ----------
 21                   new:
 22                       2.4.6-88.el7.centos
 23                   old:
 24               mailcap:
 25                   ----------
 26                   new:
 27                       2.1.41-2.el7
 28                   old:
 29 ----------
 30           ID: apache-install
 31     Function: pkg.installed
 32         Name: httpd-devel
 33       Result: True
 34      Comment: The following packages were installed/updated: httpd-devel
 35      Started: 23:52:16.965844
 36     Duration: 6661.51 ms
 37      Changes:   
 38               ----------
 39               apr-devel:
 40                   ----------
 41                   new:
 42                       1.4.8-3.el7_4.1
 43                   old:
 44               apr-util-devel:
 45                   ----------
 46                   new:
 47                       1.5.2-6.el7
 48                   old:
 49               cyrus-sasl:
 50                   ----------
 51                   new:
 52                       2.1.26-23.el7
 53                   old:
 54               cyrus-sasl-devel:
 55                   ----------
 56                   new:
 57                       2.1.26-23.el7
 58                   old:
 59               expat-devel:
 60                   ----------
 61                   new:
 62                       2.1.0-10.el7_3
 63                   old:
 64               httpd-devel:
 65                   ----------
 66                   new:
 67                       2.4.6-88.el7.centos
 68                   old:
 69               libdb-devel:
 70                   ----------
 71                   new:
 72                       5.3.21-24.el7
 73                   old:
 74               openldap:
 75                   ----------
 76                   new:
 77                       2.4.44-20.el7
 78                   old:
 79                       2.4.44-13.el7
 80               openldap-devel:
 81                   ----------
 82                   new:
 83                       2.4.44-20.el7
 84                   old:
 85 ----------
 86           ID: apache-service
 87     Function: service.running
 88         Name: httpd
 89       Result: True
 90      Comment: Service httpd has been enabled, and is running
 91      Started: 23:52:24.619598
 92     Duration: 314.737 ms
 93      Changes:   
 94               ----------
 95               httpd:
 96                   True
 97 
 98 Summary for salt02
 99 ------------
100 Succeeded: 3 (changed=3)
101 Failed:    0
102 ------------
103 Total states run:     3
104 Total run time:  37.312 s
105 salt01:  # salt01執行結果信息
106 ………………
107 salt03:  # salt03執行結果信息
108 ----------
109 ………………

 

5. Salt高級狀態管理

生產環境使用,注意事項:

1、執行時不能使用 '*' ,全部minion執行
2、不能直接執行,要先 test=True;

 

5.1. 高級狀態sls文件的位置和名稱

       默認file_roots的位置

       不用修改配置文件,默認便可。

 

5.2. 編寫top.sls文件

 1 [root@salt100 salt]# pwd
 2 /srv/salt
 3 [root@salt100 salt]# tree
 4 .
 5 ├── top.sls
 6 └── web
 7     └── apache.sls
 8 
 9 1 directory, 2 files
10 [root@salt100 salt]# cat top.sls 
11 base:
12   # 使用通配符
13   'salt0*':
14     - web.apache
15     # - web.nginx  # 能夠有多個
16   # 指定具體minion
17   'salt03':
18     - web.apache

 

5.3. 執行高級狀態

1 [root@salt100 ~]# salt 'salt01' state.highstate test=True  # 必須先執行這個
2 ………………
3 # 緣由:防止本身手動改了minion機器上組件的配置信息,可是沒有同步到salt;
4 # 結果直接執行後,又改回去了。
5 [root@salt100 ~]# salt 'salt01' state.highstate  # 執行高級狀態,經過top.sls 去查找
6 ………………
相關文章
相關標籤/搜索