Azure CLI
az <service-group> [sub-groups] <action> [args]
az group create
az sql server create
az vm list
az group delete
- azure cli also gives the responses in json form by default
- We can filter the responses using jmes path
az group list --query ""
-
Finding documentation:
- Google
azure <> cli or azure <> quick start
-
bash guide
Lets create an Azure Virtual machine
- inputs:
- resource group:
- name: hello-cli
- location: eastus
- size: Standard_B1s
- Image: ubuntu 24.04
- credentials:
- ports or nsg
- output: virtual machine with public ip
- Steps:
- create a resource group
- create a vm
#!/bin/bash
group_name='fromcli'
location='eastus'
vm_name='node-1'
# if resource group does not exist create one
if [ $(az group exists --name $group_name) = false ]; then
az group create --name ${group_name} --location ${location}
az vm list --output tsv | grep $vm_name -q || az vm create --name ${vm_name} \
--resource-group ${group_name} \
--authentication-type 'password' \
--admin-username 'qtdevops' \
--admin-password 'ltdevops@123' \
--nsg-rule 'SSH' \
--size 'Standard_B1s' \
--image 'Ubuntu2404' \
--zone 1
else
echo $resourceGroup
fi
Lets create reusable scripts in AWS and Azure using CLI
Manage start, stop and terminate of ec2 instances
- inputs:
- instance-id
- region
- action: start | stop | terminate
- solution
#!/bin/bash
#
# Controls start stop and terminating the ec2 instance
# usage:
# ./ec2_control.sh <action> <instance-id> <region>
# action = [start|stop|termination]
# author: khaja
#
action=$1
instance_id=$2
region=$3
if [[ $action == "start" ]]; then
# if action is start
aws ec2 start-instances --instance-ids ${instance_id} --region ${region}
echo "performed action: ${action} on instance-id ${instance_id}"
elif [[ $action == "stop" ]]; then
# if action is stop
aws ec2 stop-instances --instance-ids ${instance_id} --region ${region}
echo "performed action: ${action} on instance-id ${instance_id}"
elif [[ $action == "terminate" ]]; then
# if action is terminate
aws ec2 terminate-instances --instance-ids ${instance_id} --region ${region}
echo "performed action: ${action} on instance-id ${instance_id}"
else
echo "Wrong usage: Not a valid action"
echo "./ec2_control.sh <action> <instance-id> <region>"
echo "action = [start|stop|termination]"
exit 1
fi
# command="${action}-instances"
# aws ec2 ${command} --instance-ids ${instance_id} --region ${region}
- Write me something similar for azure vms
# vm_control.sh <action> <group-name> <vm-name>
# action = start | stop | delete
Like this:
Like Loading...