如何理解Salt Highstate数据结构定义

发布时间:2021-11-26 15:46:38 作者:柒染
来源:亿速云 阅读:137

今天就跟大家聊聊有关如何理解Salt Highstate数据结构定义,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

小编将详细介绍SLS文件中每个部分的名称与含义,以及SLS中的数据处理后的数据结构。

Salt State Tree

Top file

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

State tree

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

Include声明

Include声明

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

示例:

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发生改变时自动重启。

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功能。

httpd:
  pkg.installed

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

httpd:
  pkg:
    - installed

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

httpd:
  pkg.installed
  service.running

VALID:

httpd:
  pkg:
    - installed
  service:
    - running

只能用作state声明的成员。

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: 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声明

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>

看完上述内容,你们对如何理解Salt Highstate数据结构定义有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 三、配置管理远程命令、配置管理任务计划、其他命令、salt-ssh使用
  2. Salt 基础入门

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

salt

上一篇:C++什么时候使用make_unique()​

下一篇:C#如何实现基于Socket套接字的网络通信封装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》