How to use various functions in Programmable Configlets?

Programmable Configlets enable you to dynamically generate configurations for your devices. By leveraging a combination of custom variables, loops, conditional statements, and built-in device functions, you can create flexible and reusable configurations. This guide explains how to use various functions in Programmable Configlets effectively.

  • Use #set ($variable = "variablevalue") to assign value to variables Ex:  #set( $i=1)
  • To iterate over the list of available interfaces, vlans, custom columns
    #foreach($variable in $variableList)
     ....
    #end
    $variableList - $device.interfaces(),$device.vlans(),$device.customcolumns()
  • To use conditional statements
    #if($true)
     ...
    #elseif
     ...
    #end
  • To add comments for reference,
     ## This is a single-line comment

    #*
    This is a multi-line
    comment
    *#
    Apart from default functions of velocity, below custom functions can also be used.

$device functions:

To use device details inside configlet, please refer below table for available methods


SI.NO Command Description

1

$device.ip()

 

To get device ip address

2

$device.hostname()

 

To get device hostname

3

$device.imgfile()

 

 To get image file

4

$device.macaddr()

 

To get mac address

5

$device.serialno()

 

To get serial number

6

$device.ostype()

 

To get OS type of device

7

$device.vendor()

 

To get device vendor

8

$device.protocol()

 

To get device protocol

9

$device.devicetype()

 

To get device type/personality

10

$device.template()

 

 

To get device template

11

$device.model()

 

To get device model

12

$device.series()

 

To get device series

13

$device.osversion()

 

To get the OS version of device

$util functions:


Functionalities Syntax Sample

To split a string by a delimiter

$util.split(String input, String regex)

 

$util.split("hostname,interface,ip_address",",");

Converts a string number to an integer/bigIntObject

  • $util.integer(String integerNumber)
  • $util.bigint(String integerNumber)

 

  • $util.integer("1024");
  • $util.bigint("12345678901234567890");

Extracts a portion of a string/integer/long using regex (e.g., finding the progress percentage, ErrorCode from command output).

  • $util.grep(String inputTxt, String regex, int group)
  • $util.parseInt(String inputTxt, String regex, int group)
  • $util.parseLong(String inputTxt, String regex, int group)

 

  • $util.grep("Error: Connection failed, ErrorCode: 404", "ErrorCode: (\\d+)", 1);
  • $util.parseInt("Progress: 30%", "Progress: (\\d+)%", 1);
  • $util.parseLong("Timestamp: 1609459200", "Timestamp: (\\d+)", 1);

To pause the execution of the script for a given number of seconds.
 (e.g., wait till device reboots).

$util.sleep(int seconds)

 

$util.sleep(5);

To returns the current date(using a custom or default format).

  • $util.date(String pattern)
  • $util.date() (default pattern - dd-MM-YYYY)

 

  • $util.date("yyyy-MM-dd HH:mm:ss")
  • $util.date()

$server functions:


Functionalities Syntax
To get Server Ip  $server.ip()
To get Syslog port  $server.syslogport()

Note:

  1. Operators and comparisons:

  2. Using common operators (&&, ||, ==, !=, >, <) for comparisons.

  3. Array handling:

  4. Arrays are treated as fixed-length lists, allowing list methods like:

    • $myarray.isEmpty(), $myarray.size(), $myarray.get(index), $myarray.set(index, value).
  5. String operations:

  6. Common string functions include:

    • $string.length(), $string.contains(), $string.substring(index), $string.trim(), $string.toLowerCase(), $string.toUpperCase(), $string.concat($string1).
    • Additional: $string.startsWith()