zoukankan      html  css  js  c++  java
  • Salt Highstate数据结构定义

    作者言

    这篇文档详细解释了SLS文件中,每个部分的名称与含义,以及SLS中的数据处理后的数据结构。

    我只是SaltStack的初学者,如果文中有错误的地方,请不吝赐教。
    在学习的过程,我做了一些实验,犯了不少错,积累了一些经验,对SaltStack的运行也有一定了解,如果有什么问题,或是不太理解的地方,非常欢迎留言交流!

    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>
  • 相关阅读:
    厕所惊魂
    感谢协助学生返校,邀请交警合影留念
    小丑杀人
    东芝Toshiba e-STUDIO打印身份证
    JavaScript
    html
    pymysql模块
    线程
    队列与进程池
    网络编程
  • 原文地址:https://www.cnblogs.com/andy6/p/11354825.html
Copyright © 2011-2022 走看看