Automation

last person joined: 21 hours ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Commit outside of for-each loop (Op Script)

    Posted 11-05-2012 11:52

    Greetings,

     

    This is my first go at a Op script, so please bear with me 🙂

     

    I'm trying to accomplish the following: copy any lldp-remote-system-name found,  into the appropriate interface description.

    Example:

     

    user@system> show lldp neighbors 
    ge-0/0/7.0     A0:B0:C0:D0:E0:F0  eth0         node2.foo.bar
    
    Would result in:
    ge-0/0/7 {
    description "node2.foo.bar"
    unit 0 {
                .......
    }

     

    Currently i have a working Op script, but the script commits on every found "lldp-remote-system-name"

     

    version 1.0;
    
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    import "../import/junos.xsl";
    
    match / {
        <op-script-results> {
    
            var $lldp = {
               <get-lldp-neighbors-information> {
                }
            }
    
            var $lldp_out = jcs:invoke($lldp);
    
            for-each ($lldp_out/lldp-neighbor-information) {
    
                var $interface_lldp = substring-before(lldp-local-port-id, ".");
                var $interface_lldp_description = lldp-remote-system-name;
    
                var $configuration-change = <configuration> {
                    <interfaces> {
                        <interface> {
                        <name>$interface_lldp;
                        <description>$interface_lldp_description;
                        }
                     }
                }
    
                var $connection = jcs:open();
    
                var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
    
                if($results//xnm:error) {
                    for-each($results//xnm:error) {
                        <output> message;
                    }
                }
    
            var $close-results = jcs:close($connection);
            }
        }
    }

     

    So the question is, how can i commit outside of the "for-each" loop and retain multiple interface configuration blocks(more than one found lldp neighbor)

     

    Anyone have so pointers to the docs, or a example at hand? 

     

    Thanks,

    Tim

     



  • 2.  RE: Commit outside of for-each loop (Op Script)
    Best Answer

    Posted 11-06-2012 06:44

    Build your config change within the for-each loop, but assign it to a single variable:

     

    var $inner-change = {

        for-each ($lldp_out/lldp-neighbor-information)

        {

            var $interface_lldp = substring-before(lldp-local-port-id, ".");

            var $interface_lldp_description = lldp-remote-system-name;

     

            <interface> {

                <name>$interface_lldp;

                <description> $interface_lldp_description;

             }

         }

    }

     

    /* Commit if there is something to commit */

    if( string-length( $inner-change ) > 0 ) {

        var $commit-change = {

            <configuration> {

                <interfaces> {

                    copy-of $inner-change;

                }

            }

        }

     

        ....do the commit stuff....

     

    }



  • 3.  RE: Commit outside of for-each loop (Op Script)

    Posted 11-06-2012 23:25

    Thank you Curtis!

     

    That did the trick.

     

    Regards,

    Tim