您好,登錄后才能下訂單哦!
一、什么是Salt States?
Salt States是Salt模塊的擴展,主系統使用的狀態系統叫SLS系統,SLS代表Saltstack State,Salt是一些狀態文件,其中包含有關如何配置Salt子節點的信息,這些狀態被存放在一個目錄下,可以用很多不同的格式來書寫,我們可以把這些Salt States當做是對minion的管理腳本的配置模式,通過配置SLS文件,指定目標minions,可以實現在master上對minions的運行狀態進行管理。
二、Salt State樹
跟系統文件一樣,salt state文件也有自己的樹系統,這通過配置文件中的file_roots配置,默認配置如下:
#在/etc/salt/master中配置下面選項,指定salt state的根目錄在/srv/salt
[root@server01 salt]# grep -A3 file_roots: /etc/salt/master # file_roots: # base: # - /srv/salt/ # dev: -- file_roots: base: - /srv/salt [root@server01 salt]#
修改master配置文件后要重啟,使得配置文件生效
[root@server01 salt]# systemctl restart salt-master.service
Salt State的top文件
top.sls文件是SaltState的默認sls文件,再未作特別指定的時候,salt會默認調用top.sls來執行。例如
salt '*' state.highstate
top.sls文件需要手動創建,位置則是在Salt State樹定義的目錄/srv/salt下:
創建一個top.sls文件
[root@server01 salt]# vim top.sls # 添加內容如下,注意sls文件是用兩個空格來作為縮進 # base 匹配狀態數的base目錄,即/srv/salt # '*' 表示匹配所有的minions,即所有認證通過的客戶端 # - webserver:表示執行base目錄下webserver.sls狀態文件或是webserver/init.sls文件 base: '*': - webserver
例如:創建一個Salt State文件
上面的top.sls文件中定義了一個webserver文件,下面來添加一個webserver文件
在文件/srv/salt/webserver.sls文件中添加如下內容
# Apache:ID聲明,跟yum install Apache指定的名稱一樣
# pkg: 狀態聲明,說明下面的操作是什么狀態操作
# - installed:函數聲明,說明該sls文件要指定執行操作的函數
[root@server01 salt]# vim webserver.sls httpd: pkg: - installed [root@server01 salt]#
執行我們定義的Salt State文件,更新所有的minion的狀態
[root@server01 salt]# salt '*' state.highstate server02: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02:56:15.414698 Duration: 1114.898 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 server03: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02:56:15.448065 Duration: 1129.985 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
說明:根據上面top.sls以及webserver.sls的內容,所有的minion都會安裝Apache服務,如果在top.sls中修改minion的匹配,則僅會匹配到的minion進行操作。當然也可以特別指定sls文件來執行,這里用sls文件為state.sls
[root@server01 salt]# salt 'server02' state.sls webserver server02: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02:58:59.038307 Duration: 885.997 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
#開啟debug日志(minion端執行)
[root@server02 ~]# salt-minion -l debug
#設置默認超時(服務端執行)
[root@server01 salt]# salt '*' state.highstate -t 60
[root@server01 salt]# salt '*' state.highstate -t 60 server03: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03:03:24.248749 Duration: 900.103 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 server02: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03:03:30.229662 Duration: 966.72 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
三、實戰操作
經過上面的簡單介紹,下面來演示一下文件分發和軟件安裝
1、分發/etc/hosts文件
1)準備sls文件
[root@server01 salt]# pwd /srv/salt [root@server01 salt]# cat hosts.sls /etc/hosts: file.managed: - source: salt://testfiles/hosts - user: root - group: root - mode: 644 [root@server01 salt]# [root@server01 salt]# cat top.sls base: '*': - webserver - hosts [root@server01 salt]# [root@server01 salt]# mkdir testfiles [root@server01 salt]# cp /etc/hosts testfiles/ [root@server01 salt]# ll total 12 -rw-r--r-- 1 root root 120 Jun 12 21:21 hosts.sls drwxr-xr-x 2 root root 19 Jun 13 03:08 testfiles -rw-r--r-- 1 root root 41 Jun 13 03:07 top.sls -rw-r--r-- 1 root root 30 Jun 13 02:56 webserver.sls [root@server01 salt]# tree . ├── hosts.sls ├── testfiles │ └── hosts ├── top.sls └── webserver.sls 1 directory, 4 files [root@server01 salt]#
a、使用state.highstate調用
注意:使用state.highstate調用的前提是存在top.sls文件,因此需要提前寫好top.sls文件。
[root@server01 salt]# salt '*' state.highstate server03: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03:11:08.018494 Duration: 937.237 ms Changes: ---------- ID: /etc/hosts Function: file.managed Result: True Comment: File /etc/hosts is in the correct state Started: 03:11:08.984360 Duration: 5.308 ms Changes: Summary ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2 server02: ---------- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03:11:14.019185 Duration: 905.721 ms Changes: ---------- ID: /etc/hosts Function: file.managed Result: True Comment: File /etc/hosts is in the correct state Started: 03:11:14.927716 Duration: 4.522 ms Changes: Summary ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2 [root@server01 salt]#
b、使用state.sls調用
[root@server01 salt]# salt '*' state.sls hosts server03: ---------- ID: /etc/hosts Function: file.managed Result: True Comment: File /etc/hosts is in the correct state Started: 03:12:05.888857 Duration: 17.182 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 server02: ---------- ID: /etc/hosts Function: file.managed Result: True Comment: File /etc/hosts is in the correct state Started: 03:12:05.903366 Duration: 16.217 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
2、安裝軟件包
1)準備sls文件
[root@server01 salt]# vim package.sls pkg-install: pkg.installed: - names: - gcc - lrzsz - tree - dos2unix [root@server01 salt]# [root@server01 salt]# vim top.sls base: 'server02': - package [root@server01 salt]#
a、使用state.highstate調用
[root@server01 salt]# salt 'server02' state.highstate server02: ---------- ID: pkg-install Function: pkg.installed Name: gcc Result: True Comment: Package gcc is already installed. Started: 03:16:42.645005 Duration: 888.648 ms Changes: ---------- ID: pkg-install Function: pkg.installed Name: tree Result: True Comment: Package tree is already installed. Started: 03:16:43.533802 Duration: 0.405 ms Changes: ---------- ID: pkg-install Function: pkg.installed Name: dos2unix Result: True Comment: The following packages were installed/updated: dos2unix Started: 03:16:43.534278 Duration: 188849.681 ms Changes: ---------- dos2unix: ---------- new: 6.0.3-4.el7 old: ---------- ID: pkg-install Function: pkg.installed Name: lrzsz Result: True Comment: Package lrzsz is already installed. Started: 03:19:52.435148 Duration: 0.436 ms Changes: Summary ------------ Succeeded: 4 (changed=1) Failed: 0 ------------ Total states run: 4 [root@server01 salt]#
b、使用state.sls調用
[root@server01 salt]# salt 'server02' state.sls package server02: ---------- ID: pkg-install Function: pkg.installed Name: gcc Result: True Comment: Package gcc is already installed. Started: 03:23:31.945630 Duration: 849.86 ms Changes: ---------- ID: pkg-install Function: pkg.installed Name: tree Result: True Comment: Package tree is already installed. Started: 03:23:32.795666 Duration: 0.58 ms Changes: ---------- ID: pkg-install Function: pkg.installed Name: dos2unix Result: True Comment: Package dos2unix is already installed. Started: 03:23:32.796341 Duration: 0.328 ms Changes: ---------- ID: pkg-install Function: pkg.installed Name: lrzsz Result: True Comment: Package lrzsz is already installed. Started: 03:23:32.796745 Duration: 0.359 ms Changes: Summary ------------ Succeeded: 4 Failed: 0 ------------ Total states run: 4 [root@server01 salt]#
其他日常使用方法
3、管理定時任務
1)準備sls文件(添加定時任務)
[root@server01 salt]# vim cron.sls /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1: cron.present: - identifier: SUPERCRON - user: root - minute: '0' [root@server01 salt]#
2)使用state.sls調用
[root@server01 salt]# salt 'server02' state.sls cron server02: ---------- ID: /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 Function: cron.present Result: True Comment: Cron /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 already present Started: 03:41:04.035604 Duration: 10.506 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
#檢查執行結果
[root@server01 salt]# salt 'server02' cron.raw_cron root server02: # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:SUPERCRON 0 * * * * /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 [root@server01 salt]#
3)準備sls文件(刪除定時任務)
[root@server01 salt]# vim cron.sls /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1: cron.absent: - identifier: SUPERCRON - user: root - minute: '0' [root@server01 salt]# cat top.sls base: 'server02': - cron [root@server01 salt]#
4)使用state.highstate調用
[root@server01 salt]# salt 'server02' state.highstate server02: ---------- ID: /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 Function: cron.absent Result: True Comment: Cron /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 removed from root's crontab Started: 03:45:13.267202 Duration: 26.493 ms Changes: ---------- root: /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov >/dev/null 2>&1 Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 [root@server01 salt]#
#檢查執行結果
[root@server01 salt]# salt 'server02' cron.raw_cron root server02: # Lines below here are managed by Salt, do not edit [root@server01 salt]#
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。