官方文檔 https://docs.saltstack.com/en/latest/topics/states/index.htmlphp
Salt State SLS描述文件(YAML)html
名稱ID聲明 默認是name聲明node
備註: 一個ID聲明下面。狀態模塊不能重複使用mysql
例:linux
apache-install: pkg.installed: - names: - httpd - httpd-devel apache-service: # ID聲明,高級狀態,ID必須惟一。 service.running: # State聲明 狀態聲明 - name: httpd # 選項聲明 - enable: True php: pkg.installed
1)pkg (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.pkg.html#module-salt.states.pkg)redis
pkg.installed # 安裝
pkg.latest # 確保最新版本
pkg.remove # 卸載
pkg.purge # 卸載並刪除配置文件sql
# 同時安裝多個包apache
common_packages: pkg.installed: - pkgs: - unzip - dos2unix - salt-minion: 2015.8.5-1.el6
2)file (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file)bash
salt:// 表示當前環境的根目錄。例如:架構
那麼salt://lamp/files/httpd.conf 表示 /srv/salt/lamp/files/httpd.conf
3)service (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html#module-salt.states.service)
redis: service.running: - enable: True # 開機自啓動 - reload: True # 重載
1.安裝軟件包 pkg
2.修改配置文件 file
3.啓動服務 service
lamp.sls文件內容以下
lamp-pkg: pkg.installed: - pkgs: - httpd - php - mariadb - mariadb-server - php-mysql - php-cli - php-mbstring apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644 mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 apache-service: service.running: - name: httpd - enable: True - reload: True mysql-service: service.running: - name: mariadb - enable: True - reload: True
命令: salt 'linux-node2*' state.sls lamp.lamp
執行結果
1 linux-node2.example.com: 2 ---------- 3 ID: lamp-pkg 4 Function: pkg.installed 5 Result: True 6 Comment: 4 targeted packages were installed/updated. 7 The following packages were already installed: httpd, mariadb-server, mariadb 8 Started: 12:56:16.178765 9 Duration: 194279.377 ms 10 Changes: 11 ---------- 12 libzip: 13 ---------- 14 new: 15 0.10.1-8.el7 16 old: 17 php: 18 ---------- 19 new: 20 5.4.16-36.3.el7_2 21 old: 22 php-cli: 23 ---------- 24 new: 25 5.4.16-36.3.el7_2 26 old: 27 php-common: 28 ---------- 29 new: 30 5.4.16-36.3.el7_2 31 old: 32 php-mbstring: 33 ---------- 34 new: 35 5.4.16-36.3.el7_2 36 old: 37 php-mysql: 38 ---------- 39 new: 40 5.4.16-36.3.el7_2 41 old: 42 php-pdo: 43 ---------- 44 new: 45 5.4.16-36.3.el7_2 46 old: 47 ---------- 48 ID: apache-config 49 Function: file.managed 50 Name: /etc/httpd/conf/httpd.conf 51 Result: True 52 Comment: File /etc/httpd/conf/httpd.conf is in the correct state 53 Started: 12:59:30.519583 54 Duration: 98.547 ms 55 Changes: 56 ---------- 57 ID: php-config 58 Function: file.managed 59 Name: /etc/php.ini 60 Result: True 61 Comment: File /etc/php.ini is in the correct state 62 Started: 12:59:30.620067 63 Duration: 36.824 ms 64 Changes: 65 ---------- 66 ID: mysql-config 67 Function: file.managed 68 Name: /etc/my.cnf 69 Result: True 70 Comment: File /etc/my.cnf is in the correct state 71 Started: 12:59:30.657074 72 Duration: 58.78 ms 73 Changes: 74 ---------- 75 ID: apache-service 76 Function: service.running 77 Name: httpd 78 Result: True 79 Comment: The service httpd is already running 80 Started: 12:59:30.853149 81 Duration: 40.481 ms 82 Changes: 83 ---------- 84 ID: mysql-service 85 Function: service.running 86 Name: mariadb 87 Result: True 88 Comment: The service mariadb is already running 89 Started: 12:59:30.893939 90 Duration: 33.928 ms 91 Changes: 92 93 Summary for linux-node2.example.com 94 ------------ 95 Succeeded: 6 (changed=1) 96 Failed: 0 97 ------------ 98 Total states run: 6 99 Total run time: 194.548 s
第二種方式:
文件lamp2.sls 內容以下:
apache-server: pkg.installed: - pkgs: - httpd - php file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 service.running: - name: httpd - enable: True - reload: True mysql-server: pkg.installed: - pkgs: - mariadb - mariadb-server file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 service.running: - name: mariadb - enable: True - reload: True php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644
命令: salt 'linux-node2*' state.sls lamp.lamp2
執行結果
1 linux-node2.example.com: 2 ---------- 3 ID: apache-server 4 Function: pkg.installed 5 Result: True 6 Comment: All specified packages are already installed 7 Started: 13:13:53.886308 8 Duration: 665.948 ms 9 Changes: 10 ---------- 11 ID: apache-server 12 Function: file.managed 13 Name: /etc/httpd/conf/httpd.conf 14 Result: True 15 Comment: File /etc/httpd/conf/httpd.conf is in the correct state 16 Started: 13:13:54.553919 17 Duration: 19.867 ms 18 Changes: 19 ---------- 20 ID: apache-server 21 Function: service.running 22 Name: httpd 23 Result: True 24 Comment: The service httpd is already running 25 Started: 13:13:54.574411 26 Duration: 29.927 ms 27 Changes: 28 ---------- 29 ID: mysql-server 30 Function: pkg.installed 31 Result: True 32 Comment: All specified packages are already installed 33 Started: 13:13:54.604496 34 Duration: 0.771 ms 35 Changes: 36 ---------- 37 ID: mysql-server 38 Function: file.managed 39 Name: /etc/my.cnf 40 Result: True 41 Comment: File /etc/my.cnf is in the correct state 42 Started: 13:13:54.605362 43 Duration: 15.125 ms 44 Changes: 45 ---------- 46 ID: mysql-server 47 Function: service.running 48 Name: mariadb 49 Result: True 50 Comment: The service mariadb is already running 51 Started: 13:13:54.620592 52 Duration: 29.75 ms 53 Changes: 54 ---------- 55 ID: php-config 56 Function: file.managed 57 Name: /etc/php.ini 58 Result: True 59 Comment: File /etc/php.ini is in the correct state 60 Started: 13:13:54.650496 61 Duration: 17.036 ms 62 Changes: 63 64 Summary for linux-node2.example.com 65 ------------ 66 Succeeded: 7 67 Failed: 0 68 ------------ 69 Total states run: 7 70 Total run time: 778.424 ms
狀態間關係:
1.我依賴誰 require
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg # pkg ID - file: apache-config # file ID
2 我被誰依賴 require_in
mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 - require_in: - service: mysql-service
3 我監控誰 watch
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg - watch: - file: apache-config 1. 若果apache-config這個id的狀態發生變化就reload 2. 若是不加reload: True,那麼就restart
4 我被誰監控 watch_in
5 我引用誰 include
例:lamp第一種方法中,將安裝、配置、啓動分別保存3個文件, 由一個總文件引用
init.sls文件內容
include: - lamp.lamp_pkg - lamp.lamp_config - lamp.lamp_service
lamp_pkg.sls文件內容
lamp-pkg: pkg.installed: - pkgs: - httpd - php - mariadb - mariadb-server - php-mysql - php-cli - php-mbstring
lamp_config.sls文件內容
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644 mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 - require_in: - service: mysql-service
lamp_service.sls文件內容
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg - watch: - file: apache-config mysql-service: service.running: - name: mariadb - enable: True - reload: True
執行命令:salt 'linux-node2*' state.sls lamp.init
6 我擴展誰
如何編寫SLS技巧:
1.按狀態分類 若是單獨使用,很清晰。
2.按服務分類 能夠被其餘的SLS include。例如LNMP include mysql的服務。
文檔:http://docs.jinkan.org/docs/jinja2/
模板包含 變量 或 表達式,兩種分隔符: {% ... %} 和 {{ ... }} 。前者用於執行諸如 for 循環 或賦值的語句,後者把表達式的結果打印到模板上。
salt中如何使用jinja2:
文檔:https://docs.saltstack.com/en/latest/topics/jinja/index.html
1)告訴File模塊,你要使用jinja
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja
2)列出參數列表
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja - defaults: PORT: 8080
3)模板引用
httpd.conf配置文件引用以下
執行命令:salt 'linux-node2*' state.sls lamp.init
執行結果:
1 linux-node2.example.com: 2 ---------- 3 ID: lamp-pkg 4 Function: pkg.installed 5 Result: True 6 Comment: All specified packages are already installed 7 Started: 11:15:02.903236 8 Duration: 4591.748 ms 9 Changes: 10 ---------- 11 ID: apache-config 12 Function: file.managed 13 Name: /etc/httpd/conf/httpd.conf 14 Result: True 15 Comment: File /etc/httpd/conf/httpd.conf updated 16 Started: 11:15:07.558365 17 Duration: 90.859 ms 18 Changes: 19 ---------- 20 diff: 21 --- 22 +++ 23 @@ -39,7 +39,7 @@ 24 # prevent Apache from glomming onto all bound IP addresses. 25 # 26 #Listen 12.34.56.78:80 27 -Listen 80 28 +Listen 8080 29 30 # 31 # Dynamic Shared Object (DSO) Support 32 ---------- 33 ID: php-config 34 Function: file.managed 35 Name: /etc/php.ini 36 Result: True 37 Comment: File /etc/php.ini is in the correct state 38 Started: 11:15:07.649429 39 Duration: 63.754 ms 40 Changes: 41 ---------- 42 ID: mysql-config 43 Function: file.managed 44 Name: /etc/my.cnf 45 Result: True 46 Comment: File /etc/my.cnf is in the correct state 47 Started: 11:15:07.713515 48 Duration: 49.273 ms 49 Changes: 50 ---------- 51 ID: apache-service 52 Function: service.running 53 Name: httpd 54 Result: True 55 Comment: Service reloaded 56 Started: 11:15:07.800629 57 Duration: 135.15 ms 58 Changes: 59 ---------- 60 httpd: 61 True 62 ---------- 63 ID: mysql-service 64 Function: service.running 65 Name: mariadb 66 Result: True 67 Comment: The service mariadb is already running 68 Started: 11:15:07.936165 69 Duration: 95.71 ms 70 Changes: 71 72 Summary for linux-node2.example.com 73 ------------ 74 Succeeded: 6 (changed=2) 75 Failed: 0 76 ------------ 77 Total states run: 6 78 Total run time: 5.026 s
- 模板裏面支持: salt執行模塊 grinas 進行賦值
例:修改配置文件httpd.conf,將IP地址指向本機IP,經過grains['fqdn_ip4'][0]能夠獲取本機IP地址
salt 'linux-node2*' grains.item fqdn_ip4
- 模板裏面支持:salt遠程執行模塊
例:修改配置文件httpd.conf,{{ salt['netwrok.hw_addr']('eth0') }}
salt 'linux-node2*' network.hw_addr eth0
執行命令:salt 'linux-node2*' state.sls lamp.init
執行結果
1 linux-node2.example.com: 2 ---------- 3 ID: lamp-pkg 4 Function: pkg.installed 5 Result: True 6 Comment: All specified packages are already installed 7 Started: 11:51:57.213758 8 Duration: 664.953 ms 9 Changes: 10 ---------- 11 ID: apache-config 12 Function: file.managed 13 Name: /etc/httpd/conf/httpd.conf 14 Result: True 15 Comment: File /etc/httpd/conf/httpd.conf updated 16 Started: 11:51:57.880642 17 Duration: 82.912 ms 18 Changes: 19 ---------- 20 diff: 21 --- 22 +++ 23 @@ -39,7 +39,9 @@ 24 # prevent Apache from glomming onto all bound IP addresses. 25 # 26 #Listen 12.34.56.78:80 27 -Listen 8080 28 +Listen 192.168.137.12:8080 29 + 30 +# MAC IS: 00:0c:29:fd:dd:02 31 32 # 33 # Dynamic Shared Object (DSO) Support 34 ---------- 35 ID: php-config 36 Function: file.managed 37 Name: /etc/php.ini 38 Result: True 39 Comment: File /etc/php.ini is in the correct state 40 Started: 11:51:57.963715 41 Duration: 14.577 ms 42 Changes: 43 ---------- 44 ID: mysql-config 45 Function: file.managed 46 Name: /etc/my.cnf 47 Result: True 48 Comment: File /etc/my.cnf is in the correct state 49 Started: 11:51:57.978393 50 Duration: 12.482 ms 51 Changes: 52 ---------- 53 ID: apache-service 54 Function: service.running 55 Name: httpd 56 Result: True 57 Comment: Service reloaded 58 Started: 11:51:58.021471 59 Duration: 127.043 ms 60 Changes: 61 ---------- 62 httpd: 63 True 64 ---------- 65 ID: mysql-service 66 Function: service.running 67 Name: mariadb 68 Result: True 69 Comment: The service mariadb is already running 70 Started: 11:51:58.148913 71 Duration: 58.592 ms 72 Changes: 73 74 Summary for linux-node2.example.com 75 ------------ 76 Succeeded: 6 (changed=2) 77 Failed: 0 78 ------------ 79 Total states run: 6 80 Total run time: 960.559 ms
- 模板裏面支持: salt執行模塊 pillar進行賦值
例:修改配置文件httpd.conf,{{ pillar['apache'] }}
salt 'linux-node2*' pillar.item apache
執行命令:salt 'linux-node2*' state.sls lamp.init
執行結果:
1 linux-node2.example.com: 2 ---------- 3 ID: lamp-pkg 4 Function: pkg.installed 5 Result: True 6 Comment: All specified packages are already installed 7 Started: 12:01:16.490143 8 Duration: 712.121 ms 9 Changes: 10 ---------- 11 ID: apache-config 12 Function: file.managed 13 Name: /etc/httpd/conf/httpd.conf 14 Result: True 15 Comment: File /etc/httpd/conf/httpd.conf updated 16 Started: 12:01:17.204369 17 Duration: 93.136 ms 18 Changes: 19 ---------- 20 diff: 21 --- 22 +++ 23 @@ -42,6 +42,7 @@ 24 Listen 192.168.137.12:8080 25 26 # MAC IS: 00:0c:29:fd:dd:02 27 +# pillar: httpd 28 29 # 30 # Dynamic Shared Object (DSO) Support 31 ---------- 32 ID: php-config 33 Function: file.managed 34 Name: /etc/php.ini 35 Result: True 36 Comment: File /etc/php.ini is in the correct state 37 Started: 12:01:17.297764 38 Duration: 17.209 ms 39 Changes: 40 ---------- 41 ID: mysql-config 42 Function: file.managed 43 Name: /etc/my.cnf 44 Result: True 45 Comment: File /etc/my.cnf is in the correct state 46 Started: 12:01:17.315170 47 Duration: 15.217 ms 48 Changes: 49 ---------- 50 ID: apache-service 51 Function: service.running 52 Name: httpd 53 Result: True 54 Comment: Service httpd is already enabled, and is running 55 Started: 12:01:17.331369 56 Duration: 184.591 ms 57 Changes: 58 ---------- 59 httpd: 60 True 61 ---------- 62 ID: mysql-service 63 Function: service.running 64 Name: mariadb 65 Result: True 66 Comment: The service mariadb is already running 67 Started: 12:01:17.516431 68 Duration: 32.057 ms 69 Changes: 70 71 Summary for linux-node2.example.com 72 ------------ 73 Succeeded: 6 (changed=2) 74 Failed: 0 75 ------------ 76 Total states run: 6 77 Total run time: 1.054 s