无法对 arm 模板中的 Microsoft.Resources/deploymentScripts 使用 concat 解析参数

时间:2021-05-17 17:18:50

标签: azure arm-template

我有一个带有 Microsoft.Resources/deploymentScripts 的 arm 模板,它使用随机生成的名称解析一个公共 IP 地址的资源 ID。此 IP 地址由 AKS 集群创建,然后安装 Nginx 入口控制器。它有一个随机生成的名称,也是正确捕获它的唯一方法——使用分配给 IP 地址的特定标签。要获取其资源 ID,需要另一个参数 - 资源组的名称,该名称由 AKS 在群集创建期间自动创建。这里的问题 - 部署脚本似乎无法正确解析此参数值:

"ResourceGroup": {
      "value": "[concat('MC_', resourceGroup().name, '_', parameters('Cluster'), '_', resourceGroup().location)]"
    }

incorrect resolving

部署脚本的运行失败并出现以下错误:

The provided script failed with the following error: System.Management.Automation.ParseException: At line:1 char:38 + . .\userscript.ps1 -RG [concat('MC_', resourceGroup().name, '_', para … + ~ Missing expression after ','. At line:1 char:39 + . .\userscript.ps1 -RG [concat('MC_', resourceGroup().name, '_', para … + ~~~~~~~~~~~~~ Unexpected token 'resourceGroup' in expression or statement. At line:1 char:38 + . .\userscript.ps1 -RG [concat('MC_', resourceGroup().name, '_', para … + ~ Missing closing ')' in expression. At line:1 char:53 + . .\userscript.ps1 -RG [concat('MC_', resourceGroup().name, '_', para … + ~ An expression was expected after '('. At line:1 char:108 + … Group().name, '_', parameters('Cluster'), '_', resourceGroup().locati … + ~ An expression was expected after '('. At line:1 char:118 + … p().name, '_', parameters('Cluster'), '_', resourceGroup().location)] + ~ Unexpected token ')' in expression or statement. at System.Management.Automation.ScriptBlock.Create(Parser parser, String fileName, String fileContents) at System.Management.Automation.ScriptBlock.Create(ExecutionContext context, String script) at System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock(String scriptText) at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord() at System.Management.Automation.Cmdlet.DoProcessRecord() at System.Management.Automation.CommandProcessor.ProcessRecord() at <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 220. Please refer to https://aka.ms/DeploymentScriptsTroubleshoot for more deployment script information. (Code: DeploymentScriptError)

使用的以下臂和参数文件:

模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "ResourceGroup": {
      "type": "string"
    },
    "Location": {
      "type": "string"
    },
    "Cluster": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deploymentScripts",
      "apiVersion": "2020-10-01",
      "name": "parsingIngressId",
      "location": "[resourceGroup().location]",
      "kind": "AzurePowerShell",
      "identity": {
        "type": "userAssigned",
        "userAssignedIdentities": {
          "/subscriptions/xxxxxxxxxx/resourceGroups/xxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx": {
          }
        }
      },
      "properties": {
        "azPowerShellVersion": "5.0",
        "arguments": "[concat('-RG ', parameters('ResourceGroup'))]",
        "scriptContent": "
         param( [string]$RG)
         Write-Host $RG
         $DeploymentScriptOutputs = @{ }
         $ParsingId = Get-AzPublicIpAddress -ResourceGroupName $RG | Where-Object { $_.Tag['service'] -like '*ingress-controller*' -and $_.Tag['service']} | Select-Object Id
         $DeploymentScriptOutputs['id'] = $ParsingId.id
          ",
        "timeout": "PT1H",
        "cleanupPreference": "OnSuccess",
        "retentionInterval": "P1D"
      }
    }
  ],
  "outputs": {
    "id": {
      "value": "[reference('parsingIngressId').outputs.id]",
      "type": "string"
    }
  }
}

参数:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
  "parameters": {
    "ResourceGroup": {
      "value": "[concat('MC_', resourceGroup().name, '_', parameters('Cluster'), '_', resourceGroup().location)]"
    },
    "Location": {
      "value": "resourceGroup().location)"
    },
    "Cluster": {
      "value": "ClusterName"
    }
  }
}

任何想法\建议我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在尝试使用参数文件中另一个参数的值解析一个参数。

为模板文件中的资源组使用默认值并在那里解析参数。例如:

  "parameters": {
    "ResourceGroup": {
      "type": "string",
      "defaultValue": "[concat('MC_', resourceGroup().name, '_', parameters('Cluster'), '_', resourceGroup().location)]"
    },