原文链接: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. Android上传图片的两种方式

    参考:https://www.jianshu.com/p/f47943880cea

  2. 解惑,什么是data-attribute ?

    在接触 Web前端开发的一段时间,有时会去看Google或者百度的源代码,有某些地方定义了 data-key ,这种语法 但是如果你直接去 Google data-key 或 data-item 可能 ...

  3. 项目总结,彻底掌握NodeJS中如何使用Sequelize

    前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...

  4. 选择排序的实现以及如何编写测试 #CS61B-sp18-3.1

    Selection Sort的思想: 就是在一系列数字中先找到一个最小的放在所有数字的第一个位置上,然后再从余下的数字里面找最小个放在余下的数字里的第一个位置上. 例如: 在这段数据里面我们找到最小的 ...

  5. 线程_Process基础语法

    """ Process([group[,target[,name[,args[,kwargs]]]]]) group:大多数情况下用不到 target:表示这个进程实例所 ...

  6. JavaScript Set对象

    JavaScript Set对象 Set 用于存储任何类型的唯一值,无论是基本类型还是引用类型. 只有值没有键 严格类型检测存储,字符串数字不等同于数值型数字 存储的值具有唯一性 遍历顺序是添加的顺序 ...

  7. javaWeb Maven Runner设置中文乱码

    将Runner设置为 -DarchetypeCatal! 使maven在没有网络的情况下在本地查找下载好的插件 配置 -Dfile.encoding=gb2312 防止中文乱码

  8. Python稳居编程语言榜首,看完这篇总结,你就明白为什么要学它了

    最近,网上流传一组<人工智能实验教材>的图片,照片火起来的原因是教材是为幼儿园的小朋友们设计的! Python被列入小学.初高中教材已不是新鲜事,现在又成功“入侵”了幼儿园,对此有网友调侃 ...

  9. Python使用socketServer包搭建简易服务器过程详解

    官方提供了socketserver包去方便我们快速的搭建一个服务器框架. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的 ...

  10. CSS漂亮盒子(上)

    HTML文档中的所有元素都是由矩形盒子构成的--不管是包含页面结构的容器元素,还是段落中的每行文本,归根结底都是盒子. 1.背景颜色 设置页面背景颜色. body { background-color ...