highstate

来源:互联网 发布:即时通讯软件 编辑:程序博客网 时间:2024/06/05 06:35

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

Top file:Salt State系统的入口文件,其中定义了minion处于哪个环境,加载哪些SLS模块。
State tree:存放在file_roots目录下的一系列SLS文件。使用SLS模块的形式来组织State tree。
include 声明:
include 声明是一个列表,列表内容是将要被引用到当前文件的其他模块,只能用在highstate的顶层。
module 引用
sls模块的名字加路径,以salt master的文件结构命名。

include:  - edit.vim  - http.server

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

Extend声明:
扩展被引用的SLS模块中的name声明。extend声明也是一个dict,其key必须是在被引用的SLS模块中定义的ID。只能用在highstate结构的顶层。
在需要增加或修改另一个SLS文件中定义的state声明时,Extend声明非常有用。下面的代码来自mywebsite.sls文件,其中include并且extend了apache.sls模块(增加了apache监视的对象),使得Apache服务在配置文件mywebsite发生改变时自动重启。

include:  - apacheextend:  apache:    service:      - watch:        - file: mywebsitemywebsite:  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声明:

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

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

httpd:  pkg.installed

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

httpd:  pkg:    - installed

需要注意的是连续的两个简写形式是无效的,为了避免疑惑,建议全部采用完整写法。

 #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的参数:

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

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

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

motd_perms:  file.managed:    - name: /etc/motd    - mode: 644motd_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方便多了:

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

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

python-pkgs:  pkg.installed:    - names:      - python-django      - python-crypto      - python-yaml

yaml 的完整例子

<Include Declaration>:  - <Module Reference>  - <Module Reference><Extend Declaration>:  <ID Declaration>:    [<overrides>]# standard declaration<ID Declaration>:  <State Declaration>:    - <Function>    - <Function Arg>    - <Function Arg>    - <Function Arg>    - <Name>: <name>    - <Requisite Declaration>:      - <Requisite Reference>      - <Requisite Reference># inline function and names<ID Declaration>:  <State Declaration>.<Function>:    - <Function Arg>    - <Function Arg>    - <Function Arg>    - <Names>:      - <name>      - <name>      - <name>    - <Requisite Declaration>:      - <Requisite Reference>      - <Requisite Reference># multiple states for single id<ID Declaration>:  <State Declaration>:    - <Function>    - <Function Arg>    - <Name>: <name>    - <Requisite Declaration>:      - <Requisite Reference>  <State Declaration>:    - <Function>    - <Function Arg>    - <Names>:      - <name>      - <name>    - <Requisite Declaration>:      - <Requisite Reference>
原创粉丝点击