Professional Documents
Culture Documents
思科社区公开课 第71期 Cisco Netbox实战 2022.07
思科社区公开课 第71期 Cisco Netbox实战 2022.07
Cisco Netbox实战
教主技术进化论 2022
教主VIP,聊点高级的!
课程目录
1. Nexus从Netbox同步配置
2. NSO从Netbox同步配置
2
1. Nexus从Netbox同步配置
内部项目地址:
http://pygitlab.qytang.com/root/2022_21_nexus_from_netbox.git
原始项目:
https://developer.cisco.com/codeexchange/github/repo/hpreston/nxos-netbox-sync/
3
1 Nexus从Netbox同步配置
教主技术进化论
设备
• 本地网络设备使用Nexus9K
• 主要检查Nexus接口和VLAN配置
• 设备配置数据位于Netbox
4
1 Nexus从Netbox同步配置
教主技术进化论
Netbox N9K1
5
1 Nexus从Netbox同步配置
教主技术进化论
Netbox N9K1接口
6
1 Nexus从Netbox同步配置
教主技术进化论
代码视图
第一部分: 通知markdown模板
第二部分: 工具代码
• dingding_notify.py [钉钉通知]
• get_from_netbox.py [从netbox获取设备配置数
据]
• get_from_pyats.py [pyats获取设备当前配置状
态,并且配置设备]
• login_info.py [登录认证信息]
• message_templates.py [读取markdown模板]
• tests.py [测试netbox和pyats数据的差别]
check_device.py步骤分析
1. 从netbox获取配置
2. pyats从设备获取配置
3. 比较netbox和pyats配置的区
别
4. 使用pyats补齐或者纠正配置
7
1 Nexus从Netbox同步配置
教主技术进化论
值得参考代码.1
import pynetbox
from utils.login_info import netbox_url, netbox_token, switch_name
def interfaces_sot():
interfaces = netbox.dcim.interfaces.filter(device_id=device.id, mgmt_only=False)
从netbox获取接
return interfaces
口和vlan的配置
def vlans_sot():
vlans = netbox.ipam.vlans.filter(site_id=device.site.id)
return vlans
8
1 Nexus从Netbox同步配置
教主技术进化论
值得参考代码.2
from genie.testbed import load
from genie.libs.conf.vlan import Vlan
from genie.libs.conf.interface import Interface
from utils.login_info import switch_name, swtich_mgmt_port
from utils.login_info import device_username, device_password
get_from_pyats.p from utils.get_from_netbox import switch_mgmt_ip
y
device_details = {"devices": {
switch_name: {
"protocol": "ssh",
"ip": switch_mgmt_ip,
"port": swtich_mgmt_port,
"username": device_username,
"password": device_password,
"os": "nxos",
"ssh_options": "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
}}}
直接从Python字典读取,
testbed = load(device_details)
不必使用testbed文件
device = testbed.devices[switch_name]
# device.connect(learn_hostname=True)
device.connect(learn_hostname=True,
log_stdout=False,
ssh_options='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null')
9
1 Nexus从Netbox同步配置
教主技术进化论
值得参考代码.3
def interface_access_configure(netbox_interface):
if netbox_interface.mode.label == "Access":
# Configure native and tagged vlans on a trunk
if netbox_interface.name in device.interfaces.keys():
get_from_pyats.p new_interface = device.interfaces[netbox_interface.name]
y else:
new_interface = Interface(name=netbox_interface.name, device=device)
new_interface.switchport_enable = True
10
1 Nexus从Netbox同步配置
教主技术进化论
值得参考代码.4
return results
11
1 Nexus从Netbox同步配置
教主技术进化论
执行效果
Console显示效 钉钉通告效果
果
12
2. NSO从Netbox同步配置
内部项目地址:
http://pygitlab.qytang.com/root/netdevops2022.g
it
分支:
netbox_ci
13
1 Nexus从Netbox同步配置
教主技术进化论
Variables
14
1 Nexus从Netbox同步配置
教主技术进化论
保护分支
15
1 Nexus从Netbox同步配置
教主技术进化论
Netbox 设备配置
16
1 Nexus从Netbox同步配置
教主技术进化论
17
1 Nexus从Netbox同步配置
教主技术进化论
Netbox 接口配置
18
1 Nexus从Netbox同步配置
教主技术进化论
从Netbox获取数据的代码
19
1 Nexus从Netbox同步配置
教主技术进化论
从环境变量读取数据, 手动配置的authgroups信息
20