cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3670
Views
12
Helpful
4
Comments
aradford
Cisco Employee
Cisco Employee

In Part1 of this blog we went through the API calls required to use Prime Infrastructure configuration templates.  The particular example used was for interface VLAN assignment. This blog contains some python tools to use these API calls and some utilities to write your own python modules

Using the scripts in the github repository

GitHub is a cloud-based repository with a web-based interface for collaborating on projects through building, downloading and providing feedback on code.  It is freely available, so a great way to share these examples.  All of the sample scripts used in this blog are in this github repository. 

  In order to use these, you will need to modify the contents of the python script "pi-config.py".  You will need to update the values of the variables inside to match your Prime Infrastructure instance.

gitbub-post2.png

First a bit about templates

As discussed in part 1, templates are predefined command sequences in PI.  There are a number of built-in templates and you can define new ones.  A template can be applied to a device via an API call. A template has a name and an id.

Templates can be listed using the script get_template.py.  If you run it without any arguments, you will get a list of templates similar to below

$ python get_template.py

541541 Configure Interface

541542 HTTP SWIM Image Upgrade Template

541543 IOS-XE Controller - Small Network

541544 Embedded Event Manager Configuration-IOS

541545 Preprovision

541546 Mobility_Agent

541547 Delete_Custom_Template

541548 Delete_Vlan

541549 Medianet - PerfMon

  <snip>

For our example, the ID of the  “Configure Interface” template is 541541.


The template requires variables to build out the configurations. For example, to configure a vlan on a switch interface, you would need to define the InterfaceName, the VLANID etc.  These will be passed in a JSON (or XML) data structure via the REST API call. To make it easier to use the template API, the following script can be used to either show the details of the template (which will show how the template is being built), or build a python dictionary showing which variables names are required.  This can be converted to JSON and passed via a REST API call to run the template.  You could use this in a POSTMAN call or your own python script for example


  The –t <id> argument will give the full version of the template, and the –s will produce a python dictionary that can be used in a PUT REST API call to activate the template on a device.

$ ./get_template.py -t 541541 -s

{

"cliTemplateCommand": {

"targetDevices": {

      "targetDevice": {

        "targetDeviceID": "<DEVICEID>",

        "variableValues": {

          "variableValue": [

            {

              "name": "Description",

              "value": ""

            },

            {

              "name": "InterfaceName",

              "value": "required"

            },

            {

              "name": "NativeVLan",

              "value": ""

            },

            {

              "name": "StaticAccessVLan",

              "value": ""

            },

            {

              "name": "TrunkAllowedVLan",

              "value": ""

            },

            {

              "name": "VoiceVlan",

              "value": ""

            },

            {

              "name": "spd",

              "value": ""

            },

            {

              "name": "A1",

              "value": ""

            },

            {

              "name": "duplexField",

              "value": ""

            },

            {

              "name": "PortFast",

              "value": ""

            }

          ]

        }

      }

},

"templateName": "Configure Interface"

}

}


Here is an example code snippet showing how you could use the data structure above in your own code.  Assume that the variable cli_template  is assigned to the python above with your own variables for InterfaceName and StaticAccessVlan etc.

headers={"Content-Type" : "application/json"}

result = requests.put(base + "op/cliTemplateConfiguration/deployTemplateThroughJob.json", headers=headers,
                 data=json.dumps(cli_template), verify=False)
result.raise_for_status()
return result.json()

Device ID

You will notice the template requires a <DEVICEID>.  Every device in the PI inventory is referenced by a device id. It is a unique identifier.  The "get_devices.py" script will list all of the DEVICEID and IP address for devices in the inventory.

$ ./get_devices.py

Getting all devices

ID IP address

610610 192.168.3.1

610611 10.10.2.130

610612 10.10.2.41

610613 10.10.2.129

610614 10.10.2.13

610616 10.10.3.13

610618 10.10.10.147

610619 10.10.10.115

610620 10.10.7.2

610621 100.1.1.5

610622 10.10.8.100

This script can also take either an ID or an IP address to give all of the inventory details for the device.  This is useful to determine that PI has current access to the device.  The attributes highlighted are important as they determine the PI has full management access to the device.

$ ./get_devices.py --ip 10.10.8.100

