【问题标题】:ARM Template Deployment: DeploymentNotFound Error When Using reference() function for Conditional DeploymentARM 模板部署:使用 reference() 函数进行条件部署时出现 DeploymentNotFound 错误
【发布时间】:2021-07-09 21:22:08
【问题描述】:

我有一个主 ARM 模板,我们称之为 azuredeploy.json,还有一个链接模板,我们称之为 privateEndpoint.json。我有一个决定是否部署资源的参数,我们称之为enablePrivateEndpoints。它当前设置为“false”,因此不会发生此资源的部署。

在相反的情况下,应读取“privateEndpoint.json”的输出以获取专用端点的 networkInterface id。

azuredeploy.json

{
            "condition": "[not(equals(parameters('enablePrivateEndpoints'), 'false'))]",
            "name": "keyvault-privateendpoint",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2019-10-01",
            "dependsOn": [
                "keyvault"
            ],
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                "uri": "[concat(variables('templateFolderUrl'), '/', variables('privateEndpointTemplateFileName'), parameters('_artifactsLocationSasToken'))]"
                },
                "parameters": {
                ...
...
                "enablePrivateEndpoints": "[parameters('enablePrivateEndpoints')]"
                }
            }
        },
},
{
            "condition": "[not(equals(parameters('enablePrivateEndpoints'), 'false'))]",
            "name": "networkInterface-kv",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2019-10-01",
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                "uri": "[concat(variables('templateFolderUrl'), '/', variables('networkInterfaceTemplateFileName'), parameters('_artifactsLocationSasToken'))]"
                },
                "parameters": {
                    "networkInterfaceId": {
                        "value": "[reference('keyvault-privateendpoint', '2021-04-01').outputs.networkInterfaceId.value]"
                    }
                }
            }
        }

privateEndpoint.json

...
"enablePrivateEndpoints": {
      "type": "string"
    }
    },
    "variables": {},    
    "resources": [
        {
            "name": "[parameters('privateEndpointName')]",
            "type": "Microsoft.Network/privateEndpoints",
            "apiVersion": "2021-04-01",
            "location": "[resourceGroup().location]",
            "dependsOn": [],
            "properties": "[parameters('privateEndpointProperties')]"
        }
    ],
    "outputs": {
      "networkInterfaceId": {
        "condition": "[not(equals(parameters('enablePrivateEndpoints'), 'false'))]",
        "type": "string",
        "value": "[reference(resourceId('Microsoft.Network/privateEndpoints', parameters('privateEndpointName'))).networkInterfaces[0].id]"
      }
    }

问题在于这段代码:

"networkInterfaceId": {
                        "value": "[reference('keyvault-privateendpoint', '2021-04-01').outputs.networkInterfaceId.value]"
                    }

尽管条件停止了引用资源的部署,但仍在评估引用函数。部署失败并显示 “DeploymentNotFound: The deployment 'keyvault-privateendpoint' could not be found”

以下是我尝试解决此问题的一些方法:

尝试 #1

您可以看到我已经尝试向输出本身添加条件

"outputs": {
      "networkInterfaceId": {
        "condition": "[not(equals(parameters('enablePrivateEndpoints'), 'false'))]",
        "type": "string",
        "value": "[reference(resourceId('Microsoft.Network/privateEndpoints', parameters('privateEndpointName'))).networkInterfaces[0].id]"
      }
    }

这并没有解决问题

尝试 #2

我尝试将 if 添加到正在查看输出本身的值,仅在 enablePrivateEndpoints 参数为 true 时评估参考函数。

"networkInterfaceId": {
                        "value": "[if(parameters(enablePrivateEndpoints), reference('keyvault-privateendpoint', '2021-04-01').outputs.networkInterfaceId.value, '')]"
                    }

这也没有解决问题。

我不明白为什么仍在尝试引用不存在的部署。

我的意思是即使是进行引用的部署资源本身也是有条件的。

注意:这些资源还没有成功部署,所以是新的。

【问题讨论】:

    标签: azure azure-resource-manager arm-template


    【解决方案1】:

    尝试使用

    "networkInterfaceId": 
    {
                        
    "value": "[if(not(equals(parameters('enablePrivateEndpoints'), 'false')), reference('keyvault-privateendpoint', '2021-04-01').outputs.networkInterfaceId.value , '')]"
    }
    

    在资源"type": "Microsoft.Resources/deployments"的参数中。

    【讨论】:

      【解决方案2】:

      我发现了问题所在。使用条件部署时,这并不意味着不会评估引用函数,因为它是运行时函数。

      如果您不希望在部署时进行评估,则必须在条件部署中使用参考函数的任何位置添加额外的 if

      这包括输出。

      见下文,链接模板的输出现在有一个 if: privateEndpoint.json

      "networkInterfaceId": {
              "condition": "[equals(parameters('enablePrivateEndpoints'), 'true')]",
              "type": "string",
              "value": "[if(equals(parameters('enablePrivateEndpoints'), 'true'), reference(resourceId('Microsoft.Network/privateEndpoints', parameters('privateEndpointName'))).networkInterfaces[0].id, '1')]"
      

      然后在主部署arm模板中,引用函数再次放在if后面: azuredeploy.json

      "networkInterfaceId": {
                              "value": "[if(equals(parameters('enablePrivateEndpoints'), 'true'), reference('keyvault-privateendpoint', '2021-04-01').outputs.networkInterfaceId.value, '1')]"
                          }
      

      【讨论】:

        猜你喜欢
        • 2021-04-10
        • 1970-01-01
        • 2022-01-09
        • 2019-11-06
        • 1970-01-01
        • 2022-08-20
        • 1970-01-01
        • 2021-07-28
        • 2019-02-11
        相关资源
        最近更新 更多