Python Jinja2 Template: YAML File Cisco Example Tutorial
原文链接: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的更多相关文章
- Python中Template使用的一个小技巧
Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...
- python之读取yaml数据
一.yaml简介 yaml:一种标记语言,专门用来写配置文件. 二.yaml基础语法 区分大小写: 使用缩进表示层级关系: 使用空格键缩进,而非Tab键缩进 缩进的空格数目不固定,只需要相同层级的元素 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- _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 ...
- 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 ...
- python之函数用法file()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法file() #file() #说明:file()内建函数它的功能等于open(),但 ...
随机推荐
- android获取各种系统路径的方法
链接https://blog.csdn.net/qq_26296197/article/details/51909423 通过Environment获取的 Environment.getDataDir ...
- linux创建逻辑卷(lv)并挂载
新加磁盘启动系统后,查看现有磁盘使用情况 命令 df -h 查看现有磁盘情况,我们发现系统已经有一个10G的磁盘sdb,这个磁盘共有1305个柱面,每个柱面大小是8225280 bytes (大约8M ...
- reverse 字符串翻转
头文件 algorithm string s="hello"; reverse(s.begin(),s.end()); char c[]="hello"; re ...
- 远光武汉研发中心区块链事业部Java面试总结
面试在约定的时间准时进行,也是采用腾讯会议远程面试的方式.但是这是我第一次遇到面试官未打开摄像头的情况,后面经过沟通,双方都打开摄像头进行交流. 之前了解这个岗位主要是区块链相关的Java开发,所以事 ...
- Day02_CMS前端开发
CMS前端开发 1 Vue.js与Webpack研究 参考链接:https://www.cnblogs.com/artwalker/p/13387386.html 2 CMS前端工程创建 2.1 导入 ...
- matplotlib绘制子图
fig,subs = plt.subplots(2,2) subs[0][0].plot(data_math_C1) subs[0][0].set_title('C_1 曲线') subs[0][1] ...
- 比较两个等长的字符串,若相同,则输出Match!,若不同,则输出No Match!
文章目录 问题 代码 运行结果 问题 比较两个等长的字符串,若相同,则输出Match!,若不同,则输出No Match! 代码 data segment str1 db 'ASDFGHJKL';字符串 ...
- Python execfile() 函数
描述 execfile() 函数可以用来执行一个文件.每组词 www.cgewang.com 语法 以下是 execfile() 方法的语法: execfile(filename[, globals[ ...
- Python List max()方法
描述 max() 方法返回列表元素中的最大值.高佣联盟 www.cgewang.com 语法 max()方法语法: max(list) 参数 list -- 要返回最大值的列表. 返回值 返回列表元素 ...
- 【oracle】-表连接类型:内连接,外连接...
一.数据准备 1.emp表 delete from emp; insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) ...