Linux-saltstack-4 jinjia模板得基本使用

@

一、简介

jinja2是一个强大的pyton模板引擎,可以使用代码动态生成内容 创建文件内容。
可以用jinja2 配置grain和pillar扩展sls配置文件
可以用jinja2 配置grain和pillar动态下发文件(服务配置文件、shell脚本等等)

在saltstack中 jinji模板其实就是一个带有salt变量的文件:
这个文件可以是服务的配置文件 也可以是脚本

二、jinja2语法

本文档中salt得版本

[root@salt-master shell_scripts]# salt --version
salt 3002.2
因为可能因为版本问题 在编写Jinja模板时 语法可能会有些许区别

1.jinja2变量

引用变量中的方法{{ 变量名 }},引用了jinja变量的文件后缀一定是j2

1.1 配置文件中使用jinja变量

[root@salt-master ~]# mkdir -p /srv/salt/httpd

#这里准备一个配置文件和一个sls文件
[root@salt-master httpd]# ls
httpd.conf.j2  httpd.sls

#在httpd.conf中引用变量
[root@salt-master httpd]# grep -i "^listen" httpd.conf 
Listen {{ http_port }}

在sls文件中定义jinja变量

#定义jinja变量
[root@salt-master httpd]# vim httpd.sls
copy httpd config file:
  file.managed:
    - source: salt://httpd/httpd.conf.j2
    - name: /etc/httpd/conf/httpd.conf
    - template: jinja
    - defaults:
      http_port: 8081

解释: 
	- template:jinja 说明发送的文件是一个jinja模板
	- defaults: 定义了http_port变量

执行结果:

[root@salt-master httpd]# salt '*' state.sls httpd.httpd

查看客户端内容是否发生变化:
[root@salt-client ~]# grep -i "^listen" /etc/httpd/conf/httpd.conf 
Listen 8081

1.2在脚本中定义jinja变量

我们可以将jinja变量直接定义在脚本中,语法如下:

{% set 变量名 = 值 %} :在脚本中替换后会变成空行
或者
{% set 变量名 = 值 -%}:在脚本中替换后不会变成空行
[root@salt-master ~]# mkdir -p /srv/salt/shell_scripts
[root@salt-master ~]# cd /srv/salt/shell_scripts/
[root@salt-master shell_scripts]# ls
shell.sls  test.sh.j2

[root@salt-master shell_scripts]# cat shell.sls 
send_shell:
  file.managed:
    - source: salt://shell_scripts/test.sh.j2
    - name: /root/test.sh
    - user: root
    - group: root
    - mode: 644
    - template: jinja 
    
[root@salt-master shell_scripts]# cat test.sh.j2 
#!/bin/bash

{% set name = 'zhangsan' %}
echo "my name is {{name}}"

执行

[root@salt-master shell_scripts]# salt '*' state.sls shell_scripts.shell

在客户端查看test.sh

[root@salt-client ~]# cat test.sh 
#!/bin/bash


echo "my name is zhangsan"

1.3在脚本中设置grains变量

在前边文章中讲过grains的用法

[root@salt-master ~]# salt '*' grains.item os
salt-client:
    ----------
    os:
        CentOS
或者
[root@salt-master ~]# salt '*' grains.get os
salt-client:
    CentOS

例子1:单值
[root@salt-master shell_scripts]# cat test.sh.j2 
#!/bin/bash
{% set system_name = grains['os'] -%}
echo "system version = {{system_name}}"

执行结果如下:
[root@salt-master shell_scripts]# salt '*' state.sls shell_scripts.shell

在客户端查看
[root@salt-client ~]# cat test.sh 
#!/bin/bash
echo "system version = CentOS"

例子2:多值

在例子1中 os项的值只有1个,如果有多个值得项应该如何取值,比如

[root@salt-master shell_scripts]# salt '*' grains.get ipv4
salt-client:
    - 127.0.0.1
    - 192.168.1.211
    - 192.168.122.1
[root@salt-master shell_scripts]# cat test.sh.j2 
#!/bin/bash

{% set ip = grains['ipv4'][0] -%}
echo "ip address = {{ ip }}"

[root@salt-client ~]# cat test.sh 
#!/bin/bash

echo "ip address = 127.0.0.1"

或者

[root@salt-master shell_scripts]# cat test.sh.j2 
#!/bin/bash

{% set ip = salt['grains.get']('ipv4')[0] -%}
echo "ip address = {{ ip }}"

例子3:多层取值
[root@salt-master shell_scripts]# salt '*' grains.item ip_interfaces
salt-client:
    ----------
    ip_interfaces:
        ----------
        ens32:
            - 192.168.1.211
            - fe80::2da7:41c7:9e01:82c1
        lo:
            - 127.0.0.1
            - ::1
        virbr0:
            - 192.168.122.1
        virbr0-nic:

(1)在命令中多层取值
[root@salt-master shell_scripts]# salt '*' grains.item ip_interfaces:ens32:0
salt-client:
    ----------
    ip_interfaces:ens32:0:
        192.168.1.211
[root@salt-master shell_scripts]# salt '*' grains.item ip_interfaces:ens32:1
salt-client:
    ----------
    ip_interfaces:ens32:1:
        fe80::2da7:41c7:9e01:82c1

(2)在脚本中多层取值
[root@salt-master shell_scripts]# cat test.sh.j2 
#!/bin/bash

{% set ip = grains['ip_interfaces']['ens32'][0] -%}
echo "ip address = {{ ip }}"

或者
#!/bin/bash

{% set ip = salt['grains.get']('ip_interfaces:ens32:0') -%}
echo "ip address = {{ ip }}"

2.表达式

在模板中使用表达式的格式是:

{% 表达式 -%}
设置变量也是表达式得一种
1.1 if判断
[root@salt-master shell_scripts]# vim test.j2 
#!/bin/bash
{% if grains['os'] == 'CentOS' -%}
        echo "hello world"
{% else -%}
        echo "HELLO WORLD"
{% endif -%}

执行结果

[root@salt-master shell_scripts]# salt 'salt-client' state.sls shell_scripts.shell
[root@salt-client ~]# cat test.sh 
#!/bin/bash
echo "hello world"
1.2for循环
#!/bin/bash
{% for i in grains['ipv4'] -%}
iptables -A INPUT -s {{ i }} -j ACCEPT
{% endfor %}

执行结果

[root@salt-client ~]# cat test.sh 
#!/bin/bash
iptables -A INPUT -s 127.0.0.1 -j ACCEPT
iptables -A INPUT -s 192.168.1.211 -j ACCEPT
iptables -A INPUT -s 192.168.122.1 -j ACCEPT

posted @ 2021-01-24 17:31  借风拥你  阅读(217)  评论(0编辑  收藏  举报