@

一、简介

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

最新文章

  1. 2 Unique Binary Search Trees II_Leetcode
  2. java中的注解(Annotation)
  3. classpath路径和properties
  4. 利用Native Client OLEDB 11 高效率地对SQL SERVER 进行查询和插入操作
  5. 阅读Nosql代码有感
  6. C语言标量类型(转)
  7. 练习JavaScript实现梯形乘法表 效果:
  8. Part 92 Significance of Thread Join and Thread IsAlive functions
  9. 好用的ajax后台框架
  10. 【转】cocos2d-x学习笔记03:绘制基本图元
  11. java集合相关问题
  12. 【转】Android UI 五种布局
  13. [SCOI2009] 迷路
  14. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group t1,customer t2
  15. Rails6使用tailwind CSS
  16. SharedPreference映射Java类
  17. js获取子元素的内容
  18. Python基础【day01】:python 2和3区别(四)
  19. .Net进阶系列(12)-异步多线程(Thread和ThreadPool)(被替换)
  20. centos7 网卡命名

热门文章

  1. 【Linux】【Services】【Package】Basic
  2. 使用Spring Data ElasticSearch框架来处理索引
  3. 阿里巴巴Java开发手册摘要(一)
  4. java 注解的几大作用及使用方法详解
  5. typescript接口---interface
  6. [HarekazeCTF2019]baby_rop
  7. TMS570LS3137笔记-内部Flash FEE使用
  8. AT5221 [ABC140C] Maximal Value 题解
  9. 在JSP页面里,时间控件的JS位置在下面然后就显示不出来
  10. c++设计模式概述之模板方法