我使用我的帐户"emj“登录,并在Jenkins上执行以下Azure CLI
az account show
az login --service-principal -u <<UserName>> -p <<Password>> -t <tenantID>>
az account set -s <<Subscription-123>>
az account show下面是输出
它按预期显示了用户帐户。
+ az account show
{
"environmentName": "AzureCloud",
"homeTenantId": "tenantID",
"id": "Subscription-123-ID",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-123",
"state": "Enabled",
"tenantId": "tenantID",
"user": {
"name": "emj@demo.com",
"type": "user"
}
}作为服务主体登录
[Pipeline] sh
+ az login --service-principal -u <<UserName>> -p <<Password>> -t <tenantID>>
[
{
"cloudName": "AzureCloud",
"homeTenantId": "tenantID",
"id": "Subscription-456-ID",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-456",
"state": "Enabled",
"tenantId": "tenantID",
"user": {
"name": "servicePrincipalID",
"type": "servicePrincipal"
}
},
{
"cloudName": "AzureCloud",
"homeTenantId": "tenantID",
"id": "Subscription-789-ID",
"isDefault": false,
"managedByTenants": [
{
"tenantId": "tenantID"
}
],
"name": "Subscription-789",
"state": "Enabled",
"tenantId": "tenantID",
"user": {
"name": "servicePrincipalID",
"type": "servicePrincipal"
}
}
]并设置所需的订阅。
[Pipeline] sh
+ az account set -s <<Subscription-456-ID>>当我期望显示服务主体时,它显示了用户帐户。
[Pipeline] sh
+ az account show
{
"environmentName": "AzureCloud",
"homeTenantId": "tenantID",
"id": "Subscription-456-ID",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-456",
"state": "Enabled",
"tenantId": "tenantID",
"user": {
"name": "emj@demo.com",
"type": "user"
}
}注意:如果不使用"az account set -s <>“设置订阅,则显示服务主体帐户。
为什么它显示用户帐户而不是服务主体?“az登录-服务-主体-u <> -p <> -t >”的含义是什么,因为它没有被考虑在内?
发布于 2021-04-27 06:45:37
嗯,不知道为什么会发生这种情况,但是要解决这个问题,您可以在登录用户帐户/服务主体之前使用az account clear,这样就不会混淆不同的上下文。
例如,要通过服务主体凭据运行Azure CLI,应该是:
az account clear
az login --service-principal -u <<ApplicationId>> -p <<Password>> -t <tenantID>>
az account set -s <<Subscription-456-ID>>
az account show然后,如果您想通过用户凭据执行操作,只需使用以下命令:
az account clear
az login --service-principal -u <<UserName>> -p <<Password>> -t <tenantID>>
az account set -s <<Subscription-123>>
az account showhttps://stackoverflow.com/questions/67266669
复制相似问题