Automation

 View Only
last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Use hostname and routing-instance as variables

    Posted 05-31-2019 06:33

    Hi all,

     

    Trying to create a configlet that will allow me push some config based on hostname and routing-instance name. So this is a 2 part configlet that I'm trying to get together.

     

    Ex:

     

    Hostname: NA-B-COMPANY-PE  or NA-C-COMPANY-RI or  NA-C-COMPANY-RR1  (I have all)

    Routing-Instance: INTERNET

     

    Basically what I need is to push config to a router if hostname contains "B" AND "INTERNET" routing-instance is present. If INTERNET is not present then it only has to push the first part of the configlet. Also if  "B" is not present then it has to push all.

     

    I defined 2 variable one for hostname and one for routing-instance which both point to their correspondant XPATH. I can get the name piece to work but not the routing-instance part.

     

    So far I did the following:

     

    ###Obtain hostname variable and routing-instance
    #set ( $Hostname = $Hostname.get(0).split("-") )
    #set ( $Instance = $Instance.get(0) )

    #if ( $Hostname.get(2) == "B" )
    groups {
        LSP {
            protocols {
                mpls {
                    delete: no-propagate-ttl;
                    }
              }
         }
    }

    #if ($Instance.get(0) == "INTERNET" )
    routing-instances {
        INTERNET {
           no-vrf-propagate-ttl;
         }
    }
    #else
    #end


    #else
    groups {
        LSP {
            protocols {
                mpls {
                    delete: no-propagate-ttl;
                    }
              }
         }
    }

    routing-instances {
        INTERNET {
           no-vrf-propagate-ttl;
         }
    #end

     I've also tried using a #foreach command to try to grab all the routing-instances names but still no luck. Any advise on this? Thanks!


    #space
    #configlets
    #Automation
    #variables


  • 2.  RE: Use hostname and routing-instance as variables
    Best Answer

     
    Posted 06-01-2019 00:37

    Hi Fer,

     

    There's some slight difference in reading routing instances for some reason, however matching for a specific instance that you need in this case can be done with Python logic.  The following works for me (check the if/else logic to suite your intended config change).

     

    from jnpr.junos import Device
    from lxml import etree
    import xml.etree.ElementTree as ET
    from jnpr.junos.utils.config import Config
    
    #Hostname: NA-B-COMPANY-PE  or NA-C-COMPANY-RI or  NA-C-COMPANY-RR1 
    #Routing-Instance: INTERNET
    
    dev = Device(host='1.1.1.1',user='lab',passwd='lab123')
    dev.open()
    Routing_Instance=dev.rpc.get_instance_information()
    Hostname=dev.rpc.get_software_information()
    
    Host_string=etree.tostring(Hostname).decode('ascii')
    Instance_string=etree.tostring(Routing_Instance).decode('ascii')
    #print (Host_string)
    #print (Instance_string)
    
    host=ET.fromstring(Host_string).find('multi-routing-engine-item/software-information/host-name')
    #instance=ET.fromstring(Instance_string)
    
    config1 = """
    groups {
        LSP {
            protocols {
                mpls {
                    delete: no-propagate-ttl;
                    }
              }
         }
    }
    routing-instances {
        INTERNET {
           no-vrf-propagate-ttl;
         }
    }
    """
    
    config2 = """
    routing-instances {
        INTERNET {
           no-vrf-propagate-ttl;
         }
    """
    
    config3 = """
    groups {
        LSP {
            protocols {
                mpls {
                    delete: no-propagate-ttl;
                    }
              }
         }
    }
    routing-instances {
        INTERNET {
           no-vrf-propagate-ttl;
         }
    }
    """
    
    if ("NA-B-COMPANY-PE" in host.text and "INTERNET" in Instance_string):
    	#print "Found company B and INTERNET"
    	with Config(dev, mode='private') as cu: 
    		cu.load(config1, format='text', merge=True)
    		cu.pdiff() 
    		cu.commit()
    		
    elif ("NA-B-COMPANY-PE" in host.text and not("INTERNET") in instance_string):
    	#print "Nope"
    	with Config(dev, mode='private') as cu: 
    		cu.load(config2, format='text', merge=True)
    		cu.pdiff() 
    		cu.commit()
    
    else:
    	with Config(dev, mode='private') as cu: 
    		cu.load(config3, format='text', merge=True)
    		cu.pdiff() 
    		cu.commit()
    		
    dev.close()

    Hope this helps.

     

    Regards,
    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated :).



  • 3.  RE: Use hostname and routing-instance as variables

    Posted 06-06-2019 06:54

    Thanks mriyaz! the logic works and can be applied here. Will modify my configlet with this next week. 

     

    Highly appreciated!