【问题标题】:Logic App: Pass dynamic subscription id and resource group name in an ARM template逻辑应用:在 ARM 模板中传递动态订阅 ID 和资源组名称
【发布时间】:2021-05-10 17:31:09
【问题描述】:

嘿,我尝试使用 Azure DevOps 部署具有动态资源组名称和订阅 ID 的第一个逻辑应用程序已经有几天了,我查看了几个链接,这些链接在我的 template.json 文件中执行类似操作:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workflows_app_name": {
            "defaultValue": "myLogicApp",
            "type": "String"
        },
        "connections_azuretables_externalid": {
            "value": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/connections/azuretables",
            "type": "String"
        }
....

也试过了:

 "connections_azuretables_externalid": {
            "defaultValue": "[concat('/subscriptions/', subscription().id, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Web/connections/azuretables')]",
            "type": "String"
        }

这也试图在参数中传递资源组名称和订阅:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workflows_app_name": {
            "defaultValue": "myLogicApp",
            "type": "String"
        },
        "connections_azuretables_externalid": {
            "defaultValue": "[concat('/subscriptions/', parameters('subscriptionId'), '/resourceGroups/', parameters('ressourceGroupName'), '/providers/Microsoft.Web/connections/azuretables')]",
            "type": "String"
        },
        "subscriptionId": {
          "type": "String"
        },
        "ressourceGroupName": {
          "type": "String"
        },
.....
}

但出现类似的错误

属性 id '[concat('/subscriptions/', parameters('subscriptionId'), '/resourceGroups/', parameters('ressourceGroupName'), '/providers/Microsoft.Web/connections/azuretables')]'路径“properties.parameters.$connections.value.azuretables.connectionId”无效。期望以“/subscriptions/{subscriptionId}”或“/providers/{resourceProviderNamespace}/”开头的完全限定资源 ID

我知道我遗漏或误解了某些东西,但无法弄清楚!谢谢。

【问题讨论】:

    标签: azure-devops azure-logic-apps arm-template


    【解决方案1】:

    您可以在 template.json 文件的属性部分添加 API 属性,如下所示,然后在 DevOps 库中引用变量名称

    "api": {
             "id": "[concat(subscription().id,'/providers/Microsoft.Web/locations/',variables('rgLocation'),'/managedApis/azureblob')]"
           }
    

    在这种情况下,存储位置是 Blob,您可以将其自定义为您选择的存储

    【讨论】:

    • 感谢您的回答,但重点是不要在Library中创建变量,我想要的是在部署它时将替换它已部署的当前资源组名称,就像subscription.id( ),我怎样才能做到这一点,因为我发现的链接对我不起作用。
    • 我怀疑这甚至可能直接因为您必须指定参数值。您能否尝试将 PowerShell 代码添加到您的发布管道,以便该代码在运行时获取资源组/订阅 ID?
    • 我不知道如何操作powershell代码来获取资源组,您可以添加参考如何操作吗?因为这是我第一次部署 ARM 模板
    • 您可以参考以下链接:1.stackoverflow.com/questions/61679470/… 2.docs.microsoft.com/en-us/powershell/module/az.resources/….您可以使用 Login-AzureAzAccount 通过您的凭据登录并获取订阅详细信息
    • 嗨@mar​​One:如果我的回复对您有所帮助,您能否将其标记为答案,以便我们可以让社区中的其他人从中受益:)。谢谢!
    【解决方案2】:

    逻辑应用:在 ARM 模板中传递动态订阅 ID 和资源组名称

    您可以尝试添加 Azure Cli 任务以获取订阅并创建一个名为 subscriptionId 的管道变量:

      jobs:
      - job: GetSubscription
        steps:
        - checkout: none
        - task: AzureCLI@2
          inputs:
            azureSubscription: 'CTP.AssuranceDW-Non-Production'
            scriptType: 'ps'
            scriptLocation: 'inlineScript'
            inlineScript: |
              $id = convertfrom-json (az account list --query "[?isDefault].id | [0]")
              Write-Host $id
              echo "##vso[task.setvariable variable=subscriptionId]$id"
        - powershell: Write-Host $(subscriptionId)
    

    然后,我们可以使用overrideParameters 或replacetokens 任务将此值传递给ARM 模板。

    请查看线程How to pass an Azure pipeline variable to an ARM template used by AzureResourceManagerTemplateDeployment@3 task?了解更多详情。

    【讨论】:

    • 谢谢 Leo,我会尽快尝试您的解决方案
    • @marOne,你有时间检查一下吗?只是提醒this
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多