登录到 azure cli 以获取服务主体

Login into azur cli for a service principal

我正在尝试让我的 ansible 脚本通过 azure cli 登录到 azure。由于某些原因,我不允许使用 ansible azure 包。我必须使用 shell 并从那里直接调用命令。

总的来说,我对 Azure 还比较陌生,所以所有这些租户、服务主体等仍然是我没有完全掌握的概念。

我一直在检查 official the documentation。我已经为它创建了一个应用程序注册(名为 ansible_test)。我得到了我需要的一切,包括秘密。然后我这样调用命令:

az login --service-principal -u $AZURE_SERVICE_PRINCIPAL_NAME -p $AZURE_SECRET --tenant $AZURE_TENANT

其中:

$AZURE_SERVICE_PRINCIPAL_NAME = ansible_test
$AZURE_SECRET = ${The one that I've defined via Certificates & secrets section in the app registration}
$AZURE_TENANT = ${The azure tenant that I find in the app registration}

我遇到错误:

Get Token request returned http error: 400 and server response: {"error":"unauthorized_client","error_description":"AADSTS700016: Application with identifier 'ansible_test' was not found in the directory '${AZURE_TENANT}(Blurred because I'm not sure this is something secret or not)'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.

据我了解,我找错租客了。但我得到的是我从应用程序注册中得到的确切信息。一段时间以来,我一直在用头撞墙。我已经尝试了很多其他的东西,但似乎没有用。

在这张图片中,我试图表明我确实创建了应用程序注册(我的理解是它是一个服务主体)。由于不知道它们是否是私有的,我已经模糊了这些 ID。

我做错了什么?我真的无法理解错误的来源...

服务主体的用户名是其应用程序(客户端)ID,因此您需要使用它而不是应用程序名称。

它在幕后使用客户端凭据流来获取令牌,这需要客户端 ID、租户 ID + 客户端 secret/client 证书进行身份验证。