states tutorial, part 2 - more complex states, requisites

本教程建立在第1部分涵盖的主题上。建议您从此处开始。
在Salt States教程的最后一部分,我们简单的介绍了基本的安装一个软件包,我们现在将修改我们的webserver.sls文件以满足要求,并使用更多的Salt States

call multiple states(调用多状态)

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:

你可以在ID声明下指定多个State声明。 例如,对我们的webserver.sls进行快速修改,以便在未运行时启动Apache:

apache:
pkg.installed: []
service.running:
- require:
- pkg: apache

Try stopping Apache before running state.apply once again and observe the output.

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

require other states

We now have a working installation of Apache so let's add an HTML file to customize our website. It isn't exactly useful to have a website without a webserver so we don't want Salt to install our HTML file until Apache is installed and running. Include the following at the bottom of your webserver/init.sls file:

我们现在只是进行一个apache应用的安装工作,所以接下来我们将添加一个html 文件来定制我们的web服务器。没有web服务器的网站并不是很有用,所以我们不希望Salt在Apache安装并运行之前安装我们的HTML文件。我们可以在webserver/init.sls的底部添加下面的内容:

 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.)

第7行是ID声明。在这个例子中,它是我们想要安装我们的自定义HTML文件的位置。(注意:Apache服务的默认位置可能与您的操作系统或发行版上述不同。/ srv / www也可能是一个可能的地方。)

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

第8行是State声明。这个例子使用salt的file状态模块文件。

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

第9行是函数声明。managed功能将从主站下载文件并将其安装在指定的位置。

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

第10行是一个Function arg声明,在这个例子中,将源参数传递给managed函数。

Line 11 is a Requisite declaration.

第11行是一个必要的声明。

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.

第12行是一个必要的参考,它指的是一个状态和一个ID。 在本例中,它指的是第1部分中示例中的ID声明。该声明告诉Salt不要在安装Apache之前安装HTML文件。

Next, create the index.html file and save it in the webserver directory:

下一步,创建index.html文件和把它保存在webserver目录:

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

Last, call state.apply again and the minion will fetch and execute the highstate as well as our HTML file from the master using Salt's File Server:

最后,我们再次运行 state.apply,minion将使用Salt的文件服务器从主文件中获取并执行highstate和我们的HTML文件:

salt '*' state.apply

Verify that Apache is now serving your custom HTML.

验证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.

有两个Requisite声明,“require”和“watch”。 并非每个state都支持“watch”。 服务状态确实支持“watch”,并将根据监视条件重新启动服务。

For example, if you use Salt to install an Apache virtual host configuration file and want to restart Apache whenever that file is changed you could modify our Apache example from earlier as follows:

例如,如果您使用Salt来安装Apache虚拟主机配置文件并希望在文件更改时重新启动Apache,则可以按照以下方式修改我们的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.

如果您的操作系统或发行版中的pkg和服务名称不同,则可以使用第3部分中介绍的名称声明分别指定每个名称和服务名称。

引申:

match : 配模某个模块,比如 match: grain match: nodegroup
require: 依赖某个state,在运行此state前,先运行依赖的state,依赖可以有多个
watch : 在某个state变化时运行此模块,watch除具备require功能外,还增了关注状态的功能。
order : 优先级比require和watch低,有order指定的state比没有order指定的优先级高,假如一个state模块内安装多个服务,或者其他依赖关系,可以使用

【SaltStack官方版】—— states教程, part 2 - 更复杂的states和必要的事物的更多相关文章

  1. 【SaltStack官方版】—— states教程, part 4 - states 说明

    STATES TUTORIAL, PART 4 本教程建立在第1部分.第2部分.第3部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多 sls 文件的扩展模板和配置技巧. This part o ...

  2. 【SaltStack官方版】—— states教程, part 3 - 定义,包括,延伸

    STATES TUTORIAL, PART 3 - TEMPLATING, INCLUDES, EXTENDS 本教程建立在第1部分和第2部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多  s ...

  3. 【SaltStack官方版】—— job management

    JOB MANAGEMENT New in version 0.9.7. Since Salt executes jobs running on many systems, Salt needs to ...

  4. 【SaltStack官方版】—— MANAGING THE JOB CACHE

    MANAGING THE JOB CACHE The Salt Master maintains a job cache of all job executions which can be quer ...

  5. 【SaltStack官方版】—— returners——返回器

    ETURNERS 返回器 By default the return values of the commands sent to the Salt minions are returned to t ...

  6. 【SaltStack官方版】—— STORING JOB RESULTS IN AN EXTERNAL SYSTEM

    STORING JOB RESULTS IN AN EXTERNAL SYSTEM After a job executes, job results are returned to the Salt ...

  7. 【SaltStack官方版】—— Events&Reactor系统—BEACONS

    Events&Reactor系统—BEACONS Beacons let you use the Salt event system to monitor non-Salt processes ...

  8. 【SaltStack官方版】—— Events&Reactor系统—EVENT SYSTEM

    Events&Reactor系统 EVENT SYSTEM The Salt Event System is used to fire off events enabling third pa ...

  9. 【SaltStack官方版】—— EVENTS & REACTOR指南

    EVENTS & REACTOR Event System Event Bus Event types Salt Master Events Authentication events Sta ...

随机推荐

  1. 分布式任务队列 Celery —— 应用基础

    目录 目录 前文列表 前言 Celery 的周期定时任务 Celery 的同步调用 Celery 结果储存 Celery 的监控 Celery 的调试 前文列表 分布式任务队列 Celery 分布式任 ...

  2. Git配置用户名、邮箱

    当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址. 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改. 否则,用户名会显示为unkno ...

  3. 链路聚合teaming(网卡绑定技术)2

    一.sentos7网卡绑定技术之teaming 这里介绍两种最常见的双网卡绑定模式: (1) roundrobin - 轮询模式 所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力. ...

  4. C语言第九周编程作业

        这个作业属于哪个课程 C语言程序设计 这个作业的要求在哪里 https://pintia.cn/problem-sets/1120145772099268608 我在这个课程的目标是 了解结构 ...

  5. MySQL-快速入门(15)MySQL Replication,主从复制

    1.何为主从复制. 从一个MySQL主服务器(master)将数据复制到另一台或多台MySQL从服务器(slaves)的过程,将主数据库的DDL和DML操作通过二进制日志传到复制服务器上,然后在从服务 ...

  6. ThinkPHP关联模型详解

    在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...

  7. 小白学Python——用 百度翻译API 实现 翻译功能

    本人英语不好,很多词组不认识,只能借助工具:百度翻译和谷歌翻译都不错,近期自学Python,就想能否自己设计一个百度翻译软件呢? 百度翻译开放平台: http://api.fanyi.baidu.co ...

  8. 似乎在梦中见过的样子 (KMP)

    # 10047. 「一本通 2.2 练习 3」似乎在梦中见过的样子 [题目描述] 「Madoka,不要相信 QB!」伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Mo ...

  9. 如何将DataTable转换成List<T>

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. SpringAOP用到了什么代理,以及动态代理与静态代理的区别

    spring aop (面向切面)常用于数据库事务中,使用了2种代理. jdk动态代理:对实现了接口的类生成代理对象.要使用jdk动态代理,要求类必须要实现接口. cglib代理:对类生成代理对象. ...