动态的应用程序设置阵列内容

发布于 2025-02-07 14:31:12 字数 2077 浏览 2 评论 0原文

我有以下简化的手臂模板(我遗漏了某些零件以使其更可读)。我有一个称为privateKeycertificateThumbPrint的参数。如果填写此参数,我想将appsetting wording_load_certificates设置为某个值。如果参数为空,我不想设置该值。因此,基于AppSettings数组中的元素是基于private keypercertificateThumbPrint parameter的内容的动态。

我似乎没有为此找到解决方案。用户酶似乎很简单。首先,我尝试添加其他microsoft.web/stites/config/appsettings资源,仅使用worges_load_certificates键。但是这样做可以从应用程序中删除所有已经存在的应用程序集。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    ...
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "The thumbprint of the client certificate used by the application"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "Location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": [
              {
                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                "value": "[if(empty(parameters('AppinsResourceName')), '', reference(resourceId('microsoft.insights/components/', parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)]"
              },
              {
                "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                "value": "~2"
              },
              {
                "name": "WEBSITE_HEALTHCHECK_MAXPINGFAILURES",
                "value": "5"
              },
              {
                "name": "WEBSITE_LOAD_CERTIFICATES",
                "value": "[parameters('PrivateKeyCertificateThumbprint')]"
              }
          ],
          "healthCheckPath": "[parameters('HealthCheckPath')]"
        }
        ...
    }
  ]
}

I have the following simplified ARM template (I left out certain parts to make it more readable). I have a parameter called PrivateKeyCertificateThumbprint. If this parameter is filled in, I want to set the appsetting WEBSITE_LOAD_CERTIFICATES to a certain value. If the parameter is empty, I do not want to set the value. So the elements in the appSettings array are sort of dynamic based on the content of the PrivateKeyCertificateThumbprint parameter.

I do not seem to find a solution for this in ARM. The usecase seems so simple. First I tried to add an additional Microsoft.Web/sites/config/appsettings resource with only the WEBSITE_LOAD_CERTIFICATES key. But doing that removes all the already existing appsettings from the application.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    ...
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "The thumbprint of the client certificate used by the application"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "Location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": [
              {
                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                "value": "[if(empty(parameters('AppinsResourceName')), '', reference(resourceId('microsoft.insights/components/', parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)]"
              },
              {
                "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                "value": "~2"
              },
              {
                "name": "WEBSITE_HEALTHCHECK_MAXPINGFAILURES",
                "value": "5"
              },
              {
                "name": "WEBSITE_LOAD_CERTIFICATES",
                "value": "[parameters('PrivateKeyCertificateThumbprint')]"
              }
          ],
          "healthCheckPath": "[parameters('HealthCheckPath')]"
        }
        ...
    }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2025-02-14 14:31:12

为了清楚起见,我使用了a bicep template
在这里,我定义了一个带有默认应用程序的变量。然后,如果CERT THUMBPRINT不是空的,我正在添加额外的应用程序设置:

param PrivateKeyCertificateThumbprint string = ''
param AppResourcename string
param Location string
param SitesKind string
param AppinsResourceName string

// Common app settings
var defaultAppSettings = [
  {
    name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
    value: (empty(AppinsResourceName) ? '' : reference(resourceId('microsoft.insights/components/', AppinsResourceName), '2015-05-01').InstrumentationKey)
  }
  {
    name: 'ApplicationInsightsAgent_EXTENSION_VERSION'
    value: '~2'
  }
  {
    name: 'WEBSITE_HEALTHCHECK_MAXPINGFAILURES'
    value: '5'
  }
]

// if the cert thumbprint is not empty, we add it.
var appSettings = concat(defaultAppSettings, empty(PrivateKeyCertificateThumbprint) ? [] : [
  {
    name: 'WEBSITE_LOAD_CERTIFICATES'
    value: PrivateKeyCertificateThumbprint
  }
])

