States教程,第1部分 - 基础用法

本教程的目的是示范如何通过Salt States快速管理一个系统。想了解更过关于state系统,请访问 states reference .

本教程将带你了解使用Salt在minion上配置运行Apache HTTP服务并确保服务处于运行状态.

开始之前 需要确保你已经通过 安装配置 章节完成了Salt安装.

小贴士

有很多方法可以 从Salt社区获取帮助 包括我们的 `邮件列表<https://groups.google.com/forum/#!forum/salt-users>`_ 及我们的 IRC频道 #salt.

建立Salt State树

States存储在master上的文本文件中,通过master的文件服务按需传输到minion上. 这些state文件组成了 State树.

在Salt中启动一个中心state系统,需要首先确保已经建立了Salt文件服务. 编辑master配置文件(file_roots) 取消如下行的注释:

file_roots:
  base:
    - /srv/salt

注解

如果你在FreeBSD上通过ports部署了Salt, file_roots 默认路径为 /usr/local/etc/salt/states.

重启Salt master以使更改生效:

pkill salt-master
salt-master -d

准备Top文件

在master上,之前为未注释的目录下(默认为 /srv/salt),创建一个名为 top.sls 的文件并添加如下内容:

base:
  '*':
    - webserver

The top file is separated into environments (discussed later). The default environment is base. Under the base environment a collection of minion matches is defined; for now simply specify all hosts (*).

目标minions

在salt 中可以使用很多方法来匹配minion, minions 可以使用全名,正则,或则doc:grains </topics/targeting/grains>. . 例如:

base:
  'os:Fedora':
    - match: grain
    - webserver

创建一个``sls``文件

In the same directory as the top file, create a file named webserver.sls, containing the following:

apache:                 # ID declaration
  pkg:                  # state declaration
    - installed         # function declaration

The first line, called the ID declaration, is an arbitrary identifier. In this case it defines the name of the package to be installed.

注解

The package name for the Apache httpd web server may differ depending on OS or distro — for example, on Fedora it is httpd but on Debian/Ubuntu it is apache2.

The second line, called the State declaration, defines which of the Salt States we are using. In this example, we are using the pkg state to ensure that a given package is installed.

The third line, called the Function declaration, defines which function in the pkg state module to call.

Renderers

States sls files can be written in many formats. Salt requires only a simple data structure and is not concerned with how that data structure is built. Templating languages and DSLs are a dime-a-dozen and everyone has a favorite.

构建预期的数据结构式Salt renderers 的工作,他们非常容易书写。

在这个教程里面我们将使用jiaja2 的 YAML 模板 作为默认的格式,默认的是可以通过编辑maser 的配置文件里面的:conf_master:`renderer`来改变的。

安装软件包

下面让我们来运行我们创建的状态, 打开命令窗口在master 上运行上面语句。

% salt '*' state.highstate

Our master is instructing all targeted minions to run state.highstate. When a minion executes a highstate call it will download the top file and attempt to match the expressions. When it does match an expression the modules listed for it will be downloaded, compiled, and executed.

第一次编译Minion 将返回所有的动作的摘要和所有的改变内容。

警告

If you have created custom grain modules, they will not be available in the top file until after the first highstate. To make custom grains available on a minion's first highstate, it is recommended to use this example to ensure that the custom grains are synced when the minion starts.

SLS 文件命名

Note that in the example above, the SLS file webserver.sls was referred to simply as webserver. The namespace for SLS files when referenced in top.sls or an Include declaration follows a few simple rules:

  1. .sls 这个文件是被放弃的不被允许的 (i.e. webserver.sls 来源于 webserver)

  2. 使用子目录来做组织是个很好的选择
    1. Each subdirectory can be represented with a dot (following the python import model) or a slash. webserver/dev.sls can also be referred to as webserver.dev
    2. Because slashes can be represented as dots, SLS files can not contain dots in the name besides the dot for the SLS suffix. The SLS file webserver_1.0.sls can not be matched, and webserver_1.0 would match the directory/file webserver_1/0.sls
  3. init.sls 在一个子目录里面表示引导文件,也就表示子目录本身, 所以``webserver/init.sls`` 就是表示``webserver``.

  4. 如果同时存在``webserver.sls`` 和 webserver/init.sls,则 webserver/init.sls 被过滤,webserver.sls``将被用来表示 ``webserver.

调试Salt

如果没有看到预计的输出时,下面的提示可以帮助缩小问题。

开启日志功能

如果你使用``debug``功能,salt可以描述的很详细.

salt-minion -l debug
前台运行minion

minion 开启的时候不加参数 (-d) 可以看到很多minion 开始工作的输出。

salt-minion &

当运行command:salt. 的时候增加默认的超时时间值,例如改变默认的超时时间60s

salt -t 60

最好的输出就是让他们结合起来。

salt-minion -l debug &          # On the minion
salt '*' state.highstate -t 60  # On the master

下一章

This tutorial focused on getting a simple Salt States configuration working. Part 2 will build on this example to cover more advanced sls syntax and will explore more of the states that ship with Salt.