name: Azure CLI
description: 通过命令行界面全面管理 Azure 云平台
license: MIT
metadata:
author: Dennis de Vaal d.devaal@gmail.com
version: "1.0.0"
keywords: "azure, 云, 基础设施, devops, 基础设施即代码, 管理, 脚本"
repository: https://github.com/Azure/azure-cli
compatibility:
- platform: macOS
min_version: "10.12"
- platform: Linux
min_version: "Ubuntu 18.04"
- platform: Windows
min_version: "Windows 10"
掌握 Azure 命令行界面,用于云基础设施管理、自动化和 DevOps 工作流。
Azure CLI 是微软强大的跨平台命令行工具,用于管理 Azure 资源。本技能提供关于 Azure CLI 命令、身份验证、资源管理和自动化模式的全面知识。
macOS:
brew install azure-cli
Linux (Ubuntu/Debian):
curl -sL https://aka.ms/InstallAzureCliLinux | bash
Windows:
choco install azure-cli
# 或从 https://aka.ms/InstallAzureCliWindowsMSI 下载 MSI 安装包
验证安装:
az --version # 显示版本
az --help # 显示通用帮助
# 1. 登录 Azure (会打开浏览器进行身份验证)
az login
# 2. 查看你的订阅
az account list
# 3. 设置默认订阅 (可选)
az account set --subscription "我的订阅"
# 4. 创建一个资源组
az group create -g myResourceGroup -l eastus
# 5. 列出你的资源组
az group list
az login # 交互式登录
az login --service-principal -u APP_ID -p PASSWORD -t TENANT_ID
az login --identity # 托管标识
az logout # 登出
az account show # 当前账户
az account list # 所有账户
az account set --subscription SUBSCRIPTION # 设置默认订阅
--subscription ID # 目标订阅
--resource-group -g RG # 目标资源组
--output -o json|table|tsv|yaml # 输出格式
--query JMESPATH_QUERY # 过滤/提取输出
--verbose -v # 详细输出
--debug # 调试模式
--help -h # 命令帮助
az group list # 列出所有资源组
az group create -g RG -l LOCATION # 创建
az group delete -g RG # 删除
az group show -g RG # 获取详情
az group update -g RG --tags key=value # 更新标签
az vm create -g RG -n VM_NAME --image UbuntuLTS
az vm list -g RG
az vm show -g RG -n VM_NAME
az vm start -g RG -n VM_NAME
az vm stop -g RG -n VM_NAME
az vm restart -g RG -n VM_NAME
az vm delete -g RG -n VM_NAME
az storage account create -g RG -n ACCOUNT --sku Standard_LRS
az storage account list
az storage container create --account-name ACCOUNT -n CONTAINER
az storage blob upload --account-name ACCOUNT -c CONTAINER -n BLOB -f LOCAL_FILE
az storage blob download --account-name ACCOUNT -c CONTAINER -n BLOB -f LOCAL_FILE
az aks create -g RG -n CLUSTER --node-count 2
az aks get-credentials -g RG -n CLUSTER
az aks list
az aks show -g RG -n CLUSTER
az aks delete -g RG -n CLUSTER
# 仅获取特定字段
az vm list --query "[].{name: name, state: powerState}"
# 仅获取名称
az vm list --query "[].name" -o tsv
# 过滤和提取
az vm list --query "[?powerState=='VM running'].name"
#!/bin/bash
set -e # 出错时退出
# 获取虚拟机 ID
VM_ID=$(az vm create \
-g myRG \
-n myVM \
--image UbuntuLTS \
--query id \
--output tsv)
echo "已创建虚拟机: $VM_ID"
# 检查预配状态
az vm show --ids "$VM_ID" --query provisioningState
# 删除资源组中的所有虚拟机
az vm list -g myRG -d --query "[].id" -o tsv | xargs az vm delete --ids
# 按标签列出所有资源
az resource list --tag env=production
# 设置默认值以减少输入
az configure --defaults group=myRG subscription=mySubscription location=eastus
# 现在命令更简洁了
az vm create -n myVM --image UbuntuLTS # 继承了 group, subscription, location
本技能包含用于常见操作的辅助 Bash 脚本:
使用方法:
./scripts/azure-vm-status.sh -g myResourceGroup
./scripts/azure-storage-analysis.sh --subscription mySubscription
Azure CLI 支持使用 JMESPath 进行强大的输出过滤:
# 排序结果
az vm list --query "sort_by([], &name)"
# 复杂过滤
az vm list --query "[?location=='eastus' && powerState=='VM running'].name"
# 聚合
az vm list --query "length([])" # 统计虚拟机数量
# 检查退出码
az vm create -g RG -n VM --image UbuntuLTS
if [ $? -eq 0 ]; then
echo "虚拟机创建成功"
else
echo "创建虚拟机失败"
exit 1
fi
服务主体(自动化):
az login --service-principal \
--username $AZURE_CLIENT_ID \
--password $AZURE_CLIENT_SECRET \
--tenant $AZURE_TENANT_ID
托管标识(Azure 资源):
# 在 Azure 虚拟机或容器实例上
az login --identity
基于令牌的(CI/CD):
echo "$AZURE_ACCESS_TOKEN" | az login --service-principal -u $AZURE_CLIENT_ID --password-stdin --tenant $AZURE_TENANT_ID
# Linux (bash)
eval "$(az completion init bash)"
```
快速查找命令:
bash
az find "create virtual machine" # 搜索命令
对长时间操作使用 --no-wait:
bash
az vm create -g RG -n VM --image UbuntuLTS --no-wait
# 稍后使用 `az vm show` 检查状态
保存常用参数:
bash
az configure --defaults group=myRG location=eastus
与其他工具结合使用:
```bash
# 与 jq 结合进行高级 JSON 处理
az vm list | jq '.[] | select(.powerState == "VM running") | .name'
# 与 xargs 结合进行批量操作
az storage account list --query "[].name" -o tsv | xargs -I {} az storage account show -g RG -n {}
```
scripts/ 目录中的辅助脚本版本: 1.0.0
许可证: MIT
兼容性: Azure CLI v2.50+,Azure 订阅