原文链接:http://networkbit.ch/python-jinja-template/

template.txt如下:

hostname {{ name }}

interface Loopback1
ip address 10.1..{{ id }} 255.255.255.255 {% for vlan, name in vlans.items() %}
vlan {{ vlan }}
name {{ name }}
{% endfor -%} router bgp {{ id }}
{% for neighbor in bgp %}
neighbor {{ neighbor.neighbor }} remote-as {{ neighbor.remote-as }}
{% endfor %}

data.yml 如下:

name: R1
id:
vlans:
: User
: Voice
: Video
bgp:
- neighbor: 10.1.1.1
remote-as:
- neighbor: 10.1.2.2
remote-as:
- neighbor: 10.1.3.3
remote-as:
Create Python script:
#Imports from Jinja2
from jinja2 import Environment, FileSystemLoader #Import YAML from PyYAML
import yaml #Load data from YAML file into Python dictionary
config = yaml.load(open('./data.yml')) #Load Jinja2 template
env = Environment(loader = FileSystemLoader('./'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('template.txt') #Render template using data and print the output
print(template.render(config))

Python Jinja2 Template: YAML File Cisco Example Tutorial的更多相关文章

  1. Python中Template使用的一个小技巧

    Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...

  2. python之读取yaml数据

    一.yaml简介 yaml:一种标记语言,专门用来写配置文件. 二.yaml基础语法 区分大小写: 使用缩进表示层级关系: 使用空格键缩进,而非Tab键缩进 缩进的空格数目不固定,只需要相同层级的元素 ...

  3. plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory

    装一台新服务器环境的时候,装uwsgi报错: plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or di ...

  4. python d:\test.py File "<stdin>", line 1 python d:\test.py ^ SyntaxError: invalid syntax

    pyhton出错: python d:\test.py File "<stdin>", line 1 python d:\test.py ^SyntaxError: i ...

  5. qingstor python-sdk 安装错误 src/MD2.c:31:20: fatal error: Python.h: No such file or directory

    ubuntu安装python qingstor-sdk, src/MD2.c:31:20: fatal error: Python.h: No such file or directory compi ...

  6. Ubuntu下 fatal error: Python.h: No such file or directory 解决方法

    参考: fatal error: Python.h: No such file or directory Ubuntu下 fatal error: Python.h: No such file or ...

  7. _mysql.c:29:20: error: Python.h: No such file or directory

    在Centos系统中安装 pip install MySQL-python 提示: _mysql.c:29:20: error: Python.h: No such file or directory ...

  8. this inspection reports usage of the default file template for file header

    使用idea创建一个java class的时候会出现如下的warning: this inspection reports usage of the default file template for ...

  9. python之函数用法file()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法file() #file() #说明:file()内建函数它的功能等于open(),但 ...

随机推荐

  1. html层重叠 相同尺寸透明flash重叠的解决办法

    <EMBED style="z-index:1; position:absolute; top:110px;" src="http://www.jintaisd.c ...

  2. PHP NULL 合并运算符

    HP 7 新增加的 NULL 合并运算符(??)是用于执行isset()检测的三元运算的快捷方式. NULL 合并运算符会判断变量是否存在且值不为NULL,如果是,它就会返回自身的值,否则返回它的第二 ...

  3. MediaStreamConstraints对象

    MediaStreamConstraints对象作用是在调用getUserMedia()时用于指定应在返回的MediaStream中包括哪些轨道,以及(可选)为这些轨道的设置约束. 属性 audio布 ...

  4. luogu P5633 最小度限制生成树 wqs二分

    LINK:最小度限制生成树 还是WQS二分的模板题 不过相当于我WQS二分的复习题. 对于求出强制k个的答案 dp能做不过复杂度太高了. 世界上定义F(x)表示选出x个的答案 画成图像 其实形成了一个 ...

  5. 剑指 Offer 53 - II. 0~n-1中缺失的数字

    本题 题目链接 题目描述 我的题解 二分法 思路分析 排序数组中的搜索问题,首先想到二分法 当nums[center] > center 时,缺少的数在左区间 当nums[center] = c ...

  6. 排查JVM内存泄漏的命令

    1. jps 使用 jps -l -m 获取到当前jvm进程的pid,通过上述命令获取到了服务的进程号 jps(JVM Process Status Tool):显示指定系统内所有的HotSpot虚拟 ...

  7. 024_go语言中的缓冲通道

    代码演示 package main import "fmt" func main() { messages := make(chan string, 2) messages < ...

  8. 编程与算法(一)、C语言实现二分法(方程近似解)

    一.二分法 假设有这样一个函数f(x) 函数与x轴有一个交点(也就是f(a)*f(b)<0,a<b),现在我们要求这个点的x值,也就是方程f(x)=0的一个实根 直接解显然不合适,那么接下 ...

  9. java 异常二

    一 捕获异常try…catch…finally 捕获:Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理 捕获异常格式: try { //需要被检测的语句. } catch(异 ...

  10. C#LeetCode刷题之#31-下一个排列(Next Permutation)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4965 访问. 实现获取下一个排列的函数,算法需要将 ...