- Got a query regarding the Cisco Prime Infrastructure API documentation hosted under URL: https://developer.cisco.com/site/prime-infrastructure/documents/api-reference/rest-api-v3-0/. Using ReST apis mentioned in the above stated url to fetch inventory data from the Prime Infrastructure (PI) without using the GUI provided from the PI. Similarly want to create VLAN on a switch using ReST api calls without using GUI.
- The query is that can we create VLAN from ReST api calls? If so, is it through Cli templates.
- According to the documentation mentioned above, it is said that we can download Cli templates for Configure vlan from the URL mentioned under "Get Cli Templates" section of the documentation and it is also said that we can deploy templates through jobs. Therefore, followed the documentation and downloaded Cli template for Configure vlan and deployed it through job using the urls mentioned in the documentation, Now getting an error message saying "Invalid variable values" and the vlan is not being created.
Here are some examples for you.
There is also a little utility to create (JSON) schema for the template you want to use
- when tried to execute "get_template.py" script you had shared in GitHub.
$ python get_template.py -t 445464 -s
Traceback (most recent call last):
File "get_template.py", line 69, in <module>
show_template(args.t, args.s)
File "get_template.py", line 59, in show_template
show_a_template(template, schema)
File "get_template.py", line 47, in show_a_template
result = requests.get(BASE + "data/CliTemplate/%s.json?.full=true" % template, verify=False)
You need to post all variables in a template (even if they're not used) when you submit the job.
Also if you look in the "pnp_tempate.log" file you'll see which variable is causing the error.
The log can be accessed from the console shell (/opt/CSCOlumos/logs/ifm_template.log)
- Thought, only those fields/variables which are marked "Required" were supposed to be given in the request body while submitting the job.
- This is the error line I am able to see in the log file ifm_template.log .
[2016-06-07 10:30:55,008] [seqtaskexecutor-1] [service] [ERROR] - Thread Id : [213,330] : IFM_TEMPLATE_ERROR_DETAILS: [Unable to find value for variable: option] : IFM_TEMPLATE_ERROR: [Method getVarValue: Variable name- option does not exist!]
- Have included all variables available in the body even if it is not mandatory. But still facing the same issue. It says invalid variable values.
Are you using JSON or XML.
If you are using JSON, if you goto the REPO as mentioned earlier have written a little utility to generate a JSON schema from the CLI_template. You just need to fill in the blanks. Make sure you add a valid DEVICEID
python get_template.py -n "Configure Interface" -s
{
"cliTemplateCommand" : {
"targetDevices" : {
"targetDevice" : {
"targetDeviceID" : "<DEVICEID>",
"variableValues" : {
"variableValue": [
{ "name": "Description", "value" : None }
#required
{ "name": "InterfaceName", "value" : None }
{ "name": "NativeVLan", "value" : None }
{ "name": "StaticAccessVLan", "value" : None }
{ "name": "TrunkAllowedVLan", "value" : None }
{ "name": "VoiceVlan", "value" : None }
{ "name": "spd", "value" : None }
{ "name": "A1", "value" : None }
{ "name": "duplexField", "value" : None}
{ "name": "PortFast", "value" : None }
]
}
}
},
"templateName" : "Configure Interface"
}
}
Have changed the script to just include "" for the vars that are not required.
It displays "required" as the value for the variables that are mandatory.
Posted a fully worked example on github.
- Have solved the access issue. Now Job is not throwing any error, Job's run history says success, but how to check whether the vlan is created or not. Following is the JSON body that is been used .
{
"cliTemplateCommand": {
"targetDevices": {
"targetDevice": {
"targetDeviceID": "074084",
"variableValues": {
"variableValue": [
{
"name": "encapsulation",
"value": ""
},
{
"name": "interfaceName",
"value": ""
},
{
"name": "mtu",
"value": ""
},
{
"name": "vlanid",
"value": "244"
},
{
"name": "vlanName",
"value": "vlan199"
},
{
"name": "mode",
"value": ""
},
{
"name": "option",
"value": ""
}
]
}
}
},
"templateName": "Configure VLAN"
}
}
This particular template is to apply an VLAN to an interface. You need to specify an "name": "interfaceName", on the device.
For example "gig1/0/3" would apply vlan #244 to int gig1/0/3.
{ "name": "interfaceName",
"value" : "gig1/0/3"
}
If you just want to create a vlan on a device, there is another template for that. You can get a list of templates using the tools in the github repo i mentioned earlier.
Here is the schema for "create_vlan"
$ python 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"
}
}
- Have tried the above specified JSON but "Add_Vlan" template does not exist in my instance of Cisco Prime.
using PI 3.1
However, if you want to assign the vlan to a switch port and the VLAN does not exist, the switch will create it for you.
3650-dns(config-if)#switchport access vlan 222
% Access VLAN does not exist. Creating vlan 222
3650-dns#show vlan
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
222 VLAN0222 active Gi1/0/10
If you look at the definition of the template ( you can use $ python get_template.py -n "Configure VLAN")
Option can be create or delete. You should set "Create"
{
"name": "option",
"value": "Create"
}
{
"displayLabel": "Operation",
"description": "Create,Delete",
"defaultValue": "Create,Delete",
"required": false,
"type": "Dropdown",
"name": "option"
}
If there is an issue, take a look at the detailed job history.
first you get the job name after you create job above. Assume you have done the first part (got the job name)?
jobname = result.json()['mgmtResponse']['cliTemplateCommandJobResult']['jobName']
Then you monitor the job name until it completes
url = BASE + 'data/JobSummary.json?jobName=%s' %jobname
jobresult = requests.get(url, verify=False)
status = jobresult['queryResponse']['entityId'][0]['@displayName'].split(",")[-1]
# Status will be != "SCHEDULED" when job completes
Then you need to get the job number.
jobnumber = jobresult['queryResponse']['entityId'][0]['$']
from the job number, you can get the full information about the job.
url = base + 'data/JobSummary/%s.json' %jobnumber
jobdeteailresult = requests.get(url, verify=False)
print jobdetailresult.json()
That should show you something like the following:
{
"mgmtResponse": {
"@responseType": "operation",
"job": {
"description": "Cli Template Deploy for IOS devices",
"runInstances": {
"runInstance": {
"runStatus": "COMPLETED",
"results": {
"result": [
{
"property": "JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016",
"value": "1/1 template configurations successfully applied."
},
{
"property": 610622,
"value": "configlet:\n interface gigabitethernet1/0/2\n description Fred1\n switchport mode access\n switchport ACCess vlan 8 \n \n \n \n \n \n \n \n exit\n\n\n\n\n\n\n\n\n\n\n response:\n<cliChunks><cliChunk><cli>terminal width 0\nconfig t\ninterface gigabitethernet1/0/2\ndescription Fred1\nswitchport mode access\nswitchport ACCess vlan 8\nexit\n</cli><response><![CDATA[terminal width 0\n3650-dns#config t\nEnter configuration commands, one per line. End with CNTL/Z.\n3650-dns(config)#interface gigabitethernet1/0/2\n3650-dns(config-if)#description Fred1\n3650-dns(config-if)#switchport mode access\n3650-dns(config-if)#switchport ACCess vlan 8\n3650-dns(config-if)#exit\n3650-dns(config)#]]></response></cliChunk></cliChunks>"
}
]
},
"lastStartTime": "2016-06-27T20:40:39.565+10:00",
"completionTime": "2016-06-27T20:40:40.588+10:00",
"runId": 6365409,
"resultStatus": "SUCCESS"
}
},
"jobStatus": "COMPLETED",
"jobName": "JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016",
"jobId": 560879,
"jobType": "CliTemplateDeployIOSDevices"
},
"@rootUrl": "https://adam-pi/webacs/api/v1/op",
"@requestUrl": "https://adam-pi/webacs/api/v1/op/jobService/runhistory?jobName=JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016"
}
}
- Querying both Job Summary as well as full Run History that can give full run history of the job deployed. This will tell you the commands that were run and if they were successful.
- It says error in running some XDE procedure, is this a problem with the switch using. The run history was telling that job completed successfully.Not able to see the vlan in the console also.
<job>
<jobId>49695729</jobId>
<jobStatus>COMPLETED</jobStatus>
<jobType>CliTemplateDeployIOSDevices</jobType>
</job>
This indicates that PI was not able to log in to the device via ssh.
Can you check the status of the device.
GET https://pi-server/webacs/api/v1/data/Devices?.full=true&ipAddress=10.10.8.100 Where pi-server is your Prime Infrastructure.
You should see something like this.
{
"queryResponse": {
"@last": "0",
"@first": "0",
"@count": "1",
"@type": "Devices",
"@responseType": "listEntityInstances",
"@requestUrl": "https://adam-pi/webacs/api/v1/data/Devices?.full=true&ipAddress=10.10.8.100",
"@rootUrl": "https://adam-pi/webacs/api/v1/data",
"entity": [
{
"@dtoType": "devicesDTO",
"@type": "Devices",
"@url": "https://adam-pi/webacs/api/v1/data/Devices/610622",
"devicesDTO": {
"@displayName": "610622",
"@id": "610622",
"clearedAlarms": 1,
"collectionDetail": "<status><general code=\"SUCCESS\"/></status>",
"collectionTime": "2016-06-28T22:00:35.708+10:00",
"creationTime": "2016-05-15T17:27:46.354+10:00",
"criticalAlarms": 0,
"deviceId": 614650,
"deviceName": "3650-dns",
"deviceType": "Cisco Catalyst 36xx stack-able ethernet switch",
"informationAlarms": 0,
"ipAddress": "10.10.8.100",
"majorAlarms": 0,
"managementStatus": "MANAGED_AND_SYNCHRONIZED",
"manufacturerPartNrs": {
"manufacturerPartNr": "WS-C3650-48PD-L"
},
"minorAlarms": 0,
"productFamily": "Switches and Hubs",
"reachability": "Reachable",
"softwareType": "IOS-XE",
"softwareVersion": "03.07.04E",
"warningAlarms": 0
}
}
]
}
}
Got another similar issue: "Invalid var Values for given template's variables"
Been stuck on this for days. Nothing really shows the error in "ifm_jobscheduler.log"
There is no "pnp_template.log" available.
Got two variables "interfaceName" and 'vlan_id"
The job runs OK from within Prime.
TEMPLATE:
#if ($vlan_id !="")
#if ($interfaceName !="")
interface $interfaceName
switchport access vlan $vlan_id
switchport mode access
switchport port-security maximum 2
switchport port-security
switchport port-security aging time 1
switchport port-security violation restrict
switchport port-security aging type inactivity
ip arp inspection limit rate 100
no logging event link-status
ip dhcp snooping limit rate 100
#end
exit
#end
RUBY PUT:
RestClient.put "https://x.x.x.x/webacs/api/v1/op/cliTemplateConfiguration/deployTemplateThroughJob",
"{\"cliTemplateCommand\":
{\"targetDevices\":
{\"targetDevice\":
{\"targetDeviceID\":\"485830346\",
\"variableValues\":{\"variableValue\":[
{\"name\":\"interfaceName\",\"value\":\"GigabitEthernet1/0/17\"},
{\"name\":\"vlan_id\",\"value\":\"4\"}
]
}
}
},\"templateName\":\"vm_Student_Data\"
}
}",
"Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"243", "Content-Type"=>"application/json"
RESPONSES:
{"mgmtResponse"=>
{"@responseType"=>"operation", "@requestUrl"=>"https://x.x.x.x/webacs/api/v1/op/cliTemplateConfiguration/deployTemplateThroughJob",
"@rootUrl"=>"https://x.x.x.x/webacs/api/v1/op",
"cliTemplateCommandJobResult"=>{
"jobName"=>"JobCliTemplateDeployIOSDevices09_18_43_639_PM_07_03_2016",
"message"=>"An deploy job has been successfully created. "
}
}
}
"mgmtResponse": {
"@responseType": "operation",
"@requestUrl": "https://x.x.x.x/webacs/api/v1/op/jobService/runhistory?jobName=JobCliTemplateDeployIOSDevices11_16_16_707_PM_07_01_2016",
"@rootUrl": "https://x.x.x.x/webacs/api/v1/op",
"job": {
"description": "Cli Template Deploy for IOS devices",
"jobId": 627613049,
"jobName": "JobCliTemplateDeployIOSDevices11_16_16_707_PM_07_01_2016",
"jobStatus": "COMPLETED",
"jobType": "CliTemplateDeployIOSDevices",
"runInstances": {
"runInstance": {
"completionTime": "2016-07-01T23:16:49.190+08:00",
"lastStartTime": "2016-07-01T23:16:46.758+08:00",
"resultStatus": "FAILURE",
"results": {
"result": [
{
"property": 485830346,
"value": "Invalid var Values for given template's variables"
},
{
"property": "JobCliTemplateDeployIOSDevices11_16_16_707_PM_07_01_2016",
"value": "0/1 template configurations successfully applied."
}
]
},
"runId": 782951285,
"runStatus": "COMPLETED"
}
}
}
}
}
Can you post "vm_Student_Data" template contents for reference. Have you deployed this template already in prime
After testing everything else it was a typo in the 'targetDeviceID'. It's always the simple things.
Comments
0 comments
Please sign in to leave a comment.