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(),但 ...
随机推荐
- Python之filter、map、reduce函数
简介三函数: 高阶函数:一个函数可以接收另一个函数作为参数,这种函数称之为高阶函数. filter.map.reduce三个函数都是高阶函数,且语法都一致:filter/map/reduce(func ...
- springboot整合邮件发送(163邮箱发送为例)
先登录163邮箱获取授权 勾选后安装提示会叫你设置授权密码之类的:记住授权的密码 1.引入maven依赖 <dependency> <groupId>org.springfr ...
- ElasticSearch学习中的坑
elasticsearch 版本为 6.8.2 1 安装完启动报错: 解决,建立新用户执行 [root@localhost bin]# ./elasticsearch [2019-09-01T05 ...
- 【好文推荐】黑莓OS手册是如何详细阐述底层的进程和线程模型的?
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- Boolean源码解剖学
一.类继承 Boolean的源码类定义部分如下: 1 public final class Boolean implements java.io.Serializable, 2 Comparable& ...
- json-lib无法下载
maven无法下载json-lib 配置一下这个 <classifier>jdk15</classifier> 因为远程提供了两个
- 面试官:怎么做JDK8的内存调优?
面试官:怎么做JDK8的内存调优? 看着面试官真诚的眼神,心中暗想看起来年纪轻轻却提出如此直击灵魂的问题.擦了擦额头上汗,我稍微调整了一下紧张的情绪,对面试官说: 在内存调优之前,需要先了解JDK8的 ...
- synchronized 锁的原理
synchronized 的基本认识 在多线程并发编程中 synchronized 一直是元老级角色,很 多人都会称呼它为重量级锁.但是,随着 Java SE 1.6 对 synchronized 进 ...
- 018_go语言中的结构体
代码演示 package main import "fmt" type person struct { name string age int } func main() { fm ...
- 8道python练习题,能做出来的没几个
变量的定义 程序就是用来处理数据的,而变量就是用来存储数据的 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道 ...