Skip to Content
可编程网络自动化
book

可编程网络自动化

by Jason Edelman, Scott S. Lowe, Matt Oswalt
January 2023
Intermediate to advanced
458 pages
14h 16m
Chinese
Posts & Telecom Press
Content preview from 可编程网络自动化
154
6
# 在声明之后,过滤器会被加入ENV对象中。bracket_expansion是我们传入的函数,
# 在渲染模板时,模板引擎会执行此函数
ENV.filters['bracket_expansion'] = bracket_expansion
template = ENV.get_template("template.j2")
# 作为过滤器传入的bracket_expansion函数需要一个文本模式才能工作,让我们
# 将其作为iface_pattern传入
print
(template.render(iface_pattern='GigabitEthernet0/0/[0-3]'))
这是一个非常强大的工具——你完全可以编写自己的
Python
函数来将其提升到一个更高的
水平。可以尝试一下其他的文本操作函数和库,或者自己编写。
6.3.6
 
Jinja
模板继承
在为网络配置创建规模更大、功能更强的模板时,你可能想把模板分解为更小、更具体的
部分。一个模板用于
VLAN
配置
,一个模板用于接口,可能还有一个模板用于路由协议,
这都是很常见的情况。尽管不是必需的,但这种组织工具可以提供更大的灵活性。问题在
于该如何以有意义的方式将这些模板彼此联系起来以形成一套完整的配置呢?
Jinja
允许在模板文件中执行继承
,这是解决上述问题的简便方法。例如,
vlans.j2
文件只
包含
VLAN
配置
,你可以在另一个模板文件中继承该文件来生成
VLAN
配置。也可以自
己编写接口配置模板,其中的
VLAN ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

弱监督学习实用指南

弱监督学习实用指南

Wee Hyong Tok, Amit Bahree, Senja Filipi
Python和NLTK实现自然语言处理

Python和NLTK实现自然语言处理

Posts & Telecom Press, Nitin Hardeniya
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787115606181