Ask the user to select the Azure cloud under which he runs the Azure CLI:
login_azure()
{
no_cloud=true
while $no_cloud; do
echo 'Select your cloud:'
az cloud list --output table
read -p 'Enter the name:' cloudName
az cloud set --name $cloudName && no_cloud=false
done
az account show || az login
echo 'To logout, please press `Ctrl + C` and type: `az logout`.'
}
Ask the user to select the subscription under which he runs the Azure CLI:
select_subscription()
{
no_cloud=true
subscriptionId=""
while $no_cloud; do
az account list
read -p 'Enter the subscription `id` (Example: aaaaaaa-bbbb-cccc-dddd-eeeeeeeeee):' subscriptionId
az account set --subscription $subscriptionId && no_cloud=false
done
}
Get the current tenant id:
tenantId=$(az account show --output tsv | awk '{print $8}')
Get the current tenant domain:
aadDomain=$(az ad signed-in-user show --query 'userPrincipalName' | cut -d '@' -f 2 | sed 's/\"//')
Create a resource if it not exist. For example, to create a resource group:
rsgName="MyNewResourcesGroup"
az group list --output tsv | grep $rsgName -q || az group create -l $regionName -n $rsgName && sleep 10
Create application insights and get the instrument key:
applicationInsightsName="MyNewAppInsights"
# Install the extension
az extension add -n application-insights
# Create
az monitor app-insights component show --output table | grep $applicationInsightsName -q || az monitor app-insights component create -a $applicationInsightsName -l $regionName --kind web -g $rsgName --application-type web
# Get key
aiInsKey=$(az monitor app-insights component show -a $applicationInsightsName -g $rsgName -o tsv | awk '{print $10}')
Get SQL Server database connection string:
connectionString=$(az sql db show-connection-string --client ado.net --name $sqlDatabaseName --output tsv)
Get Azure SignalR connection string:
signalRConnectionString=$(az signalr key list --name $signalRServiceName -g $rsgName --query primaryConnectionString -o tsv)
Get the Azure Storage Account connection string:
storageConnectionString=$(az storage account show-connection-string -n $storageAccountName -g $rsgName --output tsv)
Get the Azure App Service access URL:
appserviceDomain=$(az webapp show -n $webAppName -g $rsgName --output tsv | awk '{print $9}')
appserviceUrl=$(echo https://$appserviceDomain)
Set environment variable to function app:
az functionapp config appsettings set -g $rsgName -n $liveServiceFunctionName --settings InputQueueName="hmedia-live-input-queue"
Set environment variable to Azure app service:
az webapp config appsettings set -g $rsgName -n $webAppName --output none --settings something:something="$somevar"
I enjoyed reading your blog post on Azure CLI script programming on bash. It is evident that you have a deep understanding of the subject matter, and your tips are well-structured and easy to follow. Your approach to asking the user to select the Azure cloud and subscription is particularly helpful, as it provides a clear and straightforward way for users to configure their environment.
One of the most notable aspects of your blog post is the extensive use of Azure CLI commands to perform various tasks, such as creating resources, retrieving connection strings, and setting environment variables. Your examples are concise and well-explained, which will undoubtedly be beneficial to readers who are new to Azure CLI scripting.
However, there are a few areas where the post could be improved. Firstly, it would be helpful to include a brief introduction to Azure CLI and its benefits, as this would provide context for readers who may be unfamiliar with the topic. Additionally, it would be useful to explain the purpose of each function and variable, as this would help readers understand the rationale behind the code.
Furthermore, there are a few instances where the code could be optimized. For example, in the
select_subscription()
function, the variableno_cloud
is used, but it would be more appropriate to use a variable name such asno_subscription
to accurately reflect the purpose of the variable.Lastly, it would be beneficial to provide a complete example script that incorporates all the tips and functions you have discussed. This would allow readers to see how everything fits together and give them a starting point for their own Azure CLI scripting projects.
In conclusion, your blog post offers valuable tips for Azure CLI script programming on bash, and your examples are well-executed and informative. With a few minor improvements, this post could be an excellent resource for anyone looking to learn more about Azure CLI scripting. Keep up the great work!