python jinja2_Python Jinja2的简单使用「终于解决」

python jinja2_Python Jinja2的简单使用「终于解决」参考链接:https://jinja.palletsprojects.com/en/2.11.x/下面测试使用jinja2版本:version=”2.11.2″,python版本:version=”3.7.4″Template使用fromjinja2importTemplatetemplate=Template(‘Hello{{name}}’)print(temp…

大家好,欢迎来到IT知识分享网。

参考链接: https://jinja.palletsprojects.com/en/2.11.x/

下面测试使用jinja2版本: version = “2.11.2” , python版本: version = “3.7.4”

Template使用

from jinja2 import Template

template = Template(‘Hello {
{ name }}’)

print(template.render(name=’dsg’))

# > output: Hello dsg

template = Template(‘service: {
{ service.name }}, version: {
{ service.version }}’)

service = {‘name’: ‘mysql’, ‘version’: 5.7}

print(template.render(service=service))

# > output: service: mysql, version: 3.7

template = Template(‘service: {
{ service.name }}, version: {
{ service.version }}’)

service = {‘service’: {‘name’: ‘mysql’, ‘version’: 5.7}}

print(template.render(service))

template = Template(‘service list: {% for s in services %} {
{s}};{% endfor %}’)

services = {‘services’: [‘mysql’, ‘nginx’]}

print(template.render(services))

# > output: service list: mysql; nginx;

去除空格

template = Template(‘service list: {% for s in services -%} {
{s}};{%- endfor %}’)

services = {‘services’: [‘mysql’, ‘nginx’]}

print(template.render(services))

# > output: service list: mysql;nginx;

直接读j2文件, j2文件示例:

# filename service.j2

name: {
{ name }}

version: {
{ version }}

with open(‘app/tmp/service.j2’, ‘r’) as f:

buffer = f.read()

service = {‘name’: ‘mysql’, ‘version’: 5.7}

template = Template(buffer)

print(template.render(service))

# output:

# > name: mysql

# > version: 5.7

使用模板文件实现部署替换配置文件

# .

# |– app

# | |– __init__.py # 必须,

# | |– tmp

# | |– service.js # 配置文件模板

# | |– nginx.js # 配置文件模板

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader(‘app’, ‘tmp’))

template = env.get_template(‘service.j2’)

content = template.render(name=’mysql’, version=’5.8′)

with open(‘./config’, ‘w’) as f:

f.write(content)

print(content)

# output:

# > name: mysql

# > version: 5.7

import yaml

with open(‘app/config.yml’) as f:

y = yaml.safe_load(f)

print(y)

template = Template(‘basedir = {% for node in nodes %} {% if “nginx” in nodes[node].modules %} {
{ nodes[node].basedir }} {% endif %}{% endfor %}’)

print(template.render(y))

# output:

# {‘nodes’: {‘node1’: {‘host’: ‘10.0.0.2’, ‘basedir’: ‘/data1’, ‘modules’: [‘mysql’, ‘nginx’]}, ‘node2’: {‘host’: ‘10.0.0.3’, ‘basedir’: ‘/data2’, ‘modules’: [‘nginx’]}}, ‘services’: {‘mysql’: {‘config’: {‘port’: 3306, ‘passwd’: 123456}}, ‘nginx’: {‘config’: {‘port’: 8088}}}}

# basedir = /data1 /data2

env = Environment(loader=PackageLoader(‘app’, ‘tmp’))

template = env.get_template(‘nginx.j2’)

content = template.render(y)

with open(‘./config’, ‘w’) as f:

f.write(content)

print(content)

nginx.j2文件内容:

version: ‘2’

services:

nginx:

image: nginx:11

container_name: nginx

ports:

– {
{ services.nginx.config.port }}:80

volumes:

– {% for node in nodes -%} {%- if “nginx” in nodes[node].modules %}{
{nodes[node].basedir}}{%- endif %} {%- endfor %}/nginx/logs/:/var/lib/nginx/logs/

– /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime

restart: always

networks:

– default_bridge

networks:

default_bridge:

external: true

config.yml文件内容:

nodes:

node1:

host: 10.0.0.2

basedir: /data1

modules:

– mysql

– nginx

node2:

host: 10.0.0.3

basedir: /data2

modules:

– nginx

services:

mysql:

config:

port: 3306

passwd: 123456

nginx:

config:

port: 8088

config文件最后写入的内容:

version: ‘2’

services:

nginx:

image: nginx:11

container_name: nginx

ports:

– 8088:80

volumes:

– /data1/data2/nginx/logs/:/var/lib/nginx/logs/

– /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime

restart: always

networks:

– default_bridge

networks:

default_bridge:

external: true

上面并没有得到我们想要的结果,如果nginx只在一台机器部署的话,结果会是我们想要的,但是多机部署就不行了。

这里我建议尽量少在模板文件中写逻辑判断,逻辑判断应该用python去实现,我们再真实环境中去替换nginx配置的basedir时都是by机器的,我们可以用python先解析出如下格式的数据:

{‘10.0.0.2’:{‘nginx’:{‘basedir’:’/data1′}},

‘10.0.0.2’:{‘mysql’:{‘basedir’:’/data1′}},

‘10.0.0.3’:{‘nginx’:{‘basedir’:’/data2′}}

}

如果我们要部署给10.0.0.2部署nginx时就能够正确获取basedir了。 具体怎么实现, 看大家了。

yml里面字典和列表

config.yml

nodes:

node1:

host: 10.0.0.2

basedir: /data1

modules:

– mysql

– nginx

node2:

host: 10.0.0.3

basedir: /data2

modules:

– nginx

template = Template(‘ {
{ nodes }}’)

print(template.render(y))

# 输出:

{‘node1’: {‘host’: ‘10.0.0.2’, ‘basedir’: ‘/data1’, ‘modules’: [‘mysql’, ‘nginx’]}, ‘node2’: {‘host’: ‘10.0.0.3’, ‘basedir’: ‘/data2’, ‘modules’: [‘nginx’]}}

config.xml

nodes:

– node1:

host: 10.0.0.2

basedir: /data1

modules:

– mysql

– nginx

– node2:

host: 10.0.0.3

basedir: /data2

modules:

– nginx

template = Template(‘ {
{ nodes }}’)

print(template.render(y))

# 输出:

[{‘node1’: {‘host’: ‘10.0.0.2’, ‘basedir’: ‘/data1’, ‘modules’: [‘mysql’, ‘nginx’]}}, {‘node2’: {‘host’: ‘10.0.0.3’, ‘basedir’: ‘/data2’, ‘modules’: [‘nginx’]}}]

yml格式不同,取值就会不同。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11101.html

(0)
上一篇 2023-02-23 21:00
下一篇 2023-02-25 11:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信