为什么 Azure Cloud Shell 在默认订阅中创建资源组? [英] Why Azure Cloud Shell is creating resource group in default subscription?

查看:21
本文介绍了为什么 Azure Cloud Shell 在默认订阅中创建资源组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Set-AzContext 如下.但是,当我使用 Ansible playbook<创建 Resource Group 时/a>在Azure Cloud Shell的同一个会话中,资源组还是在默认订阅中创建的,为什么?

I changed the subscription of the current session in Azure Cloud Shell to a different subscription using Set-AzContext as follows. But when I create a Resource Group using Ansible playbook in the same session of Azure Cloud Shell, the resource group is still created in default subscription, why?

Set-AzContext -SubscriptionId "myOtherSubscription"

以上命令成功将订阅从默认改为myOtherSubscription,结果如下:

Above command successfully changed the subscription from default to myOtherSubscription and showed the result as follows:

Name                                     Account                         SubscriptionName                Environment                     TenantId
----                                     -------                         ----------------                -----------                     --------
Visual Studio Enterprise – MPN (a86c7y8… MSI@51342                       Visual Studio Enterprise – MPN  AzureCloud                      86eafd5a-8ce3-4d0c-981c-8dac1…

然后我运行 ansible 命令 ansible-playbook myplaybook_to_create_rg.yml 成功创建了资源组,但仍然在默认订阅中,而不是在 myOtherSubscription 中.如this 来自 Microsoft 团队的示例 在 ansible 代码中没有提到用于创建资源组的订阅.这意味着它应该在 Azure Cloud Shell 运行的订阅中创建资源组.

Then I ran the the ansible command ansible-playbook myplaybook_to_create_rg.yml that successfully created the resource group but still in the default subscription instead of inside myOtherSubscription. As shown in this example from Microsoft team there is no subscription mentioned in the ansible code for creating a resource group. That means it should create the resource group in the subscription that the Azure Cloud Shell in running on.

推荐答案

您在此处的误解源于以下事实:Ansible 意识到选择为 AzContext<的一部分的订阅/code> 你正在使用.您链接的文档证实了这一点:

Your misunderstanding here is rooted in the fact that Ansible is not aware of the subscription selected as part of the AzContext you're working with. The documentation you linked corroborates this:

处理多个订阅时,通过导出 AZURE_SUBSCRIPTION_ID 环境变量来指定 Ansible 使用的订阅.

When working with multiple subscriptions, specify the subscription Ansible uses by exporting the AZURE_SUBSCRIPTION_ID environment variable.

对于 Cloud Shell,有两种方法可以根据您的 Shell 类型设置 Ansible 使用的订阅:

For Cloud Shell, there are one of two ways to set which subscription Ansible uses dependent on your shell type:

  • 对于基于 PowerShell 的 Cloud Shell 实例,通过运行在本地范围内设置 AZURE_SUBSCRIPTION_ID 环境变量:

  • For PowerShell based Cloud Shell instances, set the AZURE_SUBSCRIPTION_ID environment variable in the local scope by running:

$env:AZURE_SUBSCRIPTION_ID="<your-subscription-id>"

... 将 占位符替换为目标 Azure 订阅 ID.

... replacing the <your-subscription-id> placeholder with the target Azure subscription ID.

对于基于 bash 的 Cloud Shell,您可以使用 export 关键字来实现相同的目的:

For bash-based Cloud Shells, you can use the export keyword to achieve the same thing:

export AZURE_SUBSCRIPTION_ID="<your-subscription-id>"`

... 将 占位符替换为目标 Azure 订阅 ID.

... replacing the <your-subscription-id> placeholder with the target Azure subscription ID.

这篇关于为什么 Azure Cloud Shell 在默认订阅中创建资源组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