如何使用Salt State

本文介绍如何使用SaltStack的状态管理(SaltState)来简化配置管理任务。通过几个示例展示了如何利用SLS文件来实现软件包安装、服务管理、文件配置、用户与组创建等操作。

转自:http://wiki.saltstack.cn/topics/tutorials/starting_states

* 原文: http://docs.saltstack.com/topics/tutorials/starting_states.html * 译者: pengyao

如何使用Salt State

简单,明了,简单明了

需要强大(powerful)、有用(useful,译者注,好像翻译得很别扭)的工程解决方案建立在简单的原则上。 Salt的SLS系统目标是K.I.S.S(译者补充:Keep it simple, stupid)

Salt State系统的核心是SLS,全称为SaLState file. SLS用来描述系统应该怎么做, 其数据简单明了. 这一切常常称为配置管理(configuration management)

It's all just data

在详细研究之前,本节将带你了解SLS只是数据结构(data structure). While understanding that the SLS is just a data structure is not at all critical to understand to make use Salt States, it should help bolster the understanding of where the real power is.(译者注:此次解决是否需要翻译)

在现实中,数据只有字典(dictionaries)、列表(lists)、字符串(strings)及数字(numbers), SLS也是如此. 掌握了这一阀门,Salt将变得更灵活. 人们写更多的state文件,写的内容更清晰严谨. 尽管管理员或开发者需求增大,系统却变得更简单明了.(译者注:翻译的好像不准确,附上原文:The result is a system that is easy to understand, yet grows with the needs of the admin or developer.)

默认数据格式 - YAML

Slat默认使用的SLS序列化数据格式是 YAML

下边将给出一个常见的SLS文件例子:

切换行号显示
   1  apache:
   2    pkg:
   3      - installed
   4    service:
   5      - running
   6      - require:
   7        - pkg: apache

此SLS数据确保叫做"apache"的软件包(package)已经安装,并且"apache"服务(service)正在运行中. 内容简单明了.

数据的第一列,被成为ID说明(ID Declaration). ID说明表明可以操控的名字.

第二列和第四列是State说明(State Declaration).它们分别使用了pkgservice states. pkg state通过系统的包管理其管理关键包, service state管理系统服务(daemon). 在pkgservice列下边是运行的方法. 方法定义包和服务应该怎么做。此处是软件包应该被安装,服务应该处于运行中.

第六行使用require. 本方法称为"必须指令"(Requisite Statement),表明只有当apache软件包安装成功时,apache服务才启动起来.

添加配置和用户

当部署apache web服务时,可能需要添加若干组件,如管理其配置文件,建立其运行的用户(user)及组(group).

切换行号显示
   1  apache:
   2    pkg:
   3      - installed
   4    service:
   5      - running
   6      - watch:
   7        - pkg: apache
   8        - file: /etc/httpd/conf/httpd.conf
   9        - user: apache
  10    user.present:
  11      - uid: 87
  12      - gid: 87
  13      - home: /var/www/html
  14      - shell: /bin/nologin
  15      - require:
  16        - group: apache
  17    group.present:
  18      - gid: 87
  19      - require:
  20        - pkg: apache
  21 
  22  /etc/httpd/conf/httpd.conf:
  23    file.managed:
  24      - source: salt://apache/httpd.conf
  25      - user: root
  26      - group: root
  27      - mode: 644

本例SLS数据扩展了之前的例子,增加了一个配置文件,用户和组,并新增了一个必须指令watch.

添加更多的states非常容易,在Apache ID下增加用户和组states即可. 用户和组将是Apache的用户和组(译者注: 原文是the user and group will be the Apache user and group). require指令确保只有当Apache软件包安装之后,才会建立组,然后再建立用户.

接下来的在service下的require变为了watch,并且由原来的一条state变为3条. watch指令和require相同,确保在运行本state时先运行其他的states,但增加了扩展功能. 如果其watch的states发生变更,watch指令将运行state的watcher方法. 因此当软件包更新,配置文件变更,用户uid变更时,f服务的wather方法将运行. 服务的watcher方法是重启本服务,因此本例中配置文件的变更也会触发服务重启操作.

