1

我有一个资源组,其中包含使用二头肌单独创建的开发测试实验室和虚拟网络 (VNet)。

我可以使用 Azure 门户手动将 VNet 附加到 Devtest 实验室,方法是:

  1. 前往开发测试实验室
  2. 选择“配置和策略”
  3. 从外部资源菜单中,选择“虚拟网络”
  4. 点击“添加”
  5. 选择 VNet

是否可以使用 Azure CLI 自动执行此过程?或任何其他选择?

因为我采用 Azure DevOps 管道来自动运行(Bicep 代码)和调整(Azure CLI)资源。

我将 ARM/Bicep 模板 ( Microsoft.Network/virtualNetworks@2021-02-01) 用于 VNET,将 ( Microsoft.DevTestLab/labs@2018-09-15) 用于开发测试实验室

4

1 回答 1

1

如果您要分别创建 Vnet 和 Devtest lab,那么您可以使用来自 Github 的以下Azure 快速入门模板来创建具有现有 vnet 的 Devtestlab:

JSON ARM:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "newLabName": {
        "type": "string",
        "metadata": {
          "description": "The name of the new lab instance to be created."
        }
      },
      "newLabVirtualNetworkName": {
        "type": "string",
        "metadata": {
          "description": "The name of the new lab virtual network instance to be created with the new lab instance being created."
        }
      },
      "existingVirtualNetworkId": {
        "type": "string",
        "metadata": {
          "description": "The resource ID pointing to an existing (compute) virtual network to be referenced by the new lab virtual network instance being created."
        }
      },
      "existingSubnetName": {
        "type": "string",
        "defaultValue": "default",
        "metadata": {
          "description": "The name of an existing (compute) subnet instance to be configured for Lab VM creation."
        }
      }
    },
    "variables": {
      "existingSubnetId": "[concat(parameters('existingVirtualNetworkId'), '/subnets/', parameters('existingSubnetName'))]"
    },
    "resources": [
      {
        "apiVersion": "2018-10-15-preview",
        "type": "Microsoft.DevTestLab/labs",
        "name": "[parameters('newLabName')]",
        "location": "[resourceGroup().location]",
        "resources": [
          {
            "apiVersion": "2018-10-15-preview",
            "name": "[parameters('newLabVirtualNetworkName')]",
            "type": "virtualNetworks",
            "dependsOn": [
              "[resourceId('Microsoft.DevTestLab/labs', parameters('newLabName'))]"
            ],
            "properties": {
              "description": "Existing Compute virtual network associated as part of the lab creation process.",
              "externalProviderResourceId": "[parameters('existingVirtualNetworkId')]",
              "subnetOverrides": [
                {
                  "name": "[parameters('existingSubnetName')]",
                  "resourceId": "[variables('existingSubnetId')]",
                  "useInVmCreationPermission": "Allow",
                  "usePublicIpAddressPermission": "Allow"
                }
              ]
            }
          }
        ]
      }
    ],
    "outputs": {
      "labId": {
        "type": "string",
        "value": "[resourceId('Microsoft.DevTestLab/labs', parameters('newLabName'))]"
      }
    }
  }

输出:我在我的环境中对其进行了测试,结果如下:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

注意:不幸的是,没有使用Azure CLIAzure-Powershell连接 vnet 和Devtestlabs的命令。

二头肌臂:

@description('The name of the new lab instance to be created.')
param newLabName string

@description('The name of the new lab virtual network instance to be created with the new lab instance being created.')
param newLabVirtualNetworkName string

@description('The resource ID pointing to an existing (compute) virtual network to be referenced by the new lab virtual network instance being created.')
param existingVirtualNetworkId string

@description('The name of an existing (compute) subnet instance to be configured for Lab VM creation.')
param existingSubnetName string = 'default'

var existingSubnetId = '${existingVirtualNetworkId}/subnets/${existingSubnetName}'

resource newLabName_resource 'Microsoft.DevTestLab/labs@2018-10-15-preview' = {
  name: newLabName
  location: resourceGroup().location
}

resource newLabName_newLabVirtualNetworkName 'Microsoft.DevTestLab/labs/virtualNetworks@2018-10-15-preview' = {
  parent: newLabName_resource
  name: newLabVirtualNetworkName
  properties: {
    description: 'Existing Compute virtual network associated as part of the lab creation process.'
    externalProviderResourceId: existingVirtualNetworkId
    subnetOverrides: [
      {
        name: existingSubnetName
        resourceId: existingSubnetId
        useInVmCreationPermission: 'Allow'
        usePublicIpAddressPermission: 'Allow'
      }
    ]
  }
}

output labId string = newLabName_resource.id

输出:

在此处输入图像描述

在此处输入图像描述

于 2021-09-27T06:15:01.847 回答