States tutorial, part 2 - More Complex States, Requisites

注解

本教程建立在 :doc:`part 1 <states_pt1>`之上,建议从第一章开始阅读。

last part <states_pt1>`我们简单的介绍了基本的安装一个软件包,现在我们将改变我们的``webserver.sls` 设置他的依赖和或则其他更多的salt 状态。

调用多状态

You can specify multiple State declaration under an ID declaration. For example, a quick modification to our webserver.sls to also start Apache if it is not running:

1
2
3
4
5
apache:
  pkg.installed: []
  service.running:
    - require:
      - pkg: apache

尝试先关闭正在运行的apache服务 然后运行``state.highstate`` 仔细看看的输出.

注解

For those running RedhatOS derivatives (Centos, AWS), you will want to specify the service name to be httpd. More on state service here, service state. With the example above, just add "- name: httpd" above the require line and with the same spacing.

依赖其他的状态

我们现在只是进行一个apache应用的安装工作,所以接下来我们将添加一个html 文件来定制我们的web 服务器。我们不想直到使用salt 安装和保持运行的时还没有一个Html文件我们的webserver下面,这不是一个完整的web服务器, 我们可以在 ``webserver/init.sls``的底部添加下面的内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apache:
  pkg.installed: []
  service.running:
    - require:
      - pkg: apache

/var/www/index.html:                        # ID declaration
  file:                                     # state declaration
    - managed                               # function
    - source: salt://webserver/index.html   # function arg
    - require:                              # requisite declaration
      - pkg: apache                         # requisite reference

line 7 is the ID declaration. In this example it is the location we want to install our custom HTML file. (Note: the default location that Apache serves may differ from the above on your OS or distro. /srv/www could also be a likely place to look.)

Line 8 the State declaration. This example uses the Salt file state.

Line 9 is the Function declaration. The managed function will download a file from the master and install it in the location specified.

Line 10 is a Function arg declaration which, in this example, passes the source argument to the managed function.

Line 11 is a Requisite declaration.

Line 12 is a Requisite reference which refers to a state and an ID. In this example, it is referring to the ID declaration from our example in part 1. This declaration tells Salt not to install the HTML file until Apache is installed.

接下来我们来创建``index.html``这个文件并保存到``webserver`` 目录里面:

<!DOCTYPE html>
<html>
    <head><title>Salt rocks</title></head>
    <body>
        <h1>This file brought to you by Salt</h1>
    </body>
</html>

最后,我们再次运行 state.highstate minion 将接受并运行highstate 通过master的文件服务器来安装我们的html文件。

salt '*' state.highstate

最后我们看到apache 服务器运行着我们自定义的html。

require VS watch

There are two Requisite declaration, “require”, and “watch”. Not every state supports “watch”. The service state does support “watch” and will restart a service based on the watch condition.

例如,如果你想使用**Salt**创建一个虚拟主机配置文件并希望当文件发生改变时重启apache web服务 ,你可以我们之前的apache 配置 ,示例如下:

/etc/httpd/extra/httpd-vhosts.conf:
  file.managed:
    - source: salt://webserver/httpd-vhosts.conf

apache:
  pkg.installed: []
  service.running:
    - watch:
      - file: /etc/httpd/extra/httpd-vhosts.conf
    - require:
      - pkg: apache

If the pkg and service names differ on your OS or distro of choice you can specify each one separately using a Name declaration which explained in Part 3.

下一章

In part 3 we will discuss how to use includes, extends, and templating to make a more complete State Tree configuration.