Azure Classroom Series – 15/May/2021

Azure Instance Metadata Service and jq

  • The jq Refer Here is a light weight json parser.
  • Example queries
curl -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance?api-version=2020-09-01" | jq '.| { os: .compute.osType}'

curl -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance?api-version=2020-09-01" | jq '.| { os: .compute.osType, distribution: .compute.offer}'


curl -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance?api-version=2020-09-01" | jq '.| { os: .compute.osType, distribution: .compute.offer, publicip: .network.interface[0].ipv4.ipAddress[].publicIpAddress}'
  • Now lets try to write a script which uses IMDS
  • This sample script runs on start up of the Linux VM and updates the packages
#!/bin/bash
offer=$(curl -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/offer?api-version=2020-09-01&format=text")
echo $offer
if [[ $offer == "UbuntuServer" ]]; then
   sudo apt update
elif [[ $offer == "Centos" ]]; then
   sudo yum update -y
else
   echo "The current distribution $offer is not supported"
fi

Azure CLI

  • Ensure Azure CLI is installed on the machine
  • Now connect to Azure
az login
# once the login is success
az group list
  • Azure CLI is a command line utility which can be used to perform operations on Azure
  • Azure CLI commands will be in the following structure
az <service> [<sub section>] <action> --parameter1 <value1> ... --parametern <valuen>
  • Finding azure cli commands is very simple
  • Exercise:
    • Create a resource group in Azure using portal Preview
    • Now lets see how to do the same stuff using azure cli. Navigate to Refer Here Preview Preview Preview Preview
  • Output formats for the cli commands Refer Here
  • Lets try to use azure cli to search for VM images Preview Preview Preview
  • In Azure Any VM Image has the following relation b/w publisher, offer and sku Preview
  • Exercise: Explore different windows servers and centos server images available in Azure

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin