1. 程式人生 > >saltstack之top及highstate命令和highdata lowdata

saltstack之top及highstate命令和highdata lowdata

top.sls在salt中有兩種,即state的top.sls和pillar的top.sls。他們的格式是一樣的。

環境: #在master配置檔案中設定環境
  ‘*’    #target
      - state  #state 檔案列表
      
      
master配置檔案/etc/salt/master
file_roots:
  base:
    - /srv/salt

pillar_roots:
  base:
    - /srv/pillar

top.sls將不同的狀態檔案定位到不同的minions中,highstate函式正是通過top.sls檔案作為入口對主機和模組進行管理。


high data 和 low data,high資料一般指可見資料,low資料被salt提取並使用的資料。

第一:highstate有3個元件:

high data 
sls檔案
high state


檢視highstate使用show_highstate

[[email protected] salt]# salt '136' state.show_highstate --out yaml
'136':
  install man:
    __env__: base
    __sls__: install-man
    pkg:
    - pkgs:
      - man
      - lrzsz
    - installed
    - order: 10000
[
[email protected]
 salt]#


第二:生成high state後,會被送入salt編譯器,salt會重新格式化成為salt內部資料結構,用於評估每個宣告,推送到state模組,low資料也被拆分為以下部分:

low state
low區塊
state模組
執行模組

檢視lowdata

image.png


state執行模組:

1.state.apply會將highdata快取到minion端(high data是組成sls檔案的塊,而多個sls檔案通過top.sls檔案內的一個環境使用就組成了 highstate了)

state.apply會呼叫state.highstate 或者 state.sls,這個基於後面的引數。分為以下兩種

   a. salt '*' state.apply 會執行top.sls state檔案。  

   b. salt '*' state.apply install-man 執行salt://install-man 或者salt://install-man/init.sls

2.當執行state.highstate方法時,salt會自動編譯在top.sls中的所有sls檔案,編譯到單獨的definition中,稱為highstate。

3.state.sls 執行一個或多個sls檔案


示例:

[[email protected] salt]# cat top.sls install-man.sls
base:
  '*':
    - install-man
install man:
  pkg.installed:
{% if grains['id'] == '136' %}
    - pkgs:
      - man
      - lrzsz
{% endif %}
[[email protected] salt]# salt '136' state.apply
136:
    ----------
    pkg_|-install man_|-install man_|-installed:
        ----------
        __run_num__:
            0
        changes:
            ----------
            lrzsz:
                ----------
                new:
                    0.12.20-27.1.el6
                old:
            man:
                ----------
                new:
                    1.6f-39.el6
                old:
        comment:
            The following packages were installed/updated: lrzsz, man
        duration:
            5956.067
        name:
            install man
        result:
            True
        start_time:
            16:10:06.152850
[[email protected] salt]# salt '136' state.highstate        #也可以進行安裝
136:
    ----------
    pkg_|-install man_|-install man_|-installed:
        ----------
        __run_num__:
            0
        changes:
            ----------
            lrzsz:
                ----------
                new:
                    0.12.20-27.1.el6
                old:
            man:
                ----------
                new:
                    1.6f-39.el6
                old:
        comment:
            The following packages were installed/updated: lrzsz, man
        duration:
            20222.15
        name:
            install man
        result:
            True
        start_time:
            16:11:08.375206