企业自动化运维工具Saltstack-(4)Saltstack中jinjia模版的使用

一、Jinja模板简介

Jinja是一种基于python的模板引擎,在SLS文件里可以直接使用jinja模板来做一些操作。

通过jinja模板可以为不同服务器定义各自的变量。

两种分隔符: {% ... %} 和 {{ ... }},前者用于执行诸如 for 循环 或赋值的语句,后者把表达式的结果打印到模板上(引用)。

二、Jinja模板使用方式

使用控制结构包装条件

Jinja最基本的用法是使用控制结构包装条件:

[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'] == 'server3' %}
    - text: server3
    {% endif %}

以上文件的内容表示在server2和server3创建文件/mnt/testfile,并将不同的内容添加进入文件。

推送:

[root@server1 salt]# salt '*' state.sls test

在server2查看:

[root@server2 ~]# cd /mnt/
[root@server2 mnt]# cat testfile 
server2

在server3查看:

[root@server3 ~]# cd /mnt/
[root@server3 mnt]# cat testfile 
server3

在普通文件的使用

Jinja在普通文件的使用:

[root@server1 salt]# cd apache/
[root@server1 apache]# vim init.sls 
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: 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.63.3
      port: 80

其中:

- template: jinja		#启用jinja模板
- context:
  bind: 172.25.63.3		#定义变量
  port: 80

表示启用jinja模板和定义变量,接下来在配置文件中调用变量:

[root@server1 apache]# vim httpd.conf 
[root@server1 apache]# cat -n httpd.conf | grep 42
    42	Listen {{ bind }}:{{ port }}

推送(由于ip已经设置,故推送给server3):

[root@server1 apache]# salt server3 state.sls apache

 

在这里插入图片描述推送成功,在server3查看:

[root@server3 ~]# netstat -antlpe | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          34667      3047/nginx: master  

查看配置文件:
[root@server3 ~]# cat -n /etc/httpd/conf/httpd.conf | grep 42
    42	Listen 172.25.63.3:80

从配置文件可以看出变量成功调用。

但是这种方式在主机数量增多后就不太合适,我们可以结合grains和pillar来进行调用:

[root@server1 apache]# vim init.sls
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: true
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://apache/httpd.conf
    - template: jinja
    - context:
      bind: {{ grains['ipv4'] }}
      port: 80

但是这种方式调用时,grains['ipv4']是个列表:

[root@server1 apache]# salt server3 grains.item ipv4
server3:
    ----------
    ipv4:
        - 127.0.0.1
        - 172.25.63.3

在推送时就会有问题:

[root@server1 apache]# salt server3 state.sls apache

在这里插入图片描述可以看出apache并不支持列表的形式,因此我们可以使用切片(与python的切片方法相同):

[root@server1 apache]# vim init.sls
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: true
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://apache/httpd.conf
    - template: jinja
    - context:
      bind: {{ grains['ipv4'][-1] }}
      port: 80

再次推送:

[root@server1 apache]# salt server3 state.sls apache

在这里插入图片描述可以看出推送成功。

我们也可以将port变量调用pillar的值:

[root@server1 apache]# vim init.sls 
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: true
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://apache/httpd.conf
    - template: jinja
    - context:
      bind: {{ grains['ipv4'][-1] }}
      port: {{ pillar['port'] }}

推送:

[root@server1 apache]# salt server3 state.sls apache

在这里插入图片描述成功推送。

此时更改pillar的信息:

[root@server1 pillar]# vim web.sls 
[root@server1 pillar]# cat web.sls 
{% if grains['fqdn'] == 'server3' %}
webserver: httpd
port: 8080
{% elif grains['fqdn'] == 'server2' %}
webserver: nginx
port: 80
{% endif %}

将server3的端口改为8080,推送:

[root@server1 apache]# salt server3 state.sls apache

在server3查看端口:

[root@server3 ~]# netstat -antlpe | grep 8080
tcp        0      0 172.25.63.3:8080        0.0.0.0:*               LISTEN      0          45425      3749/httpd  

可以看出已经更改为8080.

当然也可以直接在配置文件中调用grains或者pillar:

[root@server1 apache]# vim init.sls 
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: true
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://apache/httpd.conf
    - template: jinja				#注意需要启用jinja模板
[root@server1 apache]# vim httpd.conf 
[root@server1 apache]# cat -n httpd.conf | grep 42
    42	Listen {{ grains['ipv4'][-1] }}:{{ pillar['port'] }}

然后推送:

[root@server1 apache]# salt server3 state.sls apache

在这里插入图片描述
可以看出推送成功。

import方式

第三种方式为import方式,可在state文件之间共享:

定义变量文件:

[root@server1 salt]# vim lib.sls
[root@server1 salt]# cat lib.sls 
{% set port = 80 %}

导入模板文件:

[root@server1 salt]# cd apache/
[root@server1 apache]# vim httpd.conf 
[root@server1 apache]# head -1 httpd.conf 
{% from 'lib.sls' import port %}

调用变量:

 [root@server1 apache]# cat -n httpd.conf | grep 43
    43	Listen {{ grains['ipv4'][-1] }}:{{ port }}

推送

[root@server1 apache]# salt server3 state.sls apache

 

在server3查看可以看出配置已经生效:

[root@server3 ~]# netstat -antlpe | grep httpd
tcp        0      0 172.25.63.3:80          0.0.0.0:*               LISTEN      0          45983      3749/httpd    

但是我们先在考虑一个问题,如果我们不仅在init文件中定义了变量port,还用import方式定义了变量,当调用的时候哪个优先级高呢,通过实验看下结果:

在init文件中定义变量port为8080(这里调用的pillar,server3的pillar port为8080):

[root@server1 apache]# vim init.sls 
[root@server1 apache]# cat init.sls 
apache:
  pkg.installed:
    - pkgs:
      - {{ pillar['webserver'] }}
  service.running:
    - name: httpd
    - reload: true
    - enable: true
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://apache/httpd.conf
    - template: jinja
    - context:
#      bind: {{ grains['ipv4'][-1] }}
      port: {{ pillar['port'] }}

import的方式我们上面已经设置过了,因此我们直接推送查看结果:

[root@server1 apache]# salt server3 state.sls apache

server3查看:

[root@server3 ~]# netstat -antlpe | grep httpd
tcp        0      0 172.25.63.3:80          0.0.0.0:*               LISTEN      0          45983      3749/httpd         

可以看出通过import的方式导入的变量优先级高于在init文件中定义的变量,原因是在运行的时候最后读取的是import方式导入的变量。

注意:一般习惯将变量都定义在init文件中,然后在其他文件中引用即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值