fbpx

Using Superior Velocity Templates within Cisco DNA Middle – Part 4

The previous blog in this collection protected embedding interactive and exec levels commands into templates.   This website targets passing in structured information to templates, something relevant to API calls mainly.

If a refresher is necessary by you on the template-programmer API, or perhaps a sample python script to utilize them, it is possible to check out this website post.  In addition, it contains a connect to a github repository with the sample program code.

Sending Variables in order to a template.

The UI is really a little restrictive since it will not allow JSON payloads to be sent.  The API will, and the template device shows how it operates.  First, checklist the contents of the template (called “int-desc” in the folder “adam”).  Your body is simple pretty, just two variables, the script also explicitly calls those out.  As that is educational code, the API is showed because of it calls needed.  The initial API call searches for the template (you cannot visit a template by title), and the next get the newest version of it (remember templates are versioned).  In this full case, version 7 may be the latest.  It exhibits your body of the template after that, and the parameters required (if any).  The parameters are formatted in that real way, which can be cut & pasted being an argument to the script within the next step.

$ ./template.py --template adam/int-desc
Searching for: adam/int-desc
https://dnac:443/dna/intent/api/v1/template-programmer/template
TemplateId: 019eaa38-a62e-4a12-adf6-866696d69e8f Version: 7 

https://dnac:443/dna/intent/api/v1/template-programmer/template/019eaa38-a62e-4a12-adf6-866696d69e8f
Showing Template Body:
interface $int
description $desc:AUTO


Necessary Parameters for template body system: "int":"","desc":""

Bindings []

To contact the template API, you should send the JSON dictionary of parameters. Note the JSON will be in single quotes, that is to safeguard the double quotations “” which are usually mandatory for JSON. In this illustration, the ideals for “int” and “desc” are given simply because a JSON payload.

$ ./template.py --template adam/int-desc --device 10.10.15.200 --params '"int":"g2/0/10","desc":"Example"'

Typed Data

JSON payloads have varieties, strings, booleans and integers.  It could be vital that you ensure the payload will be passed utilizing the appropriate type.

Consider the adhering to template.  If the “interfaceMax” is defined to 3, g2/0/11-13 will undoubtedly be looped through then.  This is useful for stack members often, however the example uses interface amount for illustrative purposes.

#foreach ($intf in [1..$interfaceMax])
interface range gig 2/0/1$intf
description ADAM
#end

If this template is named as follows, nothing shall happen.

./template.py --template adam/int-loop --device 10.10.15.200 --params '"interfaceMax" : "3"'

A subtle change shall deliver the proper result. The difference is that “3” is really a string, while 3 can be an integer.  The loop shall feel the values 1,2,3.

./template.py --template adam/int-loop --device 10.10.15.200 --params '"interfaceMax" : 3'

Structured Data

The example only allows one description and interface, how about turning in a summary of them?  This is simple really.  Think about the following template.  $pairs is really a list, and each element has two attributes, and interface ($pair.int) and a description ($pair.desc).

#foreach ($pair in $pairs)
interf $pair.int
desc $pair.desc
#end

To execute this template, supply the following payload. This may appear complex, but is fairly simple (remember that is structured data, so likely machine generated).  A summary of int/desc pairs is provided.

./template.py --template adam/json_input  --device 10.10.15.200 --params '"pairs" : ["int":"g2/0/10", "desc" : "one","int":"g2/0/11", "desc":"two"]' 

A simple tip would be to put the json payload right into a JSON validation engine(you can also use python).

$ python
Python 3.7.6 (default, Dec 30 2019, 19:38:36) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" to find out more.
>>> import json
>>> print(json.dumps(json.loads('"pairs" : ["int":"g2/0/10", "desc" : "one","int":"g2/0/11", "desc":"two"]'), indent=2))

  "pairs": [
    
      "int": "g2/0/10",
      "desc": "one"
    ,
    
      "int": "g2/0/11",
      "desc": "two"
    
  ]
 

You’re allowed by another example to push config snippets to a couple of interfaces. Like the example above quite, except it requires a string of config (instead of just a description.

#foreach ($pair in $pairs)
int $pair.int
$pair.config
#end

And it could be run the following.  This can push the mandatory lines of config to each interface..

./template.py --template adam/json_int_config  --device 10.10.15.200 --params '"pairs" : ["int":"g2/0/10", "config" : "switchport mode accessnswitchport access vlan 100","int":"g2/0/11", "config":" switchport mode accessnswitchport access vlan 101"]'

Using Macros

While the previous is quite flexible, allowing any config for just about any interface, it generally does not promote standardization.  The next template uses velocity macros for standard port configurations.  A macro can be used for “userport” also it takes a disagreement “$vlan”. This can be a private variable.  The “apport” macro takes two arguments.   The name of the macro is passed in (as a parameter) .  The “#evaluate” command must run the macro.

#macro  (userport $vlan)
description User Port
switchport access vlan $vlan
switchport mode access
switchport voice vlan 20
switchport host
#end

#macro (apport $desc $vlanrange)
 description $desc
 switchport trunk allowed vlan 10,$vlanrange
 switchport trunk native vlan 10
 switchport mode trunk
 spanning-tree portfast trunk
 ip arp inspection limit none
 cdp enable
#end

#foreach ($config in $configs)
interface $config.int
#evaluate($config.macro_)
#end

The macro apport requires strings as arguments, therefore the quotes have to be escaped.

./template.py --template adam/macro_loop_json  --device 10.10.15.200 --params '"configs" : ["int":"g2/0/10", "macro_" : "#userport(100)","int":"g2/0/11", "macro_":"#apport("link_AP1", "100,101")"]'

What Next?

This blog shows types of passing in structured data.   Don’t be overwhelmed by the perceived complexity of the payloads in these examples.  Remember, these will undoubtedly be generated programatically, and provided as input to the API call.  The examples certainly are a simple illustration of the concepts just.

Another blog shall go through the UI and how multiple templates could be applied to a niche site.

If you are thinking about using the API to use templates to devices, this blog post includes a sample python tool for applying templates.

In the meantime, if you want for more information about Cisco DNA Center you can visit  Cisco Devnet.

Thanks for reading

@adamradford123

What Next?

This blog shows types of passing in structured data.    Another blog can look at the UI and how multiple templates could be applied to a niche site.

If you are thinking about using the API to use templates to devices, this blog post includes a sample python tool for applying templates.

In the meantime, if you want for more information about Cisco DNA Center you can visit  Cisco Devnet.

Thanks for reading

@adamradford123

Related resources:

The post Using Advanced Velocity Templates in Cisco DNA Center – Part 4 appeared first on Cisco Blogs.

%d bloggers like this: