3

I need to store the output of an AZ cli commands that fetches my private IPs as a variable.

I am using the following in a bash script:

echo "Fetching Monitoring Server IP"
SERVER_IP=$(az vm show -n ${THIS_VM_NAME} -g ${RSC_GRP_NAME} --query privateIps -o tsv)
echo "$SERVER_IP

It would appear that this isnt working as when I echo the variable, it comes back empty.

+ THIS_VM_NAME=XXXX-XX-XX-XX-XX
+ echo 'Fetching Monitoring Server IP'
Fetching Monitoring Server IP
++ az vm show -n XXXX-XX-XX-XX-XX3 -g XXXX-XX-XX-XX-XX --query privateIps -o tsv
+ SERVER_IP=
+ echo ''

I will appreciate any pointers on this

1
  • Are you sure the command is writing to stdout? Try appending 2>&1 to your command to catch stderr as well. Commented Nov 30, 2018 at 3:52

2 Answers 2

7

Edit

The command you post lost a parameter to get the private IPs, you can use the command with the parameter -d or --show-details like this:

az vm show -g resourceGrouName -n vmName -d

But this command just gets all the IPs including the secondary IP.

You can get all the VM primary IPs of each interface through a shell script like this:

count=0
while : ; do
    nic=$(az vm nic list -g resourceGroupName --vm-name vmName --query [$count].id -o tsv)
    if [[ $nic == '' ]]; then
        break
    fi
    privateIps[$count]=$(az vm nic show -g resourceGroupName --vm-name vmName --nic $nic --query ipConfigurations[0].privateIpAddress -o tsv)
    let count++

done
echo ${privateIps[*]}
Sign up to request clarification or add additional context in comments.

2 Comments

This is incorrect as my command is returning the private ip. Please run the command above to see if it works on yours
Oh, sorry. I made a mistake. I have updated the answer. And the command you post also missed a parameter. And there is something different in the two solutions.
5

A solution to my similar problem in Azure Cloud Shell was putting a dollar sign before your SERVER_IP variable if Powershell in chosen.

$SERVER_IP=$(az vm show --name vmname --resource-group rgname --show-details --query [publicIps] --output tsv)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.