Saltstack自动化(六)Highstate数据结构

默认sls文件是可以按功能单独分开的,而Highstate可以将多个功能单一的sls组合起来,实现一系列的功能。如果mysql.sls、apache.sls、php.sls、os.sls可以组合起来成为一个lamp.sls,我们只需要在lamp.sls中include以上sls文件,执行的时候选择Highstate类的单个sls文件即可。

Salt State Tree

<strong>Top file</strong>:Salt State系统的入口文件,其中定义了minion处于哪个环境,加载哪些SLS模块。



<strong>State tree</strong>:存放在file_roots目录下的一系列SLS文件。使用SLS模块的形式来组织State tree。

Include声明

Include声明:一个list,其元素是要引用到本SLS文件的其他SLS模块。 只能用在highstate结构的顶层。



示例:



<br />
include:
  - edit.vim
  - http.server

Module引用

Module引用:SLS模块的名字,以在Salt master上的文件结构命名。名为edit.vim的模块指向salt://edit/vim.sls。

ID声明

ID声明:定义一个独立的highstate数据段。ID在highstate dict中作为key,其对应的value是包含state声明和requisit声明的另一个dict。用在highstate结构的顶层或extend声明的下一层。ID在整个State tree中必须是唯一的。如果同一个ID用了两次,只有最先匹配到的生效,其他所有的同名ID声明被忽略。

Extend声明

Extend声明:扩展被引用的SLS模块中的name声明。extend声明也是一个dict,其key必须是在被引用的SLS模块中定义的ID。只能用在highstate结构的顶层。



在需要增加或修改另一个SLS文件中定义的state声明时,Extend声明非常有用。下面的代码来自mywebsite.sls文件,其中include并且extend了apache.sls模块(增加了apache监视的对象),使得Apache服务在配置文件mywebsite发生改变时自动重启。



<br />
include:
  - apache
extend:
  apache:
    service:
      - watch:
        - file: mywebsite
mywebsite:
  file:
    - managed

State声明

State声明:一个list,至少包含一个定义function声明的string,0个或多个function arg声明的dict。还有一些可选的成员,比如名字覆盖部分(name和names声明),requistie声明。只能用在ID声明的下一级。

Requisite声明

Requisite声明:一个list,其成员是requisite引用。用来生成动作依赖树。Salt states被设计成按确定的顺序执行,require或watch其他Salt state可以调整执行的顺序。做为list组件用在state声明下一级,或是作为key用在ID声明下一级。

Requisite引用

Requisite引用:只有一个key的dict。key是被引用的state声明的名字,value是被引用的ID声明的名字。 只能用作requisite声明的成员。

Function声明

Function声明:



state中要要执行的function。1个state声明中只能有1个function声明。



下面的例子中,state声明调用了state模块pkg模块中的installed功能:



<br />
httpd:
  pkg.installed
可以用行内缩写方式声明function(上面的例子中就是),使用完整写法使得数据结构更清晰:



<br />
httpd:
  pkg:
    - installed
需要注意的是连续的两个简写形式是无效的,为了避免疑惑,建议全部采用完整写法。



<br />
#INVALID:
httpd:
  pkg.installed
  service.running
#VALID:
httpd:
  pkg:
    - installed
  service:
    - running

Function arg声明

Function arg声明:只有1个key的dict,作为参数传递给function声明,其值为有效的Python类型。其类型必须满足function的需要。 用在function声明下一级。



下面的例子中,state声明是file,function声明是managed,user、group和mode是传递给managed的参数:



<br />
/etc/http/conf/http.conf:
  file.managed:
    - user: root
    - group: root
    - mode: 644
<br />

Name声明

Name声明:覆盖state声明中的name参数。name参数的默认值是ID声明。 name总是1个单key字典,其值类型是string。



在有的场景下,修改默认的name参数非常有用。比如说,可以避免ID冲突。下面例子中的两个state不能同时使用/etc/motd作为ID:



<br />
motd_perms:
  file.managed:
    - name: /etc/motd
    - mode: 644
motd_quote:
  file.append:
    - name: /etc/motd
    - text: "Of all smells, bread; of all tastes, salt."
另外一个使用name声明的场景是,ID声明非常长,又需要在多次引用这个ID。在下面的例子,使用mywebsite比/etc/apache2/sites-available/mywebsite.com方便多了:



<br />
mywebsite:
  file.managed:
    - name: /etc/apache2/sites-available/mywebsite.com
    - source: salt://mywebsite.com
a2ensite mywebsite.com:
  cmd.wait:
    - unless: test -L /etc/apache2/sites-enabled/mywebsite.com
    - watch:
      - file: mywebsite
apache2:
  service:
    - running
    - watch:
      - file: mywebsite

Names声明

Names声明:将1个state声明扩展为多个不同名的state声明。看下面的例子:



<br />
python-pkgs:
  pkg.installed:
    - names:
      - python-django
      - python-crypto
      - python-yaml
转换成lowstate后的结果是:



<br />
python-django:
  pkg.installed
python-crypto:
  pkg.installed
python-yaml:
  pkg.installed
<br />



完整的例子



下面的YAML是一个完整的例子,其中的名字部分使用的是hightstate组件名。



<br />
:
  - 
  - 
:
  :
    []
# standard declaration
:
  :
    - 
    - 
    - 
    - 
    - : 
    - :
      - 
      - 
# inline function and names
:
  .:
    - 
    - 
    - 
    - :
      - 
      - 
      - 
    - :
      - 
      - 
# multiple states for single id
:
  :
    - 
    - 
    - : 
    - :
      - 
  :
    - 
    - 
    - :
      - 
      - 
    - :
      - 
摘选自图灵社区,官方原文链接如下:



<a href="https://docs.saltstack.com/en/latest/ref/states/highstate.html" target="_blank" rel="noopener">https://docs.saltstack.com/en/latest/ref/states/highstate.html</a>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注