【问题标题】:Azure DevTest Labs: how to parametrize resourcesAzure 开发测试实验室:如何参数化资源
【发布时间】:2021-06-27 20:31:24
【问题描述】:

我正在尝试创建一个 PowerShell 脚本,该脚本将部署 Azure 开发测试实验室的一个实例,在那里创建一个环境,以及诸如数据块、天蓝色数据工厂等资源。这是我负责此操作的实验室模板的一部分。

       "resources":[
            {
               "apiVersion":"2018-09-15",
               "name":"LabVmsShutdown",
               "location":"[parameters('location')]",
               "type":"schedules",
               "dependsOn":[
                  "[resourceId('Microsoft.DevTestLab/labs', parameters('labResourceName'))]"
               ],
               "properties":{
                  "status":"Enabled",
                  "timeZoneId":"GMT Standard Time",
                  "dailyRecurrence":{
                     "time":"0100"
                  },
                  "taskType":"LabVmsShutdownTask",
                  "notificationSettings":{
                     "status":"Disabled",
                     "timeInMinutes":30
                  }
               }
            },
            {
               "apiVersion":"2018-09-15",
               "name":"[concat('Dtl', parameters('labResourceName'))]",
               "type":"virtualNetworks",
               "location":"[parameters('location')]",
               "dependsOn":[
                  "[resourceId('Microsoft.DevTestLab/labs', parameters('labResourceName'))]"
               ]
            },
            {
               "apiVersion":"2018-09-15",
               "name":"Public Environment Repo",
               "type":"artifactSources",
               "location":"[parameters('location')]",
               "dependsOn":[
                  "[resourceId('Microsoft.DevTestLab/labs', parameters('labResourceName'))]"
               ],
               "properties":{
                  "status":"Enabled"
               }
            },
            {
               "apiVersion":"2018-09-15",
               "name":"[parameters('repositoryName')]",
               "type":"artifactSources",
               "dependsOn":[
                  "[resourceId('Microsoft.DevTestLab/labs', parameters('labResourceName'))]"
               ],
               "properties":{
                  "uri":"MY_URL",
                  "armTemplateFolderPath":"MY_PATH",
                  "displayName":"DevTestLab",
                  "branchRef":"features/devops-development",
                  "securityToken":"MY_TOKEN",
                  "sourceType":"VsoGit",
                  "status":"Enabled"
               }
            },
            {
               "apiVersion":"2018-09-15",
               "name":"[parameters('userId')]",
               "type":"users",
               "location":"[parameters('location')]",
               "dependsOn":[
                  "[resourceId('Microsoft.DevTestLab/labs', parameters('labResourceName'))]"
               ],
               "properties":{
                  "status":"Enabled"
               },
               "resources":[
                  {
                     "name":"devtestlaab",
                     "type":"environments",
                     "apiVersion":"2018-09-15",
                     "location":"[parameters('location')]",
                     "properties":{
                        "deploymentProperties":{
                           "armTemplateId":"[concat(resourceId('Microsoft.DevTestLab/labs/artifactsources', parameters('labResourceName'), parameters('repositoryName')), '/armTemplates/DevTestLab')]"
                        },
                        "armTemplateDisplayName":"DevLab Deployment Script"
                     },
                     "dependsOn":[
                        "[resourceId('Microsoft.DevTestLab/labs/users', parameters('labResourceName'), parameters('userId'))]"
                     ]
                  }
               ]
            }
         ]
      }
   ]

它在大多数情况下都可以正常工作。但是,我无法为我的内部模板传递参数。它只需要在给定时刻在我的ref_branch 上的这些模板中硬编码的任何内容并部署它。

我尝试按照以下模板添加参数部分,但它被完全忽略了。

{
  "name": "string",
  "type": "Microsoft.DevTestLab/labs/users/environments",
  "apiVersion": "2018-09-15",
  "location": "string",
  "tags": {},
  "properties": {
    "deploymentProperties": {
      "armTemplateId": "string",
      "parameters": [
        {
          "name": "string",
          "value": "string"
        }
      ]
    },
    "armTemplateDisplayName": "string"
  }
}

总而言之,我能够部署:

  • 实验室
  • 环境
  • 本实验室内的资源。

我不能:

  • 将任何参数传递给我的实验室环境中的资源。

文档非常稀缺,所以我不确定问题出在哪里。

【问题讨论】:

    标签: azure powershell arm-template azure-devtest-labs


    【解决方案1】:

    AFAI 明白,您的模板看起来不对。请查看A quick guide on writing your Azure Resource Manager (ARM) Templates 了解如何使用参数。

    对于PowerShell script deployment,您可以使用下面的命令来部署带有参数的ARM模板:

    New-AzResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleResourceGroup `
      -TemplateFile <path-to-template-or-bicep> `
      -TemplateParameterFile c:\MyTemplates\storage.parameters.json
    

    New-AzResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleResourceGroup `
      -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json `
      -TemplateParameterUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.parameters.json
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 嗨,谢谢,虽然有点不同。我使用您描述的方式来部署实验室资源本身。但我也想在这个实验室中创建一个环境,并将不同的资源部署到这个环境中。所以我的“外部”模板应该调用“内部”工件模板并将那里指定的所有资源部署到我的环境中。我觉得这个开发测试实验室有点混乱,所以我希望这次能解释得更好。
    【解决方案2】:

    通常在 TFS 管道中,您会定义变量并将它们作为参数传递给脚本。查看示例here

    但是,由于您专门处理 ARM 模板并考虑控制流(外/内循环等),您也可以查看其他几个选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多