有没有一种方法可以获取任务中使用的SubscriptionID:"Azure资源组部署" [英] Is there a way to get the SubscriptionID used in task: "Azure Resource Group Deployment"

查看:100
本文介绍了有没有一种方法可以获取任务中使用的SubscriptionID:"Azure资源组部署"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Azure Devops中使用 Azure资源组部署任务.某些 Override模板参数包括SubscriptionID.

I am using Azure Resource Group Deployment task in Azure Devops. Some of the Override template parameters include the SubscriptionID.

-virtualNetworkId /subscriptions/53614803-d327-4397-bf55-8dedcc24be0d/resourceGroups/$(resourceGroup)/providers/Microsoft.Network/virtualNetworks/vnet 

是否可以使用 Azure订阅连接中使用的SubsciptionID?

Is there a was to use the SubsciptionID used in the Azure subscription connection ?

原因:需要在构建管道中知道subscriptionId.这也解决了uniqueString模板问题,我们需要知道uniqueString,但是powershell无法告诉您.

Reason: The subscriptionId needed to be known in the build pipeline. This also solves the uniqueString template issue, where we need to know the uniqueString, but powershell does not have a way to tell you.

我的部署被构建为可以在不同Subscriptions中的任何ResourceGroup中运行.我正在使用生成的ARM模板部署多个VM,并在门户中创建一个VM.其中一个参数嵌入了所讨论的订阅ID.由于在门户网站构建的模板内部如何计算变量,[subscription.SubscriptionId]无法正常工作:

My deployments are being built to run in any ResourceGroup in different Subscriptions. I am deploying several VM's using the ARM template generated creating one VM in the portal. One of the parameters embedded the subscription Id as shown in question. Because of how the variables are calculated inside the template built by the portal, [subscription.SubscriptionId] was not going to work:

  "variables": {
    "vnetId": "[parameters('virtualNetworkId')]",
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]",

它在运行时以以下形式结束:

It ends up at run time as:

/subscriptions/$(subscription().subscriptionid)/resourceGroups/Dev1/providers/Microsoft.Network/virtualNetworks/vnet/subnets/default

我不想编辑模板,因为我需要对将来的部署进行一些调整,并且可能会使用门户网站来制作新模板.需要在构建管道中知道 subscriptionId .这也解决了 uniqueString 模板问题,我们需要了解 uniqueString ,但是 powershell 无法告诉您.>

I do not want to edit the template, as I will need to make some adjustments to future deployments, and will likely use the portal to make a new template. The subscriptionId needed to be known in the build pipeline. This also solves the uniqueString template issue, where we need to know the uniqueString, but powershell does not have a way to tell you.

推荐答案

解决方案:我创建了一个带有输出变量和空参数文件的空白模板: template.json:

Solution: I created a blank template with output variables and an empty parameters file: template.json:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {  },
  "variables": {  },
  "resources": [ ],
  "outputs": {
    "subscriptionId": {
      "condition": "[bool('true')]",
      "type": "string",
      "value": "[subscription().subscriptionId]"
    },
    "uniqueString": {
      "condition": "[bool('true')]",
      "type": "string",
      "value": "[uniqueString(resourceGroup().id)]"
    }
  }
}

parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  }
}

然后我运行任务:

1.Azure资源组部署->运行空模板

2.ARM输出->将ARM的输出捕获为变量:

2. ARM Outputs -> captures outputs from ARM as variables:

##[section]Starting: ARM Outputs
==============================================================================
Task         : ARM Outputs
Description  : This task reads the output values of an ARM deployment and sets them as Azure Pipelines variables.
Version      : 5.0.21
Author       : Kees Schollaart
Help         : [More Information](https://github.com/keesschollaart81/vsts-arm-outputs)
==============================================================================
1ef1317c-0917-4a7e-a922-a31b6069c707 exists true
Logging in using ApplicationTokenCredentials, authScheme is 'ServicePrincipal'
Updating Azure Pipelines variable 'subscriptionId'
Updating Azure Pipelines variable 'uniqueString'
##[section]Finishing: ARM Outputs

然后,我调用真正的 Azure资源组部署来运行我的实际模板.

Then I invoke the real Azure Resource Group Deployment to run my actual template.

现在,我可以使用管道语法(即$(parmName):

Now I can set the Override template parameters property using Pipeline Syntax, ie $(parmName):

-location eastus 
-enableAcceleratedNetworking true 
-subnetName default 
-virtualNetworkId /subscriptions/$(subscriptionId)/resourceGroups/$(resourceGroup)/providers/Microsoft.Network/virtualNetworks/vnet 
-diagnosticsStorageAccountId /subscriptions/$(subscriptionId)**/resourceGroups/$(resourceGroup)/providers/Microsoft.Storage/storageAccounts/osdiag$(uniqueString) 
...

这篇关于有没有一种方法可以获取任务中使用的SubscriptionID:"Azure资源组部署"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