My use-case is:
- Identify if a VM has public IP(T/F).
- (Optional) Print the Public IP.
I have been trying out a lot of combinations mentioned here and here. My primary goal is to identify if a VM has public IP associated or not. Seems like the azure python sdk has changed over time and some of the old solutions are not working.
I found a simple azure cli command which does the job but I am not able to find the corresponding api for it.
az vm list-ip-addresses --name testVM
Is there an API call that can give the same information. If not, whats the best way to identify if an azure VM has public IP.
I tried to reproduce the same in my environment and got below results:
I have one Azure VM named
srivmwith Public IP as below:To fetch this via Python API, I created one service principal with client secret as below:
I tried using below code to get Public IP address of virtual machine and got it successfully:
Response:
You can try using the same code in your environment in order to fetch the Public IP of virtual machine.
Make sure to install required modules and assign required role to the service principal before running the code.