Azure-cli: [VM] How to specify virtual network from another resource group?

Created on 17 Feb 2017  Â·  15Comments  Â·  Source: Azure/azure-cli

Hi, I have an issue with azure-cli (0.1.1b3).

I have a virtual network "vn1" in a resource group "rg-network".
I have a resource group "rg-machine" in which I want to create a new VM.

When using az vm create --resource-group "rg-machine" --vnet-name "vn1" [...], a new virtual network is created in "rg-machine" instead of using the existing one in "rg-network".
Both networks have the exact same name ("vn1"). Is this the expected behavior ? How can we use networks from another resource group ?

Most helpful comment

Yes, this is the expected behavior. If you want to use a VNET in the other resource group you should supply the fully qualified ID for the subnet in the other resource group you wish to use. If you supply the subnet ID, do not supply vnet-name (you'll get an error). It gets automatically parsed out of the subnet ID.

All 15 comments

Yes, this is the expected behavior. If you want to use a VNET in the other resource group you should supply the fully qualified ID for the subnet in the other resource group you wish to use. If you supply the subnet ID, do not supply vnet-name (you'll get an error). It gets automatically parsed out of the subnet ID.

Thank you, I confirm it works fine with --subnet using version 0.1.2rc1.

@tjprescott, thanks for sharing this info. Is the fully qualified ID of the subnet something like this or is there a friendlier version?

/subscriptions/<MY_SUBSCRIPTION_ID>/resourceGroups/<MY_RESOURCE_GROUP>/providers/Microsoft.Network/virtualNetworks/<MY_VNET>/subnets/<MY_SUBNET>

That's the format. We opted to consume that because it can be easily copied and pasted and/or programmatically retrieved with query and TSV output.

I got the error:
Deployment template validation failed: 'The template resource '"/subscriptions/<MY_SUB_ID>/resourceGroups/<MY_RESOURCE_GROUP>/providers/Microsoft.Network/virtualNetworks/<MY_VNET>"' for type 'Microsoft.Network/virtualNetworks' at line '1' and column '202' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.

Any idea?

The command was:

az vm create -g <MY_RESOURCE_GROUP> --image UbuntuLTS \
  --vnet-name "$(az network vnet show -g <VNET_RESOURCE_GROUP> -n <VNET> -o json --query id)" \
  --subnet "$(az network vnet subnet show -g <VNET_RESOURCE_GROUP> --vnet-name <VNET> -n <SUBNET> -o json --query id)" \
  -n <ServerName>

I'm not sure, but I can see a usage error in your command. You should specify either --subnet NAME --vnet-name NAME OR --subnet ID. You are specifying --subnet ID --vnet-name ID which may be why you are getting an error.

I found the error, @tjprescott. The command az network vnet show -g <VNET_RESOURCE_GROUP> -n <VNET> -o json --query id returns a string with the ". I need to remove the " from the begin and end.

Instead of using -o json, try -o tsv. That will eliminate the quotes

Any idea for why this statement fails?
az vm create -n MyVmTst22 -g "AZtstEUWvmBld" --public-ip-address "" --image win2016datacenter --admin-username "adminBOSS" --admin-password "abcde@12345#" --vnet-name "$(az network vnet show -g Net-DB -n CIND-DB-WEU-VNET-8532 -o tsv --query id)" --subnet "$(az network vnet subnet show -g Net-DB --vnet-name CIND-DB-WEU-VNET-8532 -n SUBNET-1 -o tsv --query id)"

With the following message;
Error Code: InvalidResourceName
Message: Resource name $(az network vnet show -g Net-DB -n CIND-DB-WEU-VNET-8532 -o tsv --query id) is invalid.

@yupsay what operating system are you running this on? Instead of using subcommands to look up the vnet and subnet, why not copy paste the actual IDs?

I see. Sorry, I am not familiar with this on Windows CLI, but I would recommend using the resource IDs instead of the subcommands if you don't need this command to be dynamic.

If you may, please post an example.
For I'm trying to mimic what tjprescott showed above.


From: Farhan Ahmad notifications@github.com
Sent: Thursday, June 21, 2018 7:19:22 PM
To: Azure/azure-cli
Cc: yupsay; Mention
Subject: Re: [Azure/azure-cli] [VM] How to specify virtual network from another resource group? (#2128)

I see. Sorry, I am not familiar with this on Windows CLI, but I would recommend using the resource IDs instead of the subcommands if you don't need this command to be dynamic.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FAzure%2Fazure-cli%2Fissues%2F2128%23issuecomment-399109975&data=02%7C01%7C%7C64b99c127d7346b76d0108d5d77dcca0%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636651857658853963&sdata=ZXMoaa%2BFLKOREtkk7BBYdddLWV2GZmZJ1k3RwGowRYA%3D&reserved=0, or mute the threadhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAXI8co24NWPlgOHEuvodc2h8jVxB6kiVks5t-6RigaJpZM4MEcV7&data=02%7C01%7C%7C64b99c127d7346b76d0108d5d77dcca0%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636651857658853963&sdata=LlbQpatpPVuq13opTHg0elvOcIHt5bkQsJamxCmMh5w%3D&reserved=0.

Something like...

az vm create -n MyVmTst22 -g "AZtstEUWvmBld" --public-ip-address "" --image win2016datacenter --admin-username "adminBOSS" --admin-password "abcde@12345#" --vnet-name "YOUR_VMNET_NAME_HERE" --subnet "SUBNET_HERE"

You can lookup the vnet and subnet in the Azure Portal.

Lastly, these questions might be more appropriate for Stack Overflow.

--subnet "$(az network vnet subnet show -g Net-DB --vnet-name CIND-DB-WEU-VNET-8532 -n SUBNET-1 -o tsv --query id)"

Should look like this:

--subnet $(az network vnet subnet show -g Net-DB --vnet-name CIND-DB-WEU-VNET-8532 -n SUBNET-1 -o tsv --query id)

The double quotes around your $(...) are causing it to be interpretted as a string literal. Also, this will only work in a bash shell, so if you are on Win10, you need to be using WSL.

Was this page helpful?
0 / 5 - 0 ratings