Azure Classroomnotes 27/Sep/2023

Azure CLI

  • Create a linux VM
#!/bin/bash

# exists(string, substring)
# This function tells whether the string contains substring
function exists()
{
    STR=$1
    SUB=$2
    if [[ "$STR" == *"$SUB"* ]]; then
        echo true
    else 
        echo false
    fi
}


# create_resource_group(name,location)
# This function creates resource group if it does not exist
function create_resource_group()
{
    name=$1
    location=$2
    if [ $(az group exists --name $name) = true ]; then 
        echo "resource group $name already exists"
    else
        az group create --name $name --location $location 
    fi
}

# create_vm(name, group, location, username, password, size, image)
# This function creates virtual machine if it does not exist
function create_vm()
{
    name=$1
    group=$2
    location=${3:-eastus}
    username=${4:-Dell}
    password=${5:-QualityThought@1234}
    size=${6:-Standard_B1s}
    image=${7:-Ubuntu2204}
    vms=$(az vm list --resource-group "fromcli"  --query "[].name" --output tsv)
    if [[ $(exists "$vms" "$name") == true ]]; then
        echo "vm already exists"
    else 
        az vm create \
            --name "$name" \
            --resource-group "$group" \
            --location "$location" \
            --admin-password "$password" \
            --admin-username "$username" \
            --authentication-type 'password' \
            --size "$size" \
            --nsg-rule "SSH" \
            --image "$image"
    fi

}



rg_name="fromcli"
location="eastus"
vm_name="myfirstvm"
vm_image="Ubuntu2204"
vm_username="qtdevops"
vm_password="QualityThought@1234"
vm_size="Standard_B1s"

create_resource_group ${rg_name} ${location}
create_vm $vm_name $rg_name $location $vm_username $vm_password $vm_size $vm_image
#create_vm "checkingmyluck" "fromcli"


  • Execute with bash -x and google around to enhance the scripts
Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%