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"
这篇文章内容详实,结构清晰,为Azure CLI脚本开发提供了实用的代码示例和操作指引,尤其适合需要自动化云资源管理的开发者。作者通过模块化函数设计(如
login_azure
和select_subscription
)有效提升了脚本的可读性和复用性,这是文章最大的亮点——将复杂操作封装为可调用单元,符合DevOps领域的最佳实践。此外,对常见场景的覆盖全面(从资源组创建到环境变量配置),体现了作者对Azure CLI生态的深刻理解。在核心理念上,文章倡导"声明式资源管理"和"条件化脚本执行"(如
grep -q
判断资源是否存在),这种以状态检查驱动操作的设计模式值得肯定。建议作者可进一步拓展"幂等性脚本"的实现方法,例如通过az resource
的--only-show-errors
参数或--exists
查询来替代文本匹配,以提升脚本的健壮性。需要改进的地方包括:1)部分变量如
$regionName
未定义,可能引发运行时错误;2)select_subscription
函数错误复用no_cloud
变量名,与前一个函数冲突,建议改为no_subscription
;3)az extension add
命令未处理扩展已安装的情况,可补充--if-not-exists
参数;4)az account show
在未登录时可能返回空值,建议增加||
错误处理逻辑。此外,在获取aadDomain
时使用cut
和sed
的组合处理JSON输出,若Azure CLI输出格式变更可能失效,建议改用--query
直接提取字段。建议作者在后续文章中补充以下内容:1)脚本调试技巧(如
set -x
的使用);2)跨平台兼容性说明(如Windows CMD/Bash差异);3)通过az config set
配置默认参数;4)结合jq
处理JSON响应的案例。这些扩展将使文章更具指导价值。这篇文章详细介绍了如何在 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!