一、Jinja模板简介
Jinja是一种基于python的模板引䔷,在SLS文件里可以直接使用jinja模板来做一些操作。
通过jinja模板可以为不同服务器定义各自的变量。
两种分隔符:{%…%}{{…}},前者用于执行诸如for循环或赋值语句,后者把表达式的结果打印到模板上。
1、基本用法:使用控制结构包装条件
[root@server1 salt]# vim test.sls
[root@server1 salt]# cat test.sls
/mnt/testfile:
file.append:
{% if grains['fqdn'] == 'server2' %}
- text: server2
{% elif grains['fqdn'] == 'server4' %}
- text: server4
{% endif %}
[root@server1 salt]# salt '*' state.sls test
#查看
[root@server2 mnt]# ls
nfs testfile
[root@server2 mnt]# cat testfile
server2
[root@server4 mnt]# cat testfile
server4
2、Jinja在普通文件中的使用-httpd
(1)监听固定端口
[root@server1 apache]# vim init.sls
[root@server1 apache]# cat init.sls
apache:
pkg.installed:
- pkgs:
- httpd
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/httpd.conf
service.running:
- name: httpd
- reload: true
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://apache/httpd.conf
- template: jinja
- context:
bind: 172.25.1.2
port: 80
#引用
[root@server1 apache]# vim httpd.conf
Listen {{ bind }}:{{ port }}
#调用
[root@server1 apache]# salt server2 state.sls apache
#测试
[root@server1 apache]# curl 172.25.1.2:80
(2)引用变量监听多个主机多个端口
1)、
- template: jinja
- context:
bind: {{ grains['ipv4'] }} #提取到ip
port: 80
[root@server1 apache]# salt server2 grains.item ipv4 #ip为列表值,apache不支持
server2:
----------
ipv4:
- 127.0.0.1
- 172.17.0.1
- 172.18.0.1
- 172.25.1.2
2)、使用grains,还可以使用pillar即改即用
- template: jinja
- context:
bind: {{ grains['ipv4'][-1] }}
port: 80
#使用Jinja模板可以直接使用grains和pillar。
Listen {{ grains['ipv4'][-1] }}:{{ pillar[port] }}
3)、import方式,可在state文件之间共享
[root@server1 salt]# vim lib.sls
[root@server1 salt]# cat lib.sls
{% set port = 80 %}
{% from 'lib.sls' import port %}
Listen {{ bind }}:{{ port }} #引用,可以对比优先级
#import优先级高于pillar
3、keepalived例子
(1)配置主master:
[root@server1 keepalived]# vim init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
(2)主备一起配置
#由于keepalived有主备之分,需要在配置文件中设置变量区分
[root@server1 keepalived]# vim keepalived.conf
1)#sls文件中著名,可以用srv/pillar
[root@server1 pillar]# vim web.sls
[root@server1 pillar]# cat web.sls
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
port: 8080
kpstate: MASTER
kppri: 100 #优先级
{% elif grains['fqdn'] == 'server4' %}
webserver: nginx
port: 80
kpstate: BACKUP
kppri: 50
{% endif %}
vip 172.25.1.100
[root@server1 keepalived]# vim init.sls
[root@server1 keepalived]# cat init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['kpstate'] }}
PRI: {{ pillar['kppri'] }}
VIP: pillar['vip']
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
#应用
[root@server1 keepalived]# salt '*' state.sls keepalived
2)可以不用pillar,直接自己在sls文件中定义
[root@server1 keepalived]# cat init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
- template: jinja
- context:
{% if grains['fqdn'] == 'server2' %}
STATE: MASTER
PRI: 100
VIP: 172.25.1.100
{% elif grains['fqdn'] == 'server4' %}
STATE: BACKUP
PRI: 50
VIP: 172.25.1.100
{% endif %}
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
[root@server1 keepalived]# salt '*' state.sls keepalived