Saltstack 简明教程

SaltStack - Working Example

在此操作示例中,我们将创建一个 Salt 公式,该公式将配置 apache Web 服务器以及 PHP 软件。Salt 是一个执行临时命令的好方法,但您不会真正希望持续以这种方式配置您的基础设施。通过创建一组 Salt 公式,您可以可靠地重现任何配置。

In this working example, we will create a Salt formula that will configure the apache web server along with the PHP software. Salt is a great way to execute ad-hoc commands, but you would not really want to continually configure your infrastructure this way. By creating a set of Salt formulas, you can reliably reproduce any configuration over.

Salt 公式是简单的 YAML 文本文件,并且默认驻留在 /srv/salt/ 中的 Salt 主服务器上。我们开始创建一个 Salt 公式以便同时安装 Apache Web Server 和 PHP。

Salt Formulas are simple YAML text files and by default reside on your Salt Master in /srv/salt/*. Let us start by creating a Salt Formula to install the Apache web server and PHP at the same time.

/srv/salt/ 目录下创建一个名为“websetup.sls”的文件并添加下面的代码。

Create a file named “websetup.sls” under /srv/salt/ directory and add the following code.

websetup.sls

websetup:
   pkg:
      - installed
      - pkgs:
         - apache2
         - php5
         - php5-mysql

在这个示例中,请注意“- pkgs:”参数。“- pkgs:”下面列表中的每一项都会被一起传递给操作系统程序包管理器以一起安装。当您有很多要安装的程序包列表时,这是安装它们最有效的方法。

In this example, notice the “- pkgs:” argument. Each item in the list below “- pkgs:” will be passed together to OS’s package manager to be installed together. Whenever you have a large list of packages to install this is the most efficient way to install them.

使用下面的命令将这个公式应用到 Salt 主服务器。

Apply this Formula to Salt master using the following command.

root@saltmaster:/home/vagrant# salt 'minion2' state.sls websetup

现在,您将看到以下 output

Now, you will see the following output

minion2:
----------
   ID: websetup
   Function: pkg.installed
   Result: True
   Comment: 3 targeted packages were installed/updated.
   Started: 01:50:53.978396
   Duration: 86738.132 ms
   Changes:
      ----------
         apache2:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         apache2-api-20120211:
            ----------
            new:
               1
            old:
         apache2-bin:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         apache2-data:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         libapache2-mod-php5:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         libapr1:
            ----------
            new:
               1.5.0-1
            old:
         libaprutil1:
            ----------
            new:
               1.5.3-1
            old:
         libaprutil1-dbd-sqlite3:
            ----------
            new:
               1.5.3-1
            old:
         libaprutil1-ldap:
            ----------
            new:
               1.5.3-1
            old:
         php5:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-cli:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-common:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-json:
            ----------
            new:
               1.3.2-2build1
            old:
         php5-mhash:
            ----------
            new:
               1
            old:
         php5-mysql:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-readline:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         phpapi-20121212:
            ----------
            new:
               1
            old:
         ssl-cert:
            ----------
            new:
               1.0.33
            old:
Summary for minion2
------------
Succeeded: 1 (changed = 1)
Failed:    0
------------
Total states run:     1
Total run time:  86.738 s

现在,您已在 minion2 中安装了程序包。

Now, you have installed the packages in minion2.

Highstate

“highstate” 是 Salt 用以确定哪些 Salt 公式应该被应用到某个 minions 的一种方法。使用下面的命令执行一个“highstate”。

A “highstate” is a way for Salt to determine which of the Salt Formulas should be applied to a certain minion. Execute a “highstate” using the following command.

root@saltmaster:/home/vagrant# salt <targets> state.highstate

top.sls

正如前面提到的,当 minions 请求执行一个“highstate”时,它向 Salt 主服务器请求 top.sls 并搜索它可以匹配的公式。默认情况下,该文件位于 /srv/salt/top.sls。我们把我们的公式添加到 top.sls 文件中并将 minion2 作为目标。

When the minion request to execute a highstate, as mentioned before, the minion requests the top.sls from the Salt master and searches for formulas that it matches. By default, this file is located at /srv/salt/top.sls. Let us add our formula to the top.sls file and set minion2 as target.

base:
   '*':
      - common
   'minion2’:
      - websetup

现在,执行 highstate 以将 minion2 作为目标,如下所示。

Now, execute the highstate targeting minion2 as shown below.

root@saltmaster:/home/vagrant# salt 'minion2' state.highstate

应用该文件后,您会看到以下 output

After applying this, you could see the following output

minion2:
----------
   ID: common_packages
   Function: pkg.installed
   Result: True
   Comment: All specified packages are already installed
   Started: 01:55:17.998824
   Duration: 461.615 ms
   Changes:

Summary for minion2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 461.615 ms

现在,Apache Web Server 和 PHP 已安装在 minion2 中。通过这种方式,我们必须使用 top.slshighstate 同时对 minions 定位目标,然后在最少的工作量和最大的灵活性下安装所需的软件。

Now, Apache web server and PHP is installed in the minion2. In this way, we have to target minions using both top.sls and highstate and install the required software with minimal work and maximum flexibility.