Getting started with DNA-C programming

DNA-C includes many things out of the box. Semi-automation, PnP and SWIM are extremely valuable. But if you combine this with some external scripting thats when you really unleash the beast!

We had a large project going on for an enterprise customer where following issues came up as hard to solve in a good way without repetitive clicking in the GUI and a lot of manual variable inputs. I believe the “DevNet path” is a solid path forward when the cases are increasing. Some examples:

  • Claim devices
  • Assign the device to a Site (which deploys day0 template)
  • Deploy Day-1 Template
  • Configure downlinks in upstream neighbor (The template in DNAC is stateless and therefore you can’t refer to variables from neighboring devices)

All of these steps were very easy to solve with some external programming. But one more thing, what if the customer is not experienced with any programming language? The starting threshold can be a challenge, therefore I decided to write a module – ezdnac. With the intent not to be nice coding, not to be superefficeint or anything. Just easy to use and interpret for a person that just started the DevNet journey. Lets dig in to some examples!

First of all, we need to import the module and initialize an apic object. That means that we call for a class in a module which does some magic behind the scenes, while we just have to define whats really needed:

from ezdnac import apic, device
dna = apic('','admin','password')

>>>from ezdnac import apic, device
>>>dna = apic(‘’, ‘user’, ‘password’)
Initializing dnac with ip:
>>>print (dna.authToken)
eyJ0eXhbnROYW1lIjoiVE5UMC… (repsonse is shortened in the example)

What happened now is that we got ourselves an object called dna. What happened under the hood is that it made its first REST-call for authentication and stored a lot of information necessary for whats coming next. We can show this bu printing the attribute ‘authToken’.
We can also use optional input arguments for other settings, such as:

  • timeout, when setting this in the dna object, the timeout value will be used for all REST-calls using this object during the whole script runtime.
  • Port, if other than 443 is used
  • verifySSL, its turned off be default, but can be enabled by adding the argument ‘verifySSL=True’

Then we would like to initalize a switch for sure, lets initialize two switches at the same time.

switch1 = device(dna,sn='ABCDE123456')
switch2 = device(dna,id='5e29b02e97b7380008070685')

The first input attribute (in this case ‘dna’) is simply the object for the dna-c we initialized in previous step.
Second attribute is the identity of the device. Yes, we initialized the first one based on its serial number, the second one using its device Id in DNA-C inventory. You can choose whichever method suits you best depending on what you are doing. The module will try to find the device in the inventory first, if not present it will also look in PnP so you don’t have to worry about different syntax depending on the device status! Once the switch objects are created, we can try printing some of their attributes.

>>>print (switch1.hostname)
>>>print (switch1.ip)
>>>print (switch1.platform)
>>>print (switch1.softwareType)
>>>print (switch1.softwareVersion)

Last example for this post would be how easily we can provision day-N templates to our devices.

#Define a dictionary with the template variables:
template_parameters = {
“hostname”: “LAHTILABSWI2”,
“primary_radius”: “”,
“secondary_radius”: “”,
“mgmt_address”: “”

#Translate the template name to its template Id:
template_name = ‘test_template_9200’
templateId = dnac.getTemplateId(template_name)

#Deploy the template to switch1 using the dictionary for all input variables:
switch1.deployTemplate(templateId, template_parameters)

There goes the parameters to the DNA-C, all variables gets populated by our input dictionary and the DNA-C makes sure that the device is configured accordingly, all done with 20 lines of easy-to-interpret-code including comments.

If you want to do checks wether or not the template was deployed correctly, you can use the function ‘deployTemplateStatus’, it will already be populated with the last job for that device! It could become handy to pause the script with a loop until the state is ’SUCCESS’ or apply other logic based on what happened.

print (switch1.deployTemplateStatus)

How to get it? You can install ezdnac using pip. It also requires that you have requests module installed on your computer/virtual environment.

pip3 install requests
pip3 install ezdnac

Leave a Reply

Your email address will not be published. Required fields are marked *