How do I use insert_after()
?
In the documentation I checked how to use this method, but having troubles, essentially I want to find a line in the config as an anchor point that I know exists:
!
cable profile downstream DS1
!
And then add more lines after it
cable profile load-balance ALPN
method utilization us-method modems
threshold load 30
threshold stability 70
policy pure-ds-load
docsis-policy 1
interval 300
!
cable profile load-balance ALPY
restricted
interval 5
tag TAG_STB
!
What should I do to accomplish this?
If you have a list of lines to add in order, reverse() the list… see what I do with all_changes_list
below
from ciscoconfparse2 import CiscoConfParse
config = """!
!
!
cable profile downstream DS1
!
!"""
afterSection = """!
cable profile load-balance ALPN
method utilization us-method modems
threshold load 30
threshold stability 70
policy pure-ds-load
docsis-policy 1
interval 300
!
cable profile load-balance ALPY
restricted
interval 5
tag TAG_STB
!
"""
parse = CiscoConfParse(config)
all_changes_list = afterSection.splitlines()
all_changes_list.reverse()
for anchor_obj in parse.find_objects("^cables+profiles+downstream"):
# Iterate over your *reversed config* and insert lines after the anchor
for line in all_changes_list:
anchor_obj.insert_after(line)
parse.commit() # commit() is req'd any time you finish modifying the config...