// create the webapp
resource webApp 'Microsoft.Web/sites@2018-11-01' = {
  name: AppResourcename
  identity: {
    type: 'SystemAssigned'
  }
  location: Location
  kind: SitesKind
  properties: {
    siteConfig: {
      appSettings: appSettings
    }
  }
}

use az cli ,然后您可以生成手臂temnplate:

az bicep build --file .\main.bicep

生成的手臂模板看起来像:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": ""
    },
    "AppResourcename": {
      "type": "string"
    },
    "Location": {
      "type": "string"
    },
    "SitesKind": {
      "type": "string"
    },
    "AppinsResourceName": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": "[concat(createArray(createObject('name', 'APPINSIGHTS_INSTRUMENTATIONKEY', 'value', if(empty(parameters('AppinsResourceName')), '', reference(resourceId('microsoft.insights/components/', parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)), createObject('name', 'ApplicationInsightsAgent_EXTENSION_VERSION', 'value', '~2'), createObject('name', 'WEBSITE_HEALTHCHECK_MAXPINGFAILURES', 'value', '5')), if(empty(parameters('PrivateKeyCertificateThumbprint')), createArray(), createArray(createObject('name', 'WEBSITE_LOAD_CERTIFICATES', 'value', parameters('PrivateKeyCertificateThumbprint')))))]"
        }
      }
    }
  ]
}

For clarity, I used a bicep template.
Here I've defined a variable with default appsettings. Then if the cert thumbprint is not empty, I'm adding the extra app setting:

param PrivateKeyCertificateThumbprint string = ''
param AppResourcename string
param Location string
param SitesKind string
param AppinsResourceName string

// Common app settings
var defaultAppSettings = [
  {
    name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
    value: (empty(AppinsResourceName) ? '' : reference(resourceId('microsoft.insights/components/', AppinsResourceName), '2015-05-01').InstrumentationKey)
  }
  {
    name: 'ApplicationInsightsAgent_EXTENSION_VERSION'
    value: '~2'
  }
  {
    name: 'WEBSITE_HEALTHCHECK_MAXPINGFAILURES'
    value: '5'
  }
]

// if the cert thumbprint is not empty, we add it.
var appSettings = concat(defaultAppSettings, empty(PrivateKeyCertificateThumbprint) ? [] : [
  {
    name: 'WEBSITE_LOAD_CERTIFICATES'
    value: PrivateKeyCertificateThumbprint
  }
])

// create the webapp
resource webApp 'Microsoft.Web/sites@2018-11-01' = {
  name: AppResourcename
  identity: {
    type: 'SystemAssigned'
  }
  location: Location
  kind: SitesKind
  properties: {
    siteConfig: {
      appSettings: appSettings
    }
  }
}

Using Az CLI, you can then generate the ARM temnplate:

az bicep build --file .\main.bicep

the generated ARM template looks like that:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": ""
    },
    "AppResourcename": {
      "type": "string"
    },
    "Location": {
      "type": "string"
    },
    "SitesKind": {
      "type": "string"
    },
    "AppinsResourceName": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": "[concat(createArray(createObject('name', 'APPINSIGHTS_INSTRUMENTATIONKEY', 'value', if(empty(parameters('AppinsResourceName')), '', reference(resourceId('microsoft.insights/components/', parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)), createObject('name', 'ApplicationInsightsAgent_EXTENSION_VERSION', 'value', '~2'), createObject('name', 'WEBSITE_HEALTHCHECK_MAXPINGFAILURES', 'value', '5')), if(empty(parameters('PrivateKeyCertificateThumbprint')), createArray(), createArray(createObject('name', 'WEBSITE_LOAD_CERTIFICATES', 'value', parameters('PrivateKeyCertificateThumbprint')))))]"
        }
      }
    }
  ]
}
万人眼中万个我 2025-02-14 14:31:12

