使用 Azure CLI 管理 Azure 资源Manage Azure resources by using Azure CLI

了解如何将 Azure CLI 与 Azure 资源管理器配合使用来管理 Azure 资源。Learn how to use Azure CLI with Azure Resource Manager to manage your Azure resources. 若要管理资源组,请参阅使用 Azure CLI 管理 Azure 资源组For managing resource groups, see Manage Azure resource groups by using Azure CLI.

有关资源管理的其他文章:Other articles about managing resources:

将资源部署到现有的资源组Deploy resources to an existing resource group

可以使用 Azure PowerShell 直接部署 Azure 资源,也可以部署资源管理器模板来创建 Azure 资源。You can deploy Azure resources directly by using Azure PowerShell, or deploy a Resource Manager template to create Azure resources.

部署资源Deploy a resource

以下脚本创建一个存储帐户。The following script creates a storage account.

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the location (i.e. chinaeast):" &&
read location &&
echo "Enter the storage account name:" &&
read storageAccountName &&
az storage account create --resource-group $resourceGroupName --name $storageAccountName --location $location --sku Standard_LRS --kind StorageV2 &&
az storage account show --resource-group $resourceGroupName --name $storageAccountName 

部署模板Deploy a template

以下脚本通过部署快速入门模板来创建存储帐户。The following script creates deploy a Quickstart template to create a storage account. 有关详细信息,请参阅快速入门:使用 Visual Studio Code 创建 Azure 资源管理器模板For more information, see Quickstart: Create Azure Resource Manager templates by using Visual Studio Code.

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the location (i.e. chinaeast):" &&
read location &&
az group deployment create --resource-group $resourceGroupName --template-uri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json"

有关详细信息,请参阅使用资源管理器模板和 Azure CLI 部署资源For more information, see Deploy resources with Resource Manager templates and Azure CLI.

部署资源组和资源Deploy a resource group and resources

可以创建一个资源组,然后将资源部署到该组。You can create a resource group and deploy resources to the group. 有关详细信息,请参阅创建资源组并部署资源For more information, see Create resource group and deploy resources.

将资源部署到多个订阅或资源组Deploy resources to multiple subscriptions or resource groups

通常情况下,将模板中的所有资源部署到单个资源组。Typically, you deploy all the resources in your template to a single resource group. 不过,在某些情况下,你可能希望将一组资源部署在一起但将其放置在不同的资源组或订阅中。However, there are scenarios where you want to deploy a set of resources together but place them in different resource groups or subscriptions. 有关详细信息,请参阅将 Azure 资源部署到多个订阅或资源组For more information, see Deploy Azure resources to multiple subscriptions or resource groups.

删除资源Delete resources

以下脚本演示如何删除存储帐户。The following script shows how to delete a storage account.

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the storage account name:" &&
read storageAccountName &&
az storage account delete --resource-group $resourceGroupName --name $storageAccountName 

若要详细了解 Azure 资源管理器如何控制资源的删除,请参阅 Azure 资源管理器资源组的删除For more information about how Azure Resource Manager orders the deletion of resources, see Azure Resource Manager resource group deletion.

移动资源Move resources

以下脚本演示如何将存储帐户从一个资源组移到另一个资源组。The following script shows how to remove a storage account from one resource group to another resource group.

echo "Enter the source Resource Group name:" &&
read srcResourceGroupName &&
echo "Enter the destination Resource Group name:" &&
read destResourceGroupName &&
echo "Enter the storage account name:" &&
read storageAccountName &&
storageAccount=$(az resource show --resource-group $srcResourceGroupName --name $storageAccountName --resource-type Microsoft.Storage/storageAccounts --query id --output tsv) &&
az resource move --destination-group $destResourceGroupName --ids $storageAccount

有关详细信息,请参阅将资源移到新资源组或订阅For more information, see Move resources to new resource group or subscription.

锁定资源Lock resources

锁定可以防止组织中的其他用户意外删除或修改关键资源,例如 Azure 订阅、资源组或资源。Locking prevents other users in your organization from accidentally deleting or modifying critical resources, such as Azure subscription, resource group, or resource.

以下脚本锁定一个存储帐户,因此无法删除该帐户。The following script locks a storage account so the account can't be deleted.

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the storage account name:" &&
read storageAccountName &&
az lock create --name LockSite --lock-type CanNotDelete --resource-group $resourceGroupName --resource-name $storageAccountName --resource-type Microsoft.Storage/storageAccounts 

以下脚本获取存储帐户的所有锁:The following script gets all locks for a storage account:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the storage account name:" &&
read storageAccountName &&
az lock list --resource-group $resourceGroupName --resource-name $storageAccountName --resource-type Microsoft.Storage/storageAccounts --parent ""

以下脚本删除存储帐户的锁:The following script deletes a lock of a storage account:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the storage account name:" &&
read storageAccountName &&
lockId=$(az lock show --name LockSite --resource-group $resourceGroupName --resource-type Microsoft.Storage/storageAccounts --resource-name $storageAccountName --output tsv --query id)&&
az lock delete --ids $lockId

更多信息请参阅 使用 Azure Resource Manager 锁定资源For more information, see Lock resources with Azure Resource Manager.

标记资源Tag resources

标记有助于按逻辑方式组织资源组和资源。Tagging helps organizing your resource group and resources logically. 有关信息,请参阅使用标记组织 Azure 资源For information, see Using tags to organize your Azure resources.

管理对资源的访问Manage access to resources

可以在 Azure 中通过基于角色的访问控制 (RBAC) 这种方式管理对资源的访问。Role-based access control (RBAC) is the way that you manage access to resources in Azure. 有关详细信息,请参阅使用 RBAC 和 Azure CLI 管理访问权限For more information, see Manage access using RBAC and Azure CLI.

后续步骤Next steps