Automation

last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Commit Script

    Posted 05-09-2014 13:55

    Hi ALL

     

    I did the below scripts to check all the interfaces that have description contain DSL , if they have configured with apply-group start with MOI and to give error if this apply group not there

    But still I am not able to let this work as expected is there any error on the script ?

     

    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 configuration {

            for-each (interfaces/interface) {

                    var $desc = description;

                    var $apply = apply-groups;

                    if (contains($desc, "*-DSL-*")) {

                            if (not ($apply = "MOI-.*")) { 

                                    <xnm:error> {

                                            <message> "Error, MOI APPLY-GROUP should be configured";

                                    }

                            }

                    }

            }

    }



  • 2.  RE: Commit Script

    Posted 05-09-2014 14:12

    There might be other problems also, but this definitely needs to be fixed:

     

    if (contains($desc, "*-DSL-*")) {

                            if (not ($apply = "MOI-.*")) {

     

    Neither contains() nor not() work with regexes (or wildcards of any sort). For contains(), just remove the *'s. For the not(), use the starts-with() function. Or, you can use jcs:regex() if you really want to use regular expressions:

    This Week: Junos Automation Reference for SLAX 1.0 - Juniper Networks

     



  • 3.  RE: Commit Script

    Posted 05-09-2014 16:24

    Thanks 

    I have modified the scripts as below  , but now i have anther error 

     

    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 configuration {  
            for-each (interfaces/interface) {
                    var $desc = description;
                    var $apply = apply-groups;
                    var $DSL = ".*-DSL-.*";
                    var $MOI = "^MOI.*";
                    if ($desc = ($DSL)) {
                            if (not($apply =jcs:regex($MOI))) {
                                    <xnm:error> {
                                           <message> "Error, MOI APPLY-GROUP should be configured";
                                    }
                            }
                    }
            }
    }

     

    the error that i am facing

     

    re0:
    error: Invalid number of arguments
    error: xmlXPathCompiledEval: 2 objects left on the stack.
    error: 2 errors reported by commit scripts
    error: commit script failure



  • 4.  RE: Commit Script

    Posted 05-09-2014 16:39

    jcs:regex() requires two arguments. Take a look at the function's details on page 179 of the link in my last post.

     

    BTW, I don't think the apply-groups statement will be present in the configuration that your commit script receives since it is getting the post-inheritance version. You'll be able to see what items were added/modified through the group though by searching for the junos:group attribute. (If junos:group doesn't work, then try just group, I don't remember if the namespace is stripped or not.) To see what I mean, take a look at your configuration like this:

     

    show configuration | display xml groups | display inheritance



  • 5.  RE: Commit Script
    Best Answer

     
    Posted 05-10-2014 05:09
            for-each (interfaces/interface[@junos:changed and contains(description,"DSL") and not(starts-with(.//@junos:group,"MOI"))]) {
    	        message "Error, MOI APPLY-GROUP should be configured on interface " _ name _ ", desc " _ description ;
            }
    

     This example picks changed interfaces of which description contains "DSL" and don't have group name "MOI*".

     

    /Charlie

     



  • 6.  RE: Commit Script

    Posted 05-10-2014 06:02

    Thanks Ckim  it's working as expected 

    but i need explination for use of @junos:changed and why u use the "@" the same case for .//@junos:group and is the ".//" refer to any Path ?

     

    Thanks once again 



  • 7.  RE: Commit Script

     
    Posted 05-10-2014 06:15

    Below is an example of commit-script-input.

    <interface junos:changed="changed">
    <name>ge-0/0/25</name>
    <description junos:changed="changed">123-DSL-128</description>
    <unit junos:changed="changed">
    <name>0</name>
    <description junos:group="MOJ-123" junos:changed="changed">moj-123</description>
    <family>
    <ethernet-switching>
    </ethernet-switching>
    </family>
    </unit>
    </interface>

     When some change attempted, attribute junos:changed gets flagged on those being changed interfaces. By specifying @junos:changed, it checks only those changed interfaces and skip others (which can be hundreds in cases).

     

    Same goes for not(starts-with(.//@junos:group,"MOI")). It is checking if any group (junos:grou) was applied of which name starts with "MOI" under the interface tree, and skip it was.

     

    /Charlie

     

     

     



  • 8.  RE: Commit Script

     
    Posted 05-10-2014 06:18

    and, .// means all the elements under it. Refer to http://www.w3schools.com/xpath/xpath_syntax.asp.

     



  • 9.  RE: Commit Script

     
    Posted 05-12-2014 13:22

    Below is the final version of the code. When there are multiply apply-groups in an interface, previous version will only check the first appearance of it. This version checks all the groups applied.

     

            for-each (interfaces/interface[@junos:changed and contains(description,"DSL") and not(.//@junos:group[starts-with(.,"MOI")])]) {
                message "Error, MOI APPLY-GROUP should be configured on interface " _ name _ ", desc " _ description ;
            }

    Thanks,

    /Charlie