Cisco – Rest API to export configuration from an ACI

automationcisco

Is it possible to pull config from Cisco APIC controller using REST API from a remote server .

Best Answer

I don't know if it's really what you're looking for, but for science you can export APIC configuration in JSON or XML format via REST.

First of all you need to obtain authentication token by running POST request to https://{{APIC}}/api/aaaLogin.json with JSON body:

{
    "aaaUser" : {
        "attributes" : {
            "name" : "{{username}}",
            "pwd" : "{{password}}"
        }
    }
}

Verify that body type is application/json.

On Python it will look like this:

    import requests

    APIC = "10.11.12.13"
    APIC_URL = f"https://{APIC}"
    API_URL = f"{APIC_URL}/api/node/mo/"
    AUTH_URL = f"{APIC_URL}/api/aaaLogin.json"
    body = {
        "aaaUser" : {
            "attributes" : {
                "name" : "USERNAME",
                "pwd" : "PASSWORD"
            }
        }
    }

    def get_auth_token(body):
        """
        Obtain authentication cookie for future requests
        """
        func_name = get_auth_token.__name__
        log_str = f"{func_name}(): "

        headers = {'Content-Type': "application/json", 'cache-control': "no-cache"}
        result = requests.post(AUTH_URL, data=body, headers=headers, verify=False)
        if result.status_code != 200:
            print(f"{log_str}HTTP {result.status_code}: request error!")
            return

        try:
            js = result.json()
            cookie = {}
            cookie['APIC-Cookie'] = js['imdata'][0]['aaaLogin']['attributes']['token']
            return cookie
        except:
            return

Then you can do GET request to obtain uni child objects which will be configuration you want:

https://{{APIC}}/api/node/mo/uni.json?query-target=children

query-target=children can also be replaced with query-target=subtree to get even more data including existing endpoints, counters, etc...

You should include authentication token in your request:

    def query_uni(cookie):
        """
        Retrieve JSON of requested static VLAN pool
        """
        func_name = query_uni.__name__
        log_str = f"{func_name}(): "

        uri = "uni.json?query-target=children"
        result = requests.get(f"{API_URL}{uri}", cookies=cookie, verify=False)
        if result.status_code != 200:
            print(f"{log_str}HTTP {result.status_code}: request error!")
            return
        try:
            return result.json()
        except:
            return

You're can also do this with Postman or another app that helps with REST API testing/troubleshooting.

===============================

It is important to note, that configuration obtained with that method cannot be counted as valid fabric backup. For backups you should use special instruments available inside APIC GUI that create snapshots and full backups and store them on APIC or external resources such as FTP/SFTP/TFTP.