这篇文档详细解释了SLS文件中,每一个部分的名称与含义,以及SLS中的数据处理后的数据结构。python
我只是SaltStack的初学者,若是文中有错误的地方,请不吝赐教。
在学习的过程,我作了一些实验,犯了很多错,积累了一些经验,对SaltStack的运行也有必定了解,若是有什么问题,或是不太理解的地方,很是欢迎留言交流!web
Top fileapache
Salt State系统的入口文件,其中定义了minion处于哪一个环境,加载哪些SLS模块。django
State treevim
存放在file_roots目录下的一系列SLS文件。使用SLS模块的形式来组织State tree。数据结构
Include声明app
一个list,其元素是要引用到本SLS文件的其余SLS模块。 只能用在highstate结构的顶层。less
示例:ide
include: - edit.vim - http.server
Module引用学习
SLS模块的名字,以在Salt master上的文件结构命名。名为
edit.vim
的模块指向salt://edit/vim.sls
。
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: - apache extend: apache: service: - watch: - file: mywebsite mywebsite: file: - managed
State声明
一个list,至少包含一个定义function声明的string,0个或多个function arg声明的dict。
还有一些可选的成员,好比名字覆盖部分(name和names声明),requistie声明。
只能用在ID声明的下一级。
Requisite声明
一个list,其成员是requisite引用。
用来生成动做依赖树。Salt states被设计成按肯定的顺序执行,require或watch其余Salt state能够调整执行的顺序。
作为list组件用在state声明下一级,或是做为key用在ID声明下一级。
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
只能用做state声明的成员。
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声明
覆盖state声明中的name参数。name参数的默认值是ID声明。 name老是1个单key字典,其值类型是string。
在有的场景下,修改默认的name参数很是有用。好比说,能够避免ID冲突。下面例子中的两个state不能同时使用/etc/motd
做为ID:
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
方便多了:
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声明
将1个state声明扩展为多个不一样名的state声明。
看下面的例子:
python-pkgs: pkg.installed: - names: - python-django - python-crypto - python-yaml
转换成lowstate后的结果是:
python-django: pkg.installed python-crypto: pkg.installed python-yaml: pkg.installed
下面的YAML是一个完整的例子,其中的名字部分使用的是hightstate组件名。
<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>