https://www.cnblogs.com/agnewee/p/6487262.html
官方資料:https://docs.saltstack.com/en/latest/ref/configuration/index.html
官網資料:https://repo.saltstack.com/#rhel
yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
准備工作:
yum -y install libffi-devel zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
WARNING: Redhat Python 3 support requires that EPEL be previously installed. The Python 3 packages now comes with Python 3.6
安裝最新版本。更新安裝最新版本,即使它是一個新的主要版本。
- 運行以下命令來安裝SaltStack存儲庫和密鑰:
sudo yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
-
跑
sudo yum clean expire-cache
- 安裝鹽仆從、鹽母或其他鹽組件:
sudo yum install salt-master
sudo yum install salt-minion
sudo yum install salt-ssh
sudo yum install salt-syndic
sudo yum install salt-cloud
sudo yum install salt-api
- (僅升級)重新啟動所有升級的服務,例如:
sudo systemctl restart salt-minion
pip install -U halite pip install cherrypy pip install paste pip install gevent #如果裝不上就先安裝saltstack之后再裝它 pip install pyopenssl
分別安裝python2 python3版本的pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py python3 get-pip.py [root@localhost halite-master]# pip --version pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7) [root@localhost halite-master]# pip3 --version pip 19.0.3 from /usr/local/python/lib/python3.7/site-packages/pip (python 3.7)
安裝saltstack
yum install salt-master salt-minion salt-api -y
[root@localhost ~]# vim /etc/salt/master external_auth: pam: testuser: //稍后創建此用戶 - .* - '@runner' halite: #以下內容需要手動添加 level: 'debug' server: 'cherrypy' host: '0.0.0.0' port: '8080' cors: False tls: True certpath: '/etc/pki/tls/certs/localhost.crt' keypath: '/etc/pki/tls/certs/localhost.key' pempath: '/etc/pki/tls/certs/localhost.pem' [root@localhost ~]# useradd testuser [root@localhost ~]# passwd testuser
二、 saltstack的配置
1、 服務端的配置
1) #更新主控關鍵項
vim /etc/salt/master #綁定Master通信IP interface: 192.168.1.205 #自動認證,避免手動運行salt-key來確認證書信息 auto_accept: True #指定Saltstack文件根目錄 file_roots base: -/srv/salt
systemctl restart salt-master
systemctl start salt-master systemctl enable salt-master systemctl start salt-api systemctl enable salt-api
2、 被控制端的配置
vim /etc/salt/minion #指定master主機IP master: 192.168.1.205 #修改被控主機識別id,建議使用操作系統主機名來配置 id: node1 //另外一台為node2
systemctl start salt-minion systemctl enable salt-minion
三、校驗安裝結果
通過test模塊的ping的方法,可以確認服務器與被控端是否建議信任關系。
#ping單個客戶
sudo salt ‘node1’ test.ping sudo salt ‘node2’ test.ping
#ping所有被控端
salt '*' test.ping
master配置文件:
interface: 0.0.0.0 # 默認值:0.0.0.0(所有的網絡地址接口); 綁定到本地的某個網絡地址接口 publish_port: 4505 # ; 設置master與minion的認證通信端口 user: root # ;運行salt進程的用戶 max_open_files: 100000 #salt-master可以打開的最大句柄數 worker_threads: 5 #啟動用來接收或應答minion的線程數 ret_port: 4506 #master用來發送命令或者接收minions的命令執行返回信息 pidfile: /var/run/salt-master.pid #指定master的pid文件位置 root_dir: / # 該目錄為salt運行的根目錄,改變它可以使salt從另外一個目錄開始運行,好比chroot pki_dir: /etc/salt/pki/master # 這個目錄是用來存放pki認證秘鑰 cachedir: /var/cache/salt/master # 用來存放緩存信息,salt工作執行的命令信息 verify_env: True # 在啟動驗證和設置權限配置目錄 keep_jobs: 24 #設置保持老的工作信息的過期時間,單位小時 job_cache: True #設置master維護的工作緩存,這是一個很好的功能,當你的Minons超過5000台時,他將很好的承擔這個大的架構,關閉這個選項,之前的工作執行以及工作系統將無法被利用,一般不推薦關掉改選項,開啟改選項將會是很明智的,他將使master獲得更快的IO系統 timeout: 5 #默認值:5 ;master執行命令的可以接受的延遲時間 output: nested #默認值:nested ;salt命令的輸出格式 sock_dir: /var/run/salt/master #默認值:/var/run/salt/master ; 指定unix socket主進程通信的socket創建路徑 minion_data_cache: True # 默認值:True ;minion data cache是關於minion信息存儲在master上的參數,這些信息主要是pillar 和 grains數據.這些數據被緩存在cachedir定義的目錄下的minion目錄下以minion名為名的目錄下並且預先確定哪些minions將從 執行回復 include: /etc/salt/extra_config #默認值:/etc/salt/extra_config ;包含其他路徑上的配置文件
Security settings
#open_mode: False #open_mode是一個危險的安全特性,當master遇到pki認證系統,秘鑰混淆和身份驗證失效時,打開open_mode,master將會接受 所有的身份驗證。這將會清理掉pki秘鑰接受的minions。 通常情況下open_mode不應該被打開,它只適用於短時間內清理pki keys,若要打開它,可將值調整為True #auto_accept: False #這個設置將會使master自動接受所有發送公鑰的minions #autosign_file: /etc/salt/autosign.conf #如果autosign_file的值被指定,那么autosign_file將會通過該輸入允許所有的匹配項,首先會搜索字符串進行匹配,然后通過正則表達式進行匹配。這是不安全的
#client_acl: # 默認值:{} ;開啟對系統上非root的系統用戶在master上執行特殊的模塊,這些模塊名可以使用正則表達式進行表示
1
2
3
4
|
client_acl:
larry:
-
test
.
ping
- network.*
|
#client_acl_blacklist #默認值:{} ;黑名單用戶或模塊,表示所有非sudo用戶以及root都無法通過cmd這個模塊執行命令,默認情況改配置是完全禁用的
1
2
3
4
5
6
|
client_acl_blacklist:
users
:
- root
-
'^(?!sudo_).*$'
# all non sudo users
modules:
- cmd
|
#external_auth: #默認值:{} ; salt的認證模塊采用外部的認證系統用來做認證和驗證用戶在salt系統中的訪問區域
1
2
3
|
pam:
fred:
-
test
.*
|
#file_recv: False #默認值:False ; 允許minions推送文件到master上,這個選項默認是禁用的,出於安全考慮 #file_recv_max_size: 100 # 默認值:100 設置一個hard-limit文件的大小,可以推到master。
State System settings
state_top: top.sls #默認值:top.sls ; 狀態系統使用一個入口文件告訴minions在什么環境下使用什么模塊,這個狀態入口文件被定義在基礎環境的相對根路徑下 renderer: yaml_jinja #默認值:yaml_jinja ;使用渲染器用來渲染minions的狀態數據 failhard: False #默認值:False ; 設置一個全局的failhard表示,當單個的狀態執行失敗后,將會通知所有的狀態停止運行狀態 state_verbose: True #state_verbose允許從minions返回更多詳細的信息,通常清空下只返回失敗或者已經更改,但是將state_verbose設置為True,將會返回所有的狀態檢查 state_output: full #state_output的設置將會改變信息輸出的格式, 當被設置為”full”時,將全部的輸出一行一行的顯示輸出; 當被設置為”terse“時,將會 被縮短為一行進行輸出; 當被設置為”mixed”時,輸出樣式將會是簡潔的,除非狀態失敗,這種情況下將會全部輸出; 當被設置為”change”時,輸出 將會完全輸出除非狀態沒有改變
File Server settings
# file_roots: # salt運行一個輕量級的文件服務器通過ZeroMQ對minions進行文件傳輸,因此這個文件服務器是構造在master的守護進程中,並且不需要依賴於專用的端口
文件服務器的工作環境傳遞給master,每一個環境可以有多個根目錄,但是相同環境下多個文件的子目錄不能相同,否則下載的文件將不能被可靠的保證,一個基礎環境依賴於主的入口文件,
1
2
3
4
5
6
7
8
9
10
|
Example:
file_roots:
base:
-
/srv/salt/
dev:
-
/srv/salt/dev/services
-
/srv/salt/dev/states
prod:
-
/srv/salt/prod/services
-
/srv/salt/prod/states
|
#hash_type: md5 # 默認值:md5 ; hash_type是用來當發現在master上需要對一個文件進行hash時的hash使用的算法,默認是md5.但是它也支持sha1,sha224,shar256,shar384,shar512
#file_buffer_size: 1048576 # 默認值:1048576 ;文件服務器的緩存區大小
#fileserver_backend: # 默認值: fileserver_backend:
\n\t - roots
;salt支持模塊化的后端文件系統服務器,它允許salt通過第三方的系統來管理收集文件並提供給minions使用,可以配置多個后端文件系統,這里支 持gitfs、hgfs、roots、s3fs文件調用的搜索順序按照后台文件系統的配置順序來搜索,默認的設置只開啟了標准的后端服務器roots,具 體的根選項配置通過file_roots參數設置
1
2
3
|
fileserver_backend:
- git
- roots
|
##### Pillar settings #####
#pillar_roots: # 默認值: base: \n\t - /srv/pillar 設置不同的環境對應的存放pillar數據的目錄,這個配置和file_roots參數配置一樣
1
2
3
4
5
6
7
|
pillar_roots:
base:
-
/srv/pillar
dev:
-
/srv/pillar/dev
prod:
-
/srv/pillar/prod
|
##### Logging settings #####
#log_file: /var/log/salt/master # 默認值:/var/log/salt/master ;master的日志可以發送到一個普通文件,本地路徑名或者網絡位置
1
2
3
4
|
eg:
log_file:
/var/log/salt/master
log_file:
log_file: udp:
//loghost
:10514
|
#log_level: warning #默認值:warning ; 按照日志級別發送信息到控制台 可選項:# One of 'garbage', 'trace', 'debug', info', 'warning', 'error', 'critical'.
1
|
log_level: warning
|
#log_level_logfile: warning #默認值:warning ; 按照日志級別發送信息到日志文件
#log_datefmt: '%H:%M:%S' #默認值:%H:%M:%S ;發送到控制台信息所用的日期時間格式,更多詳情
log_fmt_console
默認值: [%(levelname)-8s] %(message)s
控制台日志信息格式,
1
|
log_fmt_console:
'[%(levelname)-8s] %(message)s'
|
log_fmt_logfile
默認值: %(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s
%(asctime)s:2003-07-08 16:49:45 %(msecs)03.0f:當前時間的毫秒部分 %(name):日志記錄調用器的名字 %(levelname):日志記錄級別 %(message)s:日志詳細信息
1
|
log_fmt_logfile:
'%(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s'
|
log_granular_levels #默認值:{} ; 這可以更加具體的控制日志記錄級別
##### Node Groups #####
1
2
|
nodegroups:
webgroup1:
'L@_host_serv_1.lansgg.com,_host_serv_1.lansgg.com'
|
上面配置是我這邊測試用;編輯后,重啟master
1
|
[root@master salt]
# salt -N webgroup1 test.ping
|
高級用法:
關於組的混合匹配:(Compound matchers)
匹配中可以使用and、or及not等boolean型操作
例如,想匹配所有minion中主機名(minion id)以webserv開頭並且運行在Debian系統上或者minion的主機名(minion id)匹配正則表達式web-dc1-srv.*:
salt -C ‘webserv* and G@os:Debian or E@web-dc1-srv.*’ test.ping
在top.sls中可以如下使用:
base:
‘webserv* and G@os:Debian or E@web-dc1-srv.*’:
– match: compound
– webserver
遇到的問題:
ModuleNotFoundError: No module named 'salt'
-- The result is failed. Apr 20 01:34:27 localhost.localdomain systemd[1]: Unit salt-master.service entered failed state. Apr 20 01:34:27 localhost.localdomain systemd[1]: salt-master.service failed. Apr 20 01:34:27 localhost.localdomain polkitd[5770]: Unregistered Authentication Agent for unix-process:6074:4301 (system bus name :1.13, object path /org/freedesktop Apr 20 01:35:04 localhost.localdomain polkitd[5770]: Registered Authentication Agent for unix-process:6082:8015 (system bus name :1.14 [/usr/bin/pkttyagent --notify-f Apr 20 01:35:04 localhost.localdomain systemd[1]: Reloading. Apr 20 01:35:04 localhost.localdomain systemd[1]: Configuration file /usr/lib/systemd/system/auditd.service is marked world-inaccessible. This has no effect as config Apr 20 01:35:04 localhost.localdomain systemd[1]: Configuration file /usr/lib/systemd/system/wpa_supplicant.service is marked executable. Please remove executable per Apr 20 01:35:04 localhost.localdomain polkitd[5770]: Unregistered Authentication Agent for unix-process:6082:8015 (system bus name :1.14, object path /org/freedesktop Apr 20 01:35:12 localhost.localdomain polkitd[5770]: Registered Authentication Agent for unix-process:6100:8814 (system bus name :1.15 [/usr/bin/pkttyagent --notify-f Apr 20 01:35:12 localhost.localdomain systemd[1]: Starting The Salt Master Server... -- Subject: Unit salt-master.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit salt-master.service has begun starting up. Apr 20 01:35:12 localhost.localdomain systemd[1]: salt-master.service: main process exited, code=exited, status=1/FAILURE Apr 20 01:35:12 localhost.localdomain salt-master[6105]: Traceback (most recent call last): Apr 20 01:35:12 localhost.localdomain salt-master[6105]: File "/usr/bin/salt-master", line 6, in <module> Apr 20 01:35:12 localhost.localdomain salt-master[6105]: from salt.scripts import salt_master Apr 20 01:35:12 localhost.localdomain salt-master[6105]: ModuleNotFoundError: No module named 'salt' Apr 20 01:35:12 localhost.localdomain systemd[1]: Failed to start The Salt Master Server. -- Subject: Unit salt-master.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit salt-master.service has failed. -- -- The result is failed. Apr 20 01:35:12 localhost.localdomain systemd[1]: Unit salt-master.service entered failed state.
解決方法:
pip install salt