我终于设法通过使用功能使它在手臂上工作。我创建了3个功能,这些函数创建了3个带有参数的子数组,我只能在资源本身中获得的值。然后,我将Concat与IF结合使用,以有条件地将零件添加到资源中的设置数组中。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": 
    // ...
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "The thumbprint of the client certificate used by the application"
      }
    }
  },
  "variables": {
    // ...
  },
  "functions": [
    {
      "namespace": "test",
      "members": {
        "createAppSettings": {
          "parameters": [],
          "output": {
            "value": [
              {
                "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                "value": "~2"
              },
              {
                "name": "WEBSITE_HEALTHCHECK_MAXPINGFAILURES",
                "value": "5"
              }
            ],
            "type": "array"
          }
        },
        "createAppInsightSettings": {
          "parameters": [
            {
              "name": "instrumentationkey",
              "type": "string"
            }
          ],
          "output": {
            "value": [
              {
                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                "value": "[parameters('instrumentationkey')]"
              }
            ],
            "type": "array"
          }
        },
        "createCertificateAppSettings": {
          "parameters": [
            {
              "name": "certthumbprint",
              "type": "string"
            }
          ],
          "output": {
            "value": [
              {
                "name": "WEBSITE_LOAD_CERTIFICATES",
                "value": "[parameters('certthumbprint')]"
              }
            ],
            "type": "array"
          }
        }
      }
    }
  ],
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "Location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": "[concat(
            test.createAppSettings(),
            if(empty(parameters('AppinsResourceName')), createArray(), test.createAppInsightSettings(reference(resourceId('microsoft.insights/components/',parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)),
            if(empty(parameters('PrivateKeyCertificateThumbprint')), createArray(), test.createCertificateAppSettings(parameters('PrivateKeyCertificateThumbprint')))
          )]",
          "healthCheckPath": "[parameters('HealthCheckPath')]"
        }
        // ...
      }
    }
  ]
}

I finally managed to get it to work in ARM by using functions. I created 3 functions that created 3 sub arrays with as parameters, values I could only get in the resource itself. I then used a concat in combination with an if, to conditionally add parts to the settings array in the resource.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": 
    // ...
    "PrivateKeyCertificateThumbprint": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "The thumbprint of the client certificate used by the application"
      }
    }
  },
  "variables": {
    // ...
  },
  "functions": [
    {
      "namespace": "test",
      "members": {
        "createAppSettings": {
          "parameters": [],
          "output": {
            "value": [
              {
                "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                "value": "~2"
              },
              {
                "name": "WEBSITE_HEALTHCHECK_MAXPINGFAILURES",
                "value": "5"
              }
            ],
            "type": "array"
          }
        },
        "createAppInsightSettings": {
          "parameters": [
            {
              "name": "instrumentationkey",
              "type": "string"
            }
          ],
          "output": {
            "value": [
              {
                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                "value": "[parameters('instrumentationkey')]"
              }
            ],
            "type": "array"
          }
        },
        "createCertificateAppSettings": {
          "parameters": [
            {
              "name": "certthumbprint",
              "type": "string"
            }
          ],
          "output": {
            "value": [
              {
                "name": "WEBSITE_LOAD_CERTIFICATES",
                "value": "[parameters('certthumbprint')]"
              }
            ],
            "type": "array"
          }
        }
      }
    }
  ],
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('AppResourcename')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "Location": "[parameters('Location')]",
      "kind": "[parameters('SitesKind')]",
      "properties": {
        "siteConfig": {
          "appSettings": "[concat(
            test.createAppSettings(),
            if(empty(parameters('AppinsResourceName')), createArray(), test.createAppInsightSettings(reference(resourceId('microsoft.insights/components/',parameters('AppinsResourceName')), '2015-05-01').InstrumentationKey)),
            if(empty(parameters('PrivateKeyCertificateThumbprint')), createArray(), test.createCertificateAppSettings(parameters('PrivateKeyCertificateThumbprint')))
          )]",
          "healthCheckPath": "[parameters('HealthCheckPath')]"
        }
        // ...
      }
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文