STATES TUTORIAL, PART 3 - TEMPLATING, INCLUDES, EXTENDS

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

templating sls modules

SLS modules may require programming logic or inline execution. This is accomplished with module templating. The default module templating system used is Jinja2 and may be configured by changing the renderer value in the master config.

SLS模板块可能需要编程的逻辑或则嵌套的执行。这是通过模块的模板,默认的模块模板系统使用的是`Jinja2`, 我们可以通过更改主配置的:conf_master:`renderer`值来改变这个。

All states are passed through a templating system when they are initially read. To make use of the templating system, simply add some templating markup. An example of an sls module with templating markup may look like this:

当初始化时所有的状态类型都是通过模板系统获得。使用模板系统只需要简单的添加一些模板标记。SLS模板与模板标记的一个例子,看起来像下面这样。

{% for usr in ['moe','larry','curly'] %}
{{ usr }}:
user.present
{% endfor %}

This templated sls file once generated will look like this:

这个SLS模板文件一次性构造了如下的内容:

moe:
user.present
larry:
user.present
curly:
user.present

Here's a more complex example:

这里有个更复杂点的例子:

# Comments in yaml start with a hash symbol.
# Since jinja rendering occurs before yaml parsing, if you want to include jinja
# in the comments you may need to escape them using 'jinja' comments to prevent
# jinja from trying to render something which is not well-defined jinja.
# e.g.
# {# iterate over the Three Stooges using a {% for %}..{% endfor %} loop
# with the iterator variable {{ usr }} becoming the state ID. #}
{% for usr in 'moe','larry','curly' %}
{{ usr }}:
group:
- present
user:
- present
- gid_from_name: True
- require:
- group: {{ usr }}
{% endfor %}

using grains in sls modules

Often times a state will need to behave differently on different systems. Salt grains objects are made available in the template context. The grains can be used from within sls modules:

很多时候,一个state需要在不同的系统上表现不同。Salt grains 对象能在模版上下文中可用。

apache:
pkg.installed:
{% if grains['os'] == 'RedHat' %}
- name: httpd
{% elif grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}

using environment variables in sls modules

You can use salt['environ.get']('VARNAME') to use an environment variable in a Salt state.

您可以使用salt ['environ.get']('VARNAME')在Salt状态下使用环境变量。

MYENVVAR="world" salt-call state.template test.sls
Create a file with contents from an environment variable:
file.managed:
- name: /tmp/hello
- contents: {{ salt['environ.get']('MYENVVAR') }}

Error checking:

{% set myenvvar = salt['environ.get']('MYENVVAR') %}
{
% if myenvvar %} Create a file with contents from an environment variable:
file.managed:
- name: /tmp/hello
- contents: {{ salt['environ.get']('MYENVVAR') }} {% else %} Fail - no environment passed in:
test.fail_without_changes {% endif %}

calling salt modules from templates

All of the Salt modules loaded by the minion are available within the templating system. This allows data to be gathered in real time on the target system. It also allows for shell commands to be run easily from within the sls modules.

所有由minion加载的Salt模块都可在模板系统中使用。 这允许在目标系统上实时收集数据。 它还允许从sls模块中轻松运行shell命令。

The Salt module functions are also made available in the template context as salt:

Salt模块函数也可以在模板上下文中作为salt使用:

The following example illustrates calling the group_to_gid function in the file execution module with a single positional argument called some_group_that_exists.

以下示例说明使用称为some_group_that_exists的单个位置参数在文件执行模块中调用group_to_gid函数。

moe:
user.present:
- gid: {{ salt['file.group_to_gid']('some_group_that_exists') }}

One way to think about this might be that the gid key is being assigned a value equivelent to the following python pseudo-code:

思考这个问题的一种方法可能是,把gid key当作与下面的python伪代码等效的值:

import salt.modules.file
file.group_to_gid('some_group_that_exists')

Note that for the above example to work, some_group_that_exists must exist before the state file is processed by the templating engine.

请注意,要使上述示例正常工作,在模板引擎处理状态文件之前,必须存在some_group_that_exists。

Below is an example that uses the network.hw_addr function to retrieve the MAC address for eth0:

以下是使用network.hw_addr函数检索eth0的MAC地址的示例:

salt['network.hw_addr']('eth0')

To examine the possible arguments to each execution module function, one can examine the module reference documentation </ref/modules/all>:

检查每个执行模块函数的可能参数,大家可以检查 module reference documentation </ref/modules/all>:

advanced sls module syntax (更高级的SLS模块语法)

Lastly, we will cover some incredibly useful techniques for more complex State trees.

在最后,我们将介绍更复杂的状态树的一些非常有用的技术。

include declaration(包括声明)

A previous example showed how to spread a Salt tree across several files. Similarly, Requisites and Other Global State Arguments span multiple files by using an Include declaration. For example:

前一个示例展示了如何在多个文件中传播Salt 树。 同样,通过使用Include声明,requisites和其他全局状态参数跨越多个文件。 例如:

python/python-libs.sls:

python-dateutil:
pkg.installed

python/django.sls:

