Saltstack自动化(九)Salt-Formulas的使用

Saltstack自0.17.x版本开始引进Formulas的概念,旨在通过简化State和集成数据来实现State的友好管理。根据<a href="https://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html#usage" target="_blank" rel="noopener">SALT FORMULAS的官方文档</a>,在完成手动添加formula目录后,formula应该提供一些默认的配置而立即可用。如果需要进一步的配置,大部分的formulas可以通过Pillar数据进行配置,可以参考每个Formula仓库的pillar.example文件查询可用的配置。

一、未集成的State

先来看下编写State的一般样例:



<br />
apache:
  pkg:
    - installed
    {% if grains['os_family'] == 'RedHat' %}
    - name: httpd
    {% elif grains['os_family'] == 'Debian' %}
    - name: apache2
    {% endif %}
乍看一下觉得这样写很正常,可是仔细想想吧,这样一来,apache的应用属性的判断逻辑写死在sls文件里,一旦需要更多的判断逻辑时这样的写法便稍显笨拙,那么,有什么好办法呢?

二、引进 Formulas

其实简单来说的话,Formulas即是建议使用Salt的用户编写State sls时统一使用map.jinjia来完成处理逻辑和判断,从而生成类似pillar的变量数据。下面,同样以apache为例讲述Salt Formulas编写逻辑。Formulas结构如下:



<br />
.
├── /srv/salt/apache/conf.sls
├── /srv/salt/apache/init.sls
└── /srv/salt/apache/map.jinja
Formulas的核心便在于map.jinja的编写,如上Apache对应的map.jinja样例内容如下:



<br />
{% set apache = salt[‘grains.filter_by’]({
‘Debian’: {
‘server’: ‘apache2’,
‘service’: ‘apache2’,
‘conf’: ‘/etc/apache2/apache.conf’,
},
‘RedHat’: {
‘server’: ‘httpd’,
‘service’: ‘httpd’,
‘conf’: ‘/etc/httpd/httpd.conf’,
},
}, merge=salt[‘pillar.get’](‘apache:lookup’)) %}
这样一来,apache对应的主sls(init.sls)内容则可以改写为如下:



<br />
{% from “apache/map.jinja” import apache with context %}
apache:
pkg:
  - installed
  - name: {{ apache.server }}
service:
  - running
  - name: {{ apache.service }}
  - enable: True
少了复杂的判断逻辑,多了变量数据的简单使用!

三、结合Pillar的Formulas

Salt Formulas 还可以通过结合Pillar实现更多的扩展。同样以上述Apache的配置为例,需要编写conf.sls来完成apache的配置,结合Formulas Map 概念之后,它可能变成如下内容:



<img src="https://www.361way.com/wp-content/uploads/2017/02/salt-formulas-pillar.png" width="624" height="343" title="salt-formulas-pillar" alt="salt-formulas-pillar" />



注意上述配置里,除了导入map.jinja预定义内容外,还有一行source配置使用了奇怪的salt["pillar.get"]["apache:lookup:config:tmpl"],熟悉pillar的应该不陌生,这便是结合了pillar来完成state的定义。



事先在/srv/pillar/top.sls里包含apache.sls,而后在这个pillar定义文件里添加对应内容来重写映射关系:



<br />
apache:
  lookup:
    config:
      tmpl: salt://apache/files/redhat/
    server: my_custom_apache
这样一来,便可以基于Formulas map基础上做更多的特色化配置。

四、Salt Formulas 制作

如你所见,Formulas其实应该算是State的集成和封装,事实上,Github也已经有很多的*-Formulas state出现,一个标准的*-Formulas 拥有以下结构:



<br />
foo-formula
|-- foo/
|   |-- map.jinja
|   |-- init.sls
|   `-- bar.sls
|-- CHANGELOG.rst
|-- LICENSE
|-- pillar.example
|-- README.rst
`-- VERSION
具体可参照Github上已有的Formulas样例,这里不再赘述。



<strong>参考内容:</strong>



<a href="http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html" target="_blank" rel="noopener">salt官网</a>



<a href="http://pan.baidu.com/s/1mgI9xiC" target="_blank" rel="noopener">Forrest Alvarez Demo</a>



本篇根据<a href="http://devopstarter.info/saltstack-xue-xi-zhi-salt-formulas/" target="_blank" rel="noopener"> Colstuwjx的博客 salt相关篇</a>整理。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注