2018-12-27 批量配置生成实践

批量配置设备,最佳搭配是 Jinjia2+YAML+Python
因为很多重复内容,只有部分参数稍有区别,所以把这些参数用变量代替,就可以通过编程来解决了。
Jinjia2用来定义配置和参数变量的同一模板,YAML用来定义变量的值,多少路由器,就要定义对应的值。Python用于统一这几个系统,完成整个工作。
1、需求:配置华为路由器,有名称,loopback地址,互联地址等。

#
sysname {{hostname}}
#
undo pnp enable
#
interface LoopBack0
 ip address {{v4_loopback}} 32
 ip address {{v6_loopback}} 128
 isis enable 1
 Isis ipv6 enable 1
 isis circuit-level level-2
#
info-center loghost source LoopBack0
info-center loghost 172.16.18.15
info-center timestamp log date precision-time millisecond
info-center timestamp trap date precision-time millisecond
#
undo user-security-policy enable
undo telnet server enable
y
#
undo radius enable
#
Vlan batch 69 101 2018
#
interface Eth-Trunk15
 portswitch
 port trunk allow-pass vlan 69 101 2018
 mode lacp-static
 load-balance l4
#
interface GigabitEthernet0/2/0
 undo shutdown
 ip address {{v4_P2P_POP_Name}} 31
 ip address {{v6_P2P_POP_Name}} 127
 ipv6 enable
 isis enable 1
 isis ipv6 enable 1
 isis circuit-type p2p
 isis circuit-level level-2
 dcn
#
#
interface GigabitEthernet0/2/4
 undo shutdown
 eth-trunk 15
 dcn
#
interface GigabitEthernet0/2/5
 undo shutdown
 eth-trunk 15
 dcn
#
#
interface Vlanif69
 ip address {{v4_Pool_69}} 24
 isis enable 1
 isis silent
 isis circuit-level level-2
#
interface Vlanif101
 ip address {{v4_Pool_101}} 24
 isis enable 1
 isis silent
 isis circuit-level level-2
#
interface Vlanif2018
 ip address {{v4_P2P_2018}} 30
 isis enable 1
 isis silent
 isis circuit-level level-2
#

2、思路和知识点:设备名称,接口地址作为变量,这些要写到YAML文件里,而固定不变的部分,用Jinjia2写到模板里。
在Jinjia2里,用两个花括号,中间变量名。
Python要做的事就是,读取YAML文件--加载jinjia2模板--生成配置--将配置写入文件。
这里有一些循环,比如hostname,可以是R1, R2, R3, R4…… 就是模板里的 sysname {{hostname}}
Python 里的for 循环

for each in listA:
    print(each)
测试实例:
anthony$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hostname = ['R1','R2','R3','R4','R5']
>>> for each in hostname:
...     print("This is "+ each)
...
This is R1
This is R2
This is R3
This is R4
This is R5
>>>

Python 的while循环

>>> kevin = 1
>>> while kevin < 18:
...     print("Kevin is " + str(kevin))
...     print("Kevin can not drink alcoho")
...     kevin = kevin +1
...
Kevin is 1
Kevin can not drink alcoho
Kevin is 2
Kevin can not drink alcoho
Kevin is 3
Kevin can not drink alcoho
Kevin is 4
Kevin can not drink alcoho
Kevin is 5
Kevin can not drink alcoho
Kevin is 6
Kevin can not drink alcoho
Kevin is 7
Kevin can not drink alcoho
Kevin is 8
Kevin can not drink alcoho
Kevin is 9
Kevin can not drink alcoho
Kevin is 10
Kevin can not drink alcoho
Kevin is 11
Kevin can not drink alcoho
Kevin is 12
Kevin can not drink alcoho
Kevin is 13
Kevin can not drink alcoho
Kevin is 14
Kevin can not drink alcoho
Kevin is 15
Kevin can not drink alcoho
Kevin is 16
Kevin can not drink alcoho
Kevin is 17
Kevin can not drink alcoho
>>>

Python字典类型的循环

>>> code = {"R1":"001","R2":"002","R3":"003","R4":"004","R5":"005"}
#用老办法只能取出第一层的数据
>>> for each in code:
...     print(each)
...
R1
R2
R3
R4
R5
#使用两个变量 name,num来定义,使用 code.items()函数取出数值
>>> for name, num in code.items():
...     print(name+ " is " + num)
...
R1 is 001
R2 is 002
R3 is 003
R4 is 004
R5 is 005
>>>

jinjia2的循环,要把内容写到{%%}里面:

{% for each in hostname %}
  This is {{ each }}
{% endfor %}
#基于字典的循环
{% for name, num in code.items() %}
  {{ key }} is  {{ num }}
{% endfor %}

Python的条件判断:if, else

>>> age = 19
>>> if age > 18 :
...     print("Adult")
...
Adult
>>> age = 8
>>> if age > 18:
...     print("This is adult")
... else:
...     print("This is a kid")
...
This is a kid
>>>

Jinjia2里的条件判断: if else

{% if weather == 'windy' %}
This is a {{ weather }} day.
{% endif %}

Python的文件操作:
new_file = open('filename','w') #准备一个名叫filename的文件准备写入,但是注意,如果已有文件并有内容,这个函数会覆盖原文件。
new_file.write('content') #写入内容为content
new_file.close() #关闭文件

>>> new_file = open('./test.txt','w')
>>> new_file.write('This is a test file\n')
20
>>> new_file.close()
>>>
anthony$ ls | grep test
test.txt
anthony$ cat test.txt
This is a test file
anthony$

明天再接着写YAML文件和PYTHON部分。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容