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
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[*]}
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.