You are automating your stuff and need to obtain the the public IP from an Azure VM, via CLI. Here’s some tips and a step by step for you.
- You are using Linux|Unix
- You are using CLI
- You have access to the azure environment and a running VM.
- Install Azure CLI;
- Get public IP;
$ curl -L https://aka.ms/InstallAzureCli | bash $ source ~/.bash_profile $ az vm show -d -g MyGroupName1 -n MyVMName1 --query publicIps -o tsv
Installing Azure CLI
If you recently started working with Azure you first need to install Azure CLI on your environment.
1. Open your terminal and run:
curl -L https://aka.ms/InstallAzureCli | bash
If you don’t want to change the installation directories, just press enter whenever queried. Watch it download and install dependencies.
2. Refresh the settings available in your bash_profile configuration (or any other file you chose during installation) with the command below (or open a new tab).
3. Log in into the azure account you want to work with.
Option 1 – Non-Interactive:
az login -u <username> -p <password>
Option 2 – Interactive: When running the command below, a web page should open in your browser.
Select the a user within in the web page that just opened.
Get back to your terminal and you should now be able to access your resources.
Obtaining Azure VM Public IP via CLI
In azure, the virtual machines and all other resources belongs to a group. If you don’t remember the names of your groups, run this command to get a list of their names:
$ az group list --query .name [ "MyGroupName1", "MyGroupName2" ]
To grab the name of your VM, you can run:
$ az vm list --query .name [ "MyVMName1", "MyVMName2" ]
Now, you can finally get your public IP (fake result exemplified below):
$ az vm show -d -g MyGroupName1 -n MyVMName1 --query publicIps -o tsv 22.214.171.124
That’s it. :]