用于从 ACI 导出配置的 Rest API

网络工程 思科 自动化
2021-07-31 09:12:04

是否可以使用远程服务器的 REST API 从 Cisco APIC 控制器中提取配置。

2个回答

我不知道这是否真的是您要找的,但对于科学,您可以通过 REST 以 JSON 或 XML 格式导出 APIC 配置。

首先,您需要通过https://{{APIC}}/api/aaaLogin.json使用 JSON 正文运行 POST 请求来获取身份验证令牌

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

验证主体类型为application/json

在 Python 上,它看起来像这样:

    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

然后你可以做 GET 请求来获取uni你想要的配置的子对象:

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

query-target=children也可以替换query-target=subtree为获取更多数据,包括现有端点、计数器等...

您应该在请求中包含身份验证令牌:

    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

您也可以使用Postman或其他有助于 REST API 测试/故障排除的应用程序来执行此操作

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

需要注意的是,通过该方法获得的配置不能算作有效的结构备份。对于备份,您应该使用 APIC GUI 中可用的特殊工具来创建快照和完整备份,并将它们存储在 APIC 或外部资源(如 FTP/SFTP/TFTP)上。

我使用 query-target=children 或 query-target=subtree 尝试了前面描述的方法;我有一个 json 或 xml 文件,但所有对象都设置在相同的缩进级别,因此,没有显示子树层次结构:例如,所有租户都显示在里面没有他们的 EPG,所有 EPG 都没有显示它们的静态端口,也没有域,...

我通过 APIC bash 中的以下 icurl 命令进行了此测试:icurl -o /tmp/Extract-Full-uni-191023-V4.json -X GET ' http://localhost:7777/api/node/mo/uni。 json?query-target=children '

我还尝试了这两种其他形式:

icurl -o /tmp/Extract-Full-uni-191023-V2.json -X GET ' http://localhost:7777/api/node/mo/uni.json?query-target=subtree&rsp​​-prop-include=config -只有'

icurl -o /tmp/Extract-Full-uni-191023-V3.json -X GET ' http://localhost:7777/api/node/mo/uni.json?query-target=subtree '

这三种形式都没有给我对象的真正层次结构

有关信息,我经常使用以下命令通过 APIC bash 下的 icurl 提取所有租户和 uni/infa,并且它运行良好;

1) 收集所有租户:

icurl -o /tmp/Extract-All-Tenants-181019-V1.json -X GET ' http://localhost:7777/api/class/fvTenant.json?rsp-subtree=full&rsp-prop-include=config-only '

2) 收集 uni/infra(主要是 FAP):

icurl -o /tmp/Extract-All-FAPs-181019-V1.json -X GET ' http://localhost:7777/api/node/mo/uni/infra.json?rsp-subtree=full&rsp-prop-include = 仅配置'

我还使用“请求”Python 包通过 Python 脚本获得了相同的结果:下面的简短摘录:

allTenants = session.get(APIC_URL + 'class/fvTenant.json?rsp-subtree=full&rsp-prop-include=config-only')
ALL_TENANTS_JSON_STR = allTenants.text

UniInfra = session.get(APIC_URL + 'node/mo/uni/infra.json?rsp-subtree=full&rsp-prop-include=config-only')
UNI_INFRA_JSON_STR = UniInfra.text

获取租户类和 uni/infra 对象的唯一问题如上所示,是我错过了 APIC MO 配置的一些有用部分,例如域(物理、L2ext、L3ext)和许多其他内容

如果我在 GETting node/mo/uni.json 时尝试使用相同的选项“rsp-subtree=full&rsp-prop-include=config-only”,我会在结果 JSON 或 XML 文件中收到以下错误:“代码”: "400","text":"请求失败,'polUni' 类不支持 rsp-subtree 选项"

任何想法来解决这个问题?

我知道我可以通过 APIC GUI 获取我想要的 JOSN 或 XML 文件:ADMIN ==> 导入/导出菜单:生成的 tar.gz 文件在根目录中包含良好的 XML/JSON 文件,但我想得到它通过 Python 脚本来分析这个配置文件;

提前致谢