saltstack中使用Jinja
jinja简介
Jinja是基于python的模板引擎,在saltstack中我们使用yaml_jinja渲染器来根据模板生产对应的配置文件,对于不同的操作系统或者不同的情况通过jinja可以让配置文件或者操作形成一种模板的编写方式。
一般在diango或者saltstack这种python环境下使用较多。
在saltstack中使用jinja
使用的步骤比较简单:
1.File状态使用template参数 - template: jiaja
2.模板文件里面使用{{名称}}
3.File状态模块要指定变量列表:
- default:
PORT: 8080
以zabbix_agentd客户端的配置文件为例:
/etc/zabbix_agentd.conf:
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix/files/zabbix_agentd.conf
- templete: jinja
- Server: {{pillar['zabbix-agent']['Zabbix_Server']}}
一个简单示例
我们以用saltstack安装apache服务为例,在centos和ubuntu中apache服务的安装包名称是不同的,如果在一个apache.sls文件中进行安装设置,则需要借助jinja和pillar对安装包的名称进行逻辑判断。
由grains.item可以获得salt客户端的相关信息,本例中需要获取os版本:
[root@server2 pillar]# salt '*' grains.items
显示部分结果如下:
os:
CentOS
os_family:
RedHat
所以我们要对grains的os值进行判断,不同的系统我们对apache的值进行不同的设定,这样apache就相当于是可以随机应变的值。
{% if grains['os'] == 'CentOS' %}
apache: httpd
git: git
{% elif grains['os'] == 'Debian' %}
apache: apache2
git: git-core
{% endif %}
在安装的步骤apache.sls中直接使用如下设定:
[root@server2 init]# cat apache.sls
apache:
pkg.installed:
- name: {{ pillar['apache']}}
{{pillar[‘apache’]}}就是使用了pillar中的apache的值,我们根据不同的系统会选择不同的安装包名称。通过这种方式可以简化我们配置时的系统差异,只需要考虑关键的逻辑即可。
执行结果如下:
[root@server2 init]# salt '*' state.highstate
minion-192.168.42.130:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf is in the correct state
Started: 15:59:02.850610
Duration: 98.457 ms
Changes:
----------
ID: /etc/bashrc
Function: file.append
Result: True
Comment: File /etc/bashrc is in correct state
Started: 15:59:02.949421
Duration: 6.435 ms
Changes:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: File /etc/profile is in correct state
Started: 15:59:02.956039
Duration: 2.451 ms
Changes:
----------
ID: /etc/yum.repos.d/local_yum.repo
Function: file.managed
Result: True
Comment: File /etc/yum.repos.d/local_yum.repo is in the correct state
Started: 15:59:02.958724
Duration: 74.73 ms
Changes:
----------
ID: apache
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 15:59:03.922463
Duration: 7044.686 ms
Changes:
----------
httpd:
----------
new:
2.4.6-40.el7.centos
old:
Summary for minion-192.168.42.130
------------
Succeeded: 5 (changed=1)
Failed: 0
------------
Total states run: 5
Total run time: 7.227 s
minion-192.168.42.128:
----------
ID: /etc/resolv.conf
Function: file.managed
Result: True
Comment: File /etc/resolv.conf is in the correct state
Started: 15:59:02.895075
Duration: 108.572 ms
Changes:
----------
ID: /etc/bashrc
Function: file.append
Result: True
Comment: File /etc/bashrc is in correct state
Started: 15:59:03.003882
Duration: 8.062 ms
Changes:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: File /etc/profile is in correct state
Started: 15:59:03.012117
Duration: 3.349 ms
Changes:
----------
ID: /etc/yum.repos.d/local_yum.repo
Function: file.managed
Result: True
Comment: File /etc/yum.repos.d/local_yum.repo is in the correct state
Started: 15:59:03.015661
Duration: 66.75 ms
Changes:
----------
ID: apache
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 15:59:03.992430
Duration: 7570.761 ms
Changes:
----------
httpd:
----------
new:
2.4.6-40.el7.centos
old:
Summary for minion-192.168.42.128
------------
Succeeded: 5 (changed=1)
Failed: 0
------------
Total states run: 5
Total run time: 7.757 s