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"
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…
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.
AZURE_SUBSCRIPTION_IDenvironment variable (PowerShell:$env:AZURE_SUBSCRIPTION_ID)? Is it the ID ofmyOtherSubscriptionor another subscription? It's specifically mentioned in the linked article that the Ansible playbook uses that to determine which subscription to use: "When working with multiple subscriptions, specify the subscription Ansible uses by exporting theAZURE_SUBSCRIPTION_IDenvironment variable." - esqewexport AZURE_SUBSCRIPTION_ID=<your-subscription-id>withmy-subscription-id. Let me try it first. - namAZURE_SUBSCRIPTION_IDin the current session of Azure Cloud Shell? - nam$env:AZURE_SUBSCRIPTION_IDto print the current value; in Bash-based shells you should be able to use something likeecho $AZURE_SUBSCRIPTION_ID. - esqew$env:AZURE_SUBSCRIPTION_ID="<your-subscription-id>"to create the locally-scoped environment variable and re-run the Ansible playbook in the Cloud Shell? The issue may be that you're attempting to useexportwhich isn't valid in a PowerShell-based environment. You should have gotten a big red error when trying to run theexport...command. - esqew