include:
- python.python-libs django:
pkg.installed:
- require:
- pkg: python-dateutil

 extend declaration

You can modify previous declarations by using an Extend declaration. For example the following modifies the Apache tree to also restart Apache when the vhosts file is changed:

您可以使用扩展声明修改以前的声明。 例如,以下内容修改了Apache树,以便在vhosts文件更改时重新启动Apache:

apache/apache.sls:

apache:
pkg.installed

apache/mywebsite.sls:

include:
- apache.apache extend:
apache:
service:
- running
- watch:
- file: /etc/httpd/extra/httpd-vhosts.conf /etc/httpd/extra/httpd-vhosts.conf:
file.managed:
- source: salt://apache/httpd-vhosts.conf

Using extend with require or watch

The extend statement works differently for require or watch. It appends to, rather than replacing the requisite component.
扩展语句对于require或watch有不同的作用。它附加在,而不是取代必要的组件。

name declaration(命名声明)

You can override the ID declaration by using a Name declaration. For example, the previous example is a bit more maintainable if rewritten as follows:

您可以使用名称声明覆盖ID声明。 例如,如果按照以下方式进行重写,前面的示例可以更容易维护:

include:
- apache.apache extend:
apache:
service:
- running
- watch:
- file: mywebsite mywebsite:
file.managed:
- name: /etc/httpd/extra/httpd-vhosts.conf
- source: salt://apache/httpd-vhosts.conf

names declaration

Even more powerful is using a Names declaration to override the ID declaration for multiple states at once. This often can remove the need for looping in a template. For example, the first example in this tutorial can be rewritten without the loop:

更强大的是使用Names声明来一次覆盖多个状态的ID声明。 这通常可以消除在模板中循环的需要。 例如,本教程中的第一个示例可以在没有循环的情况下重写:

stooges:
user.present:
- names:
- moe
- larry
- curly

In part 4 we will discuss how to use salt's file_roots to set up a workflow in which states can be "promoted" from dev, to QA, to production.

在第4部分中,我们将讨论如何使用salt的file_roots来建立一个工作流程,在这个工作流程中,可以将状态从开发,质量保证,到生产“推广”。

【SaltStack官方版】—— states教程, part 3 - 定义,包括,延伸的更多相关文章

  1. 【SaltStack官方版】—— states教程, part 2 - 更复杂的states和必要的事物

    states tutorial, part 2 - more complex states, requisites 本教程建立在第1部分涵盖的主题上.建议您从此处开始. 在Salt States教程的 ...

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

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

  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官方版】—— returners——返回器

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

  5. 【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 ...

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

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

  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. C#通用类库

    http://www.cnblogs.com/feiyangqingyun/archive/2010/12/20/1911630.html

  2. BigDecimal进行加减乘除计算

    以前大部分关于查询计算的逻辑是在sql语句中执行的,但是有时候会出现比较复杂的计算情况,需要我们在代码中进行计算,这个时候使用BigDecimal进行计算会很方便. BigDecimal num1 = ...

  3. Eclipse."Courier New"字体

    1.Win 7 下eclipse添加Courier New字体 - 彦帅的博客.html(https://blog.csdn.net/theblackbeard/article/details/525 ...

  4. PHP_OS的常见值

    PHP_OS是PHP中的一个预定义常量,表示当前操作系统.那么PHP_OS有哪些值可用呢??PHP_OS的值一般可以为:CYGWIN_NT-5.1,Darwin,FreeBSD,HP-UX,IRIX6 ...

  5. 嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法

    嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法 点我进入OJ题目详情 题意 给你一串数,让你求长度最长的子串,这个字串满足里面没有重复出现的数字. 解题思路 使用一个标记数组,来标记每个数的第一 ...

  6. ubuntu 配置 tftp 服务器

    一. 安装 tftp 1.1. 安装 tftp 所需的软件. a. 安装 tftp-hpa,tftpd-hpa,前者是客户端,后者是服务程序, 在终端下输入 sudo apt-get install ...

  7. Codeforces 1262D Optimal Subsequences(BIT+二分)

    首先比较容易想到肯定是前k大的元素,那么我们可以先对其进行sort,如果数值一样返回下标小的(见题意),接下里处理的时候我们发现需要将一个元素下标插入到有序序列并且需要访问第几个元素是什么,那么我们可 ...

  8. linux下定时器的实现

    简介: linux下经常有这样的需求,需要定时轮询执行某种任务,当然,用shell脚本的话,crontab和at就可以满足要求.如果从C语言的角度来看,实现定时器也是一个比较简单的任务,因为具有普遍性 ...

  9. 断开ssh链接在后台继续运行命令

    转载:http://blog.csdn.net/v1v1wang/article/details/6855552 对于linux运维,我们都是使用ssh登录到服务器,如果我们运行的任务需要很长时间或不 ...

  10. 【源码解读】cycleGAN(一):网络

    源码地址:https://github.com/aitorzip/PyTorch-CycleGAN 如图所示,cycleGAN的网络结构包括两个生成器G(X->Y)和F(Y->X),两个判 ...