为 Bash 创建 Azure CLI 脚本
几乎在 Azure 门户中可以完成的每个任务,都可以使用 Azure CLI 参考命令来完成。 使用 Azure 门户了解 Azure 是一个很好的起点。 但是,我们建议使用 Azure CLI 或 Azure PowerShell 大规模管理 Azure 资源。
请考虑为全球公司管理 Azure 的方案。 每天收到针对新资源组、Azure 逻辑应用、存储帐户、数据工厂和 Azure SQL 数据库的多个请求。 所有团队都在开发、阶段和生产环境中工作,因此,对于每个请求,都需要创建三个遵循公司命名标准和安全策略的类似 Azure 资源。 是时候使用 Azure CLI 了!
创建 Bash 脚本
- 激活本单元中提供的沙盒。
- 将以下 Bash 脚本复制并粘贴到沙盒中。
- 在沙盒中,按
enter执行脚本
#!/bin/bash
# Get the single resource group name created by the sandbox.
resourceGroupName=$(az group list --query "[].{Name:name}" --output tsv)
echo $rgName
# Create additional variables with values of your choice.
saCount=3
saLocation="westus2"
saNamePrefix="msdocs"
# Loop
for i in $(seq 1 $saCount)
do
let "randomIdentifier=$RANDOM*$RANDOM"
saName=$saNamePrefix$randomIdentifier
echo "Creating storage account $saName in resource group $resourceGroupName"
# create the storage account
az storage account create --name $saName \
--resource-group $resourceGroupName \
--location $saLocation \
--sku Standard_RAGRS \
--kind StorageV2
done
# Verify results
az storage account list --query "[].{saName:name, saPrimaryBlobEndpoint:primaryEndpoints.blob}" --output table
沙盒创建三个 Azure 存储帐户大约需要一分钟时间。 沙盒现在显示以下输出:
SaName SaPrimaryBlobEndpoint
--------------- ----------------------------------------------
msdocsxxxxxxxxx https://msdocsxxxxxxxxx.blob.core.windows.net/
msdocsyyyyyyyyy https://msdocsyyyyyyyyy.blob.core.windows.net/
msdocszzzzzzzzz https://msdocszzzzzzzzz.blob.core.windows.net/
执行 Bash sh 脚本文件
完成第一个 Bash 脚本后,让我们执行保存在文件中的 Bash 脚本。
使用所选的文本编辑器,复制并粘贴 在“创建 Bash 脚本”中给出的 Bash 脚本。
将文本文件另存为
bashScript01.sh到计算机上的某个位置。将脚本文件上传到本单元中提供的沙盒驱动器。
- 在沙盒的主菜单上,展开“ 管理文件 ”下拉列表。
- 选择“上传”。
- 导航到保存
sh文件的位置并上传文件。
在沙盒命令提示符处复制,粘贴并执行以下代码块:
#!/bin/bash # make your file executable chmod +x bashScript01.sh # remove Windows line continuation characters sed -i -e 's/\r$//' bashScript01.sh # Run the script ./bashScript01.sh
使用脚本删除 Azure 资源
创建和测试脚本时,请记住删除测试资源以避免成本。 如果对 Azure 资源使用了命名约定,则使用删除脚本是清理环境的一种简单方法。
删除在特定日期和时间或之后创建的所有存储帐户。 --yes使用参数绕过确认提示。 将 2025-02-17T19:14:27.103127+00:00 更改为你测试脚本的日子。
for saList in $(az storage account list \
--query "[?creationTime >='2025-02-17T19:14:27.103127+00:00'].id" \
--output tsv); do
echo "deleting storage account $saList"
az storage account delete --ids $saList --yes
done
删除包含类似名称的所有资源组,其中包含以下附加功能:
- 将结果写入日志文件。
- 要删除资源组,请使用参数
--no-wait,这样会使删除过程在后台进行,并释放命令行让您继续工作。
# Set your log file location
logFileLocation="myLogName.txt"
# Get the name of all resource groups that start with 'msdocs'
az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o table
# Delete resource groups without a confirmation prompt (--yes)
# Do not wait for the operation to finish (--no-wait)
echo "Deleting resource groups">$logFileLocation
for rgList in $(az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" --output tsv);
do
echo "deleting resource group $rgList">>$logFileLocation
az group delete --name $rgList --yes --no-wait
done
# read your log file with Linux "cat" command
clear
cat $logFileLocation
# verify all resource groups are gone
az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name"
高级脚本编写
本单元中提供的脚本旨在引导人们了解 Azure CLI,这是一种用于大规模管理 Azure 资源的工具。 每个组织都是一个独特的环境,脚本的复杂性取决于需要完成的任务。 在生产设置中运行端到端 Azure CLI 脚本只需完成以下任务:
- 使用
az login通过服务主体、托管标识或联合标识登录到 Azure。 - 创建参数。
- 从配置文件中分配参数值。
- 使用 WHILE 循环或 IF..ELSE 语句创建多个 Azure 资源。
- 验证结果。
- 将结果写入日志文件。
尽管这些脚本相对简单,但本单元中的脚本在将循环和变量与 Azure CLI 参考命令组合在一起时展示了 Azure CLI 的强大潜力。 Azure CLI 示例 GitHub 存储库 包含许多 Bash 脚本,可帮助你继续 Azure CLI 之旅。