Moving Beyond a single sls

在建立Salt States时,往往需要使用多个SLS文件. 之前的例子仅仅使用了一个SLS文件,但多个SLS文件组合将建立一个State树(State Tree). 之前的例子中也引用了salt://apache/httpd.conf源. 请确保该文件存在有效.

SLS文件位于Salt master上的一个目录下,文件只是文件,A SLS is just a file and files to download are just files.(译者注:绕口令,绕了半天不知道该怎么翻译)

之前的Apache例子的Salt文件服务看起来像是:

apache/init.sls
apache/httpd.conf

httpd.conf只是apche目录下的一个文件,被直接引用.

但是当有多个SLS文件时,将会添加更多内容,如下边的SSH例子:

ssh/init.sls:

切换行号显示
   1  openssh-client:
   2    pkg.installed
   3 
   4  /etc/ssh/ssh_config:
   5    file.managed:
   6      - user: root
   7      - group: root
   8      - mode: 644
   9      - source: salt://ssh/ssh_config
  10      - require:
  11        - pkg: openssh-client

ssh/server.sls:

切换行号显示
   1  include:
   2    - ssh
   3 
   4  openssh-server:
   5    pkg.installed
   6 
   7  sshd:
   8    service.running:
   9      - require:
  10        - pkg: openssh-client
  11        - pkg: openssh-server
  12        - file: /etc/ssh/banner
  13        - file: /etc/ssh/sshd_config
  14 
  15  /etc/ssh/sshd_config:
  16    file.managed:
  17      - user: root
  18      - group: root
  19      - mode: 644
  20      - source: salt://ssh/sshd_config
  21      - require:
  22        - pkg: openssh-server
  23 
  24  /etc/ssh/banner:
  25    file:
  26      - managed
  27      - user: root
  28      - group: root
  29      - mode: 644
  30      - source: salt://ssh/banner
  31      - require:
  32        - pkg: openssh-server

Note
你可能注意到Salt file managed有两种不同的写法。在/etc/ssh/sshd_config state中.我们使用了file managed state说明,而在/etc/banner state中,我们使用了file state声明,然后在state声明下添加了managed属性. 这两种方法结果一样, file.managed是简写形式.

现在我们的State树(State Tree)看起来像是:

apache/init.sls
apache/httpd.conf
ssh/init.sls
ssh/server.sls
ssh/banner
ssh/ssh_config
ssh/sshd_config

本例中使用了include指令,当某些在其他SLS中的部件需要在本SLS中required,watch等时,使用include指令include进来进行扩展

include指令允许跨链接,即一个SLS include其他sls时,本SLS也可以被include其他SLS中去(译者注:继续绕口令,原文是When an SLS has an include statement it is literally extended to include the contents of the included SLS files.)

扩展include SLS数据

有时SLS数据需要进行扩展. 比如apache需要watch附加资源,或在某些环境下需要放置其他文件.

接下来的例子将增加apache watchers和更改ssh banner.

ssh/custom-server.sls:

切换行号显示
   1  include:
   2    - ssh.server
   3 
   4  extend:
   5    /etc/ssh/banner:
   6      file:
   7        - source: salt://ssh/custom-banner

python/mod_python.sls:

切换行号显示
   1  include:
   2    - apache
   3 
   4  extend:
   5    apache:
   6      service:
   7        - watch:
   8          - pkg: mod_python
   9 
  10  mod_python:
  11    pkg.installed

custom-server.sls文件用于使用了extend指令,修改了需要下载的banner文件,用于自定义banner.

mod_python SLS新增了mod_python软件包,重要的是扩展了extended服务,增加了watch mod_python软件包.

在extend中的require或watch

extend中含有requirewatch时,它表示增加,而不是替换掉必须部分(requisite component).


未完,待续补充jinja2内容


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值