Azure Function - 触发包含 Azure CLI 命令的 Python 脚本

Azure Function - trigger Python script containing Azure CLI commands

我有一个 python 脚本用于在 Azure - IaC 中配置基础设施。此脚本主要使用 Python SDK,但也 运行 多个 Azure CLI 命令 - 当我在 Python SDK 中找不到等效命令时,有时需要它。

我的目标是使用 Azure Functions 按需触发此脚本。在本地测试 Azure Function 时,一切正常,因为我在我的机器上安装了 Azure CLI,但是,当我将它发布到 Azure Functions 时,我会 运行 出现错误:/bin/sh: 1: az: not found

下面是我在 Azure 函数中触发的示例 python 函数(请注意脚本的其余部分工作正常,因此我可以创建 RG、SQL 服务器等,问题只是az 命令)。我想知道,是否以及如何在 Azure Function 上安装 Azure CLI 以便能够 运行 CLI 命令?

这是导致错误的 python 函数:

    # Loging to AZ
    call("az login --service-principal -u '%s' -p '%s' --tenant '%s'" % (client_id, client_secret, tenant_id), shell=True)
    b2c_id = check_output("az resource show -g '<rg_name>' -n '<b2c_name>' --resource-type 'Microsoft.AzureActiveDirectory/b2cDirectories' --query id --output tsv", shell=True)
    print("The B2C ID is: %s" % b2c_id)```

我尝试使用 HttpTrigger 创建一个简单的 Azure Function 以通过不同方式调用 Azure CLI 工具,但发布后从未在云上运行。

似乎唯一的解决方案是在将所需的包 azure-cli 添加到 requirements.txt 文件,如下图错误所示,

There was an error restoring dependencies.ERROR: cannot install antlr4-python3-runtime-4.7.2 dependency: binary dependencies without wheels are not supported. Use the --build-native-deps option to automatically build and configure the dependencies using a Docker container. More information at https://aka.ms/func-python-publish

由于我本地没有docker工具,所以没有成功运行func azure functionapp publish <your function app name> --build-native-deps.

同时,运行使用 Azure CLI 命令并不是使用 Azure CLI 功能的唯一方法。 az命令只是一个运行nable脚本文件,不是二进制执行文件。在我查看了 azazure-cli 包的一些源代码之后,我认为您可以直接通过 from azure.cli.core import get_default_cli 导入包并使用它来执行与下面代码相同的操作。

from azure.cli.core import get_default_cli

az_cli = get_default_cli()
exit_code = az_cli.invoke(args)
sys.exit(exit_code)

代码是参考azure-cli包的azure/cli/__main__.py源码编写的,在你的虚拟环境lib/python3.x/site-packages路径下可以看到。

希望对您有所帮助。

谢谢,彼得,我最后使用了类似的东西并让它工作。我有这个功能,如果我们需要,它将 运行 AZ CLI 命令和 return 结果(例如,如果我需要 运行 cli 命令但也存储输出,例如如果我想知道服务主体的对象 ID 是什么,我可以获得如下示例中的结果:

def az_cli (args):
    cli = get_default_cli()
    cli.invoke(args)
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
    return True

现在,我可以这样调用(client_id 是 ServicePrincipal ID):

ob_id = az_cli(['ad', 'sp', 'show', '--id', client_id])
print(ob_id["objectId"])