0

Cisco ACI – How to connect to the APIC API and get data

In this guide I will show Python code that will use HTTP GET requests on the Cisco APIC / ACI system and pull data. This will let you get information from the APIC, and display it in a better way. There are many things missing from the GUI (mostly on the reporting front) that you can create with some basic Python scripting.

There are two formatting types to get data in and out of the APIC: JSON and XML. I will show both of them in this guide so you can decide which one works better in your environment. From a programmers perspective, I like JSON because it is basically a Dict in Python (which is nice for processing). With XML you need to convert it into a usable format using the xmltodict library.

Note: you will need to have pip available to install some modules if your system doesn’t have these by default. Install them with:

I have broken this up into 3 different files. I use the from __future__ import so that this will work with python2 and python3. These should be ran from the command line like so:

Files:

  1. apic_token.py – this will login to your apic, you won’t run this directly
  2. json_get.py – use a http GET request to get some JSON data
  3. xml_get.py – use a http GET request to get some XML data

 

File 1: apic_token.py – before we can get any data from the APIC, we have to authenticate and get a token. This token is then sent with each request and it identifies us.

 

File 2 – json_get.py – here we use the token and query the APIC for some information. In this case we are asking for multiple lists of items. We will print out the Tenants, VRF’s, BD’s ( bridge domains), Fabric Pods, and all the devices (called nodes in ACI speak). This is using an HTTP GET request for JSON data.

 

File 3 – xml_get.py, this does the same thing but requests XML instead. We then convert the response into a python Dict (ordered Dict specifically).

 

Leave a Reply

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