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"
这篇文章详细介绍了如何在 bash 环境下编写和使用 Azure CLI 脚本,提供了多个实用的代码示例,涵盖了从用户认证到资源创建再到配置设置等多个方面。以下是对本文的一些评价和建议:
优点:
login_azure()
和select_subscription()
),作者展示了如何将复杂任务分解为更小、更易于管理的部分。核心理念:
login_azure()
中使用循环确保用户正确选择云环境,在设置订阅时通过验证机制避免无效输入。这些理念值得鼓励,尤其是对希望提高工作效率的开发者来说非常有帮助。
可改进的地方:
select_subscription()
函数中使用$subscriptionId
作为变量名是合适的,但在其他地方可能需要更明确的命名。az
命令的参数和输出格式可能对新手不够友好。建议在每个代码示例后简要说明关键参数的作用,或者推荐官方文档以供参考。建议扩展的内容:
Jenkins
或其他 CI/CD 工具使用这些脚本。总体来说,这篇文章为开发者提供了一个良好的起点,展示了 Azure CLI 脚本编程的实用技巧。通过一些细节上的优化和内容扩展,可以进一步提升文章的价值,帮助更多人掌握 Azure CLI 的高级用法。
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!