{

"queryResponse": {

"@responseType": "listEntityInstances",

"@count": "1",

"@last": "0",

"@rootUrl": "https://adam-pi/webacs/api/v1/data",

"@requestUrl": "https://adam-pi/webacs/api/v1/data/Devices?.full=true&ipAddress=10.10.8.100",

"entity": [

{

"@url": "https://adam-pi/webacs/api/v1/data/Devices/610622",

"@type": "Devices",

"devicesDTO": {

"collectionTime": "2016-07-01T22:00:31.717+10:00",

"@displayName": "610622",

"softwareType": "IOS-XE",

"minorAlarms": 0,

"deviceName": "3650-dns",

"manufacturerPartNrs": {

"manufacturerPartNr": "WS-C3650-48PD-L"

},

"ipAddress": "10.10.8.100",

"informationAlarms": 0,

"creationTime": "2016-05-15T17:27:46.354+10:00",

"softwareVersion": "03.06.05E",

"collectionDetail": "<status><general code=\"SUCCESS\"/></status>",

"productFamily": "Switches and Hubs",

"reachability": "Reachable",

"majorAlarms": 0,

"criticalAlarms": 0,

"deviceType": "Cisco Catalyst 3650-48PD-E Switch",

"deviceId": 614650,

"clearedAlarms": 1,

"managementStatus": "MANAGED_AND_SYNCHRONIZED",

"@id": "610622",

"warningAlarms": 0

},

"@dtoType": "devicesDTO"

}

An example – VLAN assignment

Here is an example of a using python script to do VLAN assignment to a port using a Prime Infrastructure configuration template.  The template is called "Configure Interface" and is provided some parameters, such as the InterfaceName, StaticAccessVlan and Access mode (A1). See below for the other parameters, which can be changed.

This script combines together two core concepts that are explained earlier:

  1. 1) Configuration Templates.  To find out parameters required, use the "./get_template.py –n <templateName> –s " script.  Note you may need to put the template name in quotes if it contains spaces.
  2. 2) device ID. To find a list of devices, use the "get_device.py" script.

$ ./run_template.py -t "Configure Interface" -p '{"InterfaceName" : "g1/0/2", "A1" : "access", "StaticAccessVLan" : "8", "Description" : "API example"}' -d 10.10.8.100

Applying template:'Configure Interface' to device:10.10.8.100

An deploy job has been successfully created.

Waiting for Job:JobCliTemplateDeployIOSDevices01_55_57_204_AM_06_20_2016

user:api run:COMPLETED result:SUCCESS Start:2016-06-20T01:56:27.247+10:00

For full job history: op/jobService/runhistory.json?jobName=JobCliTemplateDeployIOSDevices01_55_57_204_AM_06_20_2016

More Examples

You can use other templates in a similar way.  The first step is to take a look at the template with "./get_template.py –[nt] <name|id> –s".  You can use either the template name or the templateId. You need the –s to get the data structure, and leave it off to get the full template and extra information.

For example, to  add a vlan, you take a look at the data structure for the template.  Both "vlanId" and "vlanName" are required variables.

$ ./get_template.py -n "Add_Vlan" -s

{

"cliTemplateCommand": {

"targetDevices": {

"targetDevice": {

"targetDeviceID": "<DEVICEID>",

"variableValues": {

"variableValue": [

{

              "name": "vlanId",

              "value": "required"

},

{

              "name": "vlanName",

              "value": "required"

}

]

}

}

},

"templateName": "Add_Vlan"

  }

}

Then use the "run_template.py" script to apply this template to a device.

$ ./run_template.py -t "Add_Vlan" -p '{"vlanId" : "333", "vlanName" : "DDDD"}' -d 10.10.8.100

Applying template:'Add_Vlan' to device:10.10.8.100

An deploy job has been successfully created.

Waiting for Job:JobCliTemplateDeployIOSDevices06_27_09_679_PM_07_02_2016

user:api run:COMPLETED result:SUCCESS Start:2016-07-02T18:27:39.742+10:00 Stop:2016-07-02T18:27:40.775+10:00

For full job history: op/jobService/runhistory.json?jobName=JobCliTemplateDeployIOSDevices06_27_09_679_PM_07_02_2016

There are lots of other template options.  For example all templates for IWAN are available, which could automate the deployment of an IWAN solution.  Take a look at the IWAN MPLS border router schema below.  This shows you the parameters needed to deploy a IWAN MPLS border router.  Note this example is using pre-shared-keys; there is also a PKI template too.

$ ./get_template.py -n CVD-DMVPN-MPLS-DC1 -s

{

"cliTemplateCommand": {

"targetDevices": {

"targetDevice": {

"targetDeviceID": "<DEVICEID>",

"variableValues": {

"variableValue": [

{

              "name": "PnPGlobal_HIDDEN",

              "value": ""

},

{

              "name": "MPLS-WAN-Bandwidth-KBPS",

              "value": "required"

},

{

              "name": "DC_Prefix",

              "value": "required"

},

{

              "name": "APIC-EM-IP-Address",

              "value": ""

},

{

              "name": "PI-IP-Address",

              "value": "required"

},

{

              "name": "Tunnel-IP",

              "value": "required"

},

{

              "name": "Master-Controller-IP",

              "value": "required"

},

{

              "name": "MPLS-WAN-IP",

              "value": "required"

},

{

              "name": "MPLS-WAN-GW-IP",

              "value": "required"

},

{

              "name": "Loopback-IP",

              "value": "required"

},

{

              "name": "pre-shared-key",

              "value": "required"

},

{

              "name": "Tunnel-Subnet",

              "value": "required"

},

{

              "name": "MPLS-WAN-Subnet-Mask",

              "value": "required"

},

{

              "name": "Tunnel-Subnet-Mask",

              "value": "required"

},

{

              "name": "Loopback-Subnet-Mask",

              "value": "required"

},

{

              "name": "MPLS-WAN-Interface",

              "value": "required"

}

]

}

}

},

"templateName": "CVD-DMVPN-MPLS-DC1"

  }

}

Wrapping up

For more examples on Prime Infrastructure API, you could visit DevNet In addition, we have a Github repository where you can get examples related to this blog.  You can use other templates quite easily using the scripts in this repository.

Thanks for reading

@adamradford123

4 Comments
choywy
Level 1
Level 1

Hi Aradfort,

Like to know is there an option to perform mass deployment of the template in your run_template.py script?

ie. Deploy the template to 50-100 of my routers? If we do it one at a time, there will be 100s of deployment job being created...

aradford
Cisco Employee
Cisco Employee

Hi,

did you want to apply the same parameters to all of the devices, or different parameters to different devices?

mmehtabu
Cisco Employee
Cisco Employee

Hi Aradfort,

sticking to Wai`s Question if the devices is all same. can we do it or is there any sample script that you can help us with?

Cheers

Mehtab

aradford
Cisco Employee
Cisco Employee

The script does not handle that natively... but it would be easy to change.  You just need to have multiple entries for each device + variables in the payload.  Do you want to have the same variables for each device?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: