Merge native configurations (IOS)¶
In our previous example we merged two objects that already complied with the openconfig models. In this example, we are going to merge to native configurations.
Let’s start by loading the driver as usual:
[1]:
from ntc_rosetta import get_driver
ios = get_driver("ios", "openconfig")
ios_processor = ios()
Now we are going to load a file with the “running” configuration:
[2]:
with open("data/ios/config.txt", "r") as f:
running_config = f.read()
And now a different file with the “candidate” config:
[3]:
with open("data/ios/new_config.txt", "r") as f:
candidate_config = f.read()
Let’s see the files side by side highlighting the differences:
[4]:
!diff -y data/ios/config.txt data/ios/new_config.txt
interface FastEthernet1 interface FastEthernet1
description This is Fa1 description This is Fa1
shutdown shutdown
exit exit
! !
interface FastEthernet1.1 interface FastEthernet1.1
description This is Fa1.1 description This is Fa1.1
exit exit
! !
interface FastEthernet1.2 interface FastEthernet1.2
description This is Fa1.2 description This is Fa1.2
exit exit
! !
interface FastEthernet3 interface FastEthernet3
description This is Fa3 description This is Fa3
no shutdown no shutdown
switchport mode access switchport mode access
switchport access vlan 10 switchport access vlan 10
exit exit
! !
interface FastEthernet4 interface FastEthernet4
shutdown shutdown
switchport mode trunk switchport mode trunk
switchport trunk allowed vlan 10,20 switchport trunk allowed vlan 10,20
exit exit
! !
vlan 10 vlan 10
name prod name prod
no shutdown | shutdown
exit exit
! !
vlan 20 | vlan 30
name dev | name staging
shutdown | no shutdown
exit exit
! !
As you can see vlan 20 is gone, vlan 10 has been suspended and there is a new vlan 30.
Now let’s parse those configurations as we did in our first tutorial:
[5]:
parsed_candidate = ios_processor.parse(native={"dev_conf": candidate_config})
parsed_running = ios_processor.parse(native={"dev_conf": running_config})
Now that we have parsed both native configurations, doing a merge operation is identical as in our previous tutorial:
[6]:
config = ios_processor.merge(
candidate=parsed_candidate.raw_value(),
running=parsed_running.raw_value()
)
print(config)
no vlan 20
vlan 10
shutdown
exit
!
vlan 30
name staging
no shutdown
exit
!