原文链接: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. CS231n 斯坦福李飞飞视觉识别课程

    本文是个人在学习<CS231n 斯坦福李飞飞视觉识别课程>的学习笔记. 第一讲:课程简介 课时1 计算机视觉概述 课时2 计算机视觉历史背景 课时3 课程后勤 选读书籍<DeepLe ...

  2. 我自己总结的sqlite的命令行命令集

    我自己总结的sqlite 的命令行命令 导入文本数据文件时,设置分隔符为","sql>.separator "," sql>import devic ...

  3. python3 url编码与解码

    在通过浏览器修改数据库时,要对url内容进行编码 quote()编码; unquote()解码; 直接上代码:

  4. form-create 组件生成规则说明

    本文介绍form-create生成规则与组件的关系 form-create 是一个可以通过 JSON 生成具有动态渲染.数据收集.验证和提交功能的表单生成器.并且支持生成任何 Vue 组件.结合内置1 ...

  5. 第五章 泛型&集合

    5.1.泛型 概述:泛型是是JDK5中引入的特性,它提供了编译时类型安全检测机制,该机制允许在编译时检测到非法的类型,它的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数. 泛型类: // ...

  6. Python pass语句

    Python pass语句:空语句,主要用于保持程序结构的完整性 或者 函数想要添加某种功能,但是还没有想好具体应该怎么写. 在 for 循环中使用 pass: lst = [7,8,9,4] for ...

  7. python基础day7_购物车实例

    print("欢迎光临") money = input("请输入您的金额:") shopping_car ={} li = [{"name" ...

  8. Python os.stat_float_times() 方法

    概述 os.stat_float_times() 方法用于决定stat_result是否以float对象显示时间戳.高佣联盟 www.cgewang.com 语法 stat_float_times() ...

  9. Python Cookbook(第3版) 中文版 pdf完整版|网盘下载内附提取码

    Python Cookbook(第3版)中文版介绍了Python应用在各个领域中的一些使用技巧和方法,其主题涵盖了数据结构和算法,字符串和文本,数字.日期和时间,迭代器和生成器,文件和I/O,数据编码 ...

  10. ElasticSearch添加索引

    1. 编写索引内容 节点解释: settings:配置信息 "number_of_replicas": 0  不需要备份(单节点的ElasticSearch使用) "ma ...