mpenning / ciscoconfparse

Parse, Audit, Query, Build, and Modify Arista / Cisco / Juniper / Palo Alto / F5 configurations.
http://www.pennington.net/py/ciscoconfparse/
GNU General Public License v3.0
793 stars 220 forks source link

append_to_family method breaks parent/child relationship two or more levels below #101

Closed Siemir closed 6 years ago

Siemir commented 6 years ago

Hello, while working with automating policy maps on a router I have encountered quite a weird behavior - my script was working for one loop, but for anything more it was sticking all the commands to the last child in policy-map instead of one for each class-map. Trying it on much simpler example (modified append_to_family example) the effect was the same:

config = [
    '!',
    'interface Serial1/0',
    ' ip address 1.1.1.1 255.255.255.252',
    '  test line 1',
    'interface Serial1/1',
    ]
parse = CiscoConfParse(config)
for obj in parse.find_objects(r'^interface'):
  obj.append_to_family(' carrier-delay msec 500')
!
interface Serial1/0
 ip address 1.1.1.1 255.255.255.252
 carrier-delay msec 500
  test line 1
interface Serial1/1
 carrier-delay msec 500

I think the problem is that it should look for a last child in a block not the first one, for my use I have modified the function in ccp_abc.py file by adding

while len(last_child.children) > 0: last_child = last_child.children[-1]

before insert_after function, which now preserves parent/child relationship on lower than one levels:

interface Serial1/0
 ip address 1.1.1.1 255.255.255.252
  test line 1
 carrier-delay msec 500
interface Serial1/1
 carrier-delay msec 500

which in my opinion is the expected behavior.

mpenning commented 6 years ago

Thank you for reporting this issue... it should be fixed in version 1.3.11