Implementing idempotency in Unicon a.k.a do the desired changes already exist on the router? Part 2

This is currently the output.
First, i’m parsing the file with the changes.

Then i’m checking if the file with changes contains routers that are not in my infrastructure. If it does, i remove the changes for these routers from the modification plan (‚an error has been found and removed’)

Finally, i compare the remaining changes with existing configs of routers. If a match is found, the change doesn’t make sense.

def makedictionaryfromchangefile():
    
    #looping through list of configs and parsing the config
    with open('change.txt', 'r') as reader:
          configchangetext = reader.read()
          stext = configchangetext.split('\n')
          mydict = dict(map(lambda s: s.split(','), stext))
          Dict = { }
          for k, v in mydict.items():
            v = dict(z.split(";") for z in v.split("?"))
            Dict[k] =  {}
            y = 0
            for y in range(y,len(v)):
              eachitem = list(v.items())[y]       
              Dict[k][eachitem[0]] = eachitem[1]
    
    return Dict
def comparechangewithconfig():
    
    Dict = makedictionaryfromchangefile()
    #i need to get the list of devices for which change should made. This may be different than the list of routers that are in the environment(typos etc.)
    
    
    for i in Dict.keys():
        #print(' I am now inside the change dictionary, and i am in the main key ' + str(i) + str(Dict[i]))
        #this will just print parents 
        #for j in Dict[i].keys():
            #print(' i am printing what will be changed for ' + str(i) + ' and the changes are as follows ' + str(j))
        for l, m in Dict[i].items():
        	print('i am showing now how I want router ' + str(i) + ' config to be modified: ' + l + '  ' + m)
    realdevicelist = []
    for i in Dict.keys():
      #i add the devices to the list from dict main keys
      realdevicelist.append(i)
      for j in Dict[i].keys():
      	#i don't do anything here
        pass
    #let's now use this list of keys (R1, R2, R3, R4 in this case) and using those names let's open the configs from desktop.
    #if a config is not found, an exception is caught
    while True:
      try:
        x = 0
        #print(str(realdevicelist) + '  ..this is the list ')
        for x in range(x,len(realdevicelist)):
          with open(realdevicelist[x], 'r') as reader:
            
            config = reader.read()
            my_config_as_list = config.split("\n")
      except FileNotFoundError as e:
          print('*******************************ERROR**************************************************************************')
          print(str(e) + '.This may mean that in the change file there is a router mentioned for which i do not have a config. This error appeared in ' + realdevicelist[x])
          print('*******************************ERROR**************************************************************************')
          
          del Dict[realdevicelist[x]]
          realdevicelist.remove(realdevicelist[x])
          print('An error has been found and removed. ')
          
          
          continue
      else:
        print('After removing all errors from config file, this will be implemented. ')
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(Dict)
        break 
    #print('This is the list again ' + str(realdevicelist))   
    for i in Dict.keys():
    	#print('I am in ' + str(i))
    	with open(i, 'r') as reader:
        #i'm on R1 now   
            config = reader.read()
            my_config_as_list = config.split("\n")
            for j, k in Dict[i].items():
              #print(j + ' and its value ' + k)
              parse = CiscoConfParse(my_config_as_list)
              answer = parse.find_objects('^' + j)
              #print(answer)
              if len(answer) > 0:
                #print('a subkey from Dict matches a parent object from the config. This does not tell us anything yet. Maybe the value will be modified, maybe not') 
                pass
              second_answer = parse.find_objects_w_child(j, k)
              if len(second_answer) > 0:
                print('i am on ' + i + ' in subkey ' + j + ' in value ' + k + ' and I have found a match in the existing configuration of ' + i + ' so i am not going to implement this change')
              else:
              	print(' no match  in config of ' + i + ' found when on ' + i + ' in subkey ' + j + ' in value ' + k + ' so i will implement this change ')
                #connect to i and implement config

Unfortunately, I’ve found a bug that overrides the change value for a given interface if this interface appears twice in the change config. I need to be able to create a dictionary with change values as a list, not just with single values. This needs further research.

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s