gpt4 book ai didi

azure - 使用 Azure Devops Pipeline 将标签(带空格)传递到 ARM 模板

转载 作者:行者123 更新时间:2023-12-04 09:10:09 27 4
gpt4 key购买 nike

我正在使用 Azure DevOps Pipelines 部署 ARM 模板。我的模板有一个标签参数,我使用 AzureResourceManagerTemplateDeployment@3 将其传递到管道中。

我的 ARM 模板在参数部分有一个作为对象的值。 tags 是一个对象,这是许多示例模板所显示的内容:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "string",
"metadata": {
"description": "Specifies the name of the resource, including its prefix."
}
},
"tags": {
"type": "object",
"defaultValue": {
"Cost Center": "Admin"
}
}
},
"resources": [
{
"apiVersion": "2019-06-01",
"kind": "StorageV2",
"location": "[resourceGroup().location]",
"name": "[parameters('resourceName')]",
"properties": {
"supportsHttpsTrafficOnly": true
},
"sku": {
"name": "Standard_LRS"
},
"type": "Microsoft.Storage/storageAccounts",
"tags": "[parameters('tags')]"
}
]
}

[已编辑以匹配随后的线程]

我的池使用ubuntu-latest。标签可能有空格。

在我的管道中,为了简单起见,我将标签设置为变量。

pool:
vmImage: 'ubuntu-latest'
variables:
- name: tags
value: ("Location Region=West US 2" "Environment=${{ parameters.environment }}")

当我调用模板部署时,我将标签作为 overrideParameters 传递

  - task: AzureResourceManagerTemplateDeployment@3
displayName: "Deploy my templateaccount"
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'ResourceManager-connection'
subscriptionId: ${{ parameters.subscriptionid }}
action: 'Create Or Update Resource Group'
resourceGroupName: '$(resourceGroupName)'
location: '${{ parameters.location }}'
templateLocation: 'Linked artifact'
csmFile: 'mytemplatelocation/azuredeploy.json'
overrideParameters: -resourceName abcdefg76534 -tags "$(tags)"
deploymentMode: 'Incremental'
deploymentOutputs: resourceOutput
- pwsh: Write-Output '$(resourceOutput)'

到目前为止,我还不明白 Ubuntu 上的 AzureResourceManagerTemplateDeployment@3 期望如何发送标签。

在每种情况下,模板都无法部署。

Azure DevOps Pipeline 支持此场景吗?

有人有建议吗?

最佳答案

在 Azure DevOps 管道 AzureResourceManagerTemplateDeployment@3 中使用的标签格式是对 ARM 模板对象(例如标签)使用 JSON。

  • 左大括号,冒号分隔键值对,逗号分隔标签。
  • 在我的例子中,每个键和值都使用引号。
  • 右大括号。

但以下模板通过传入 JSON 对象来工作:{"Cost Center":"DevTest","Location":"West US"} 作为模板参数。在上下文中,这看起来像:

- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'ResourceManager-connection'
subscriptionId: 'XXXXX'
action: 'Create Or Update Resource Group'
resourceGroupName: 'rg-wus2-exampletest'
location: 'West US 2'
templateLocation: 'Linked artifact'
csmFile: 'storageaccount/example.azuredeploy.json'
csmParametersFile: 'storageaccount/azuredeploy.parameters.json'
overrideParameters: '-resourceName oweruhsow -resourceTags {"Cost Center":"DevTest","Location":"West US"}'
deploymentMode: 'Complete'

此管道模块需要 JSON 对象,而不是在其他地方使用 PowerShell ( https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resource ) 从命令行部署的相同格式。

另外,作为旁注,其他帖子建议您使用 tags 以外的名称作为标记参数。对我有用的是resourceTags。这是我的 ARM 模板:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "string",
"metadata": {
"description": "Specifies the name of the resource"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for the resources."
}
},
"resourceTags": {
"type": "object",
"defaultValue": {
"Cost Center": "Admin"
}
}
},
"resources": [
{
"apiVersion": "2019-06-01",
"kind": "StorageV2",
"location": "[parameters('location')]",
"name": "[parameters('resourceName')]",
"properties": {
"supportsHttpsTrafficOnly": true
},
"sku": {
"name": "Standard_LRS"
},
"type": "Microsoft.Storage/storageAccounts",
"tags": "[parameters('resourceTags')]"
}
]
}

如果要将模板对象设置为变量,则可以使用 DevOps 变量将其传递,例如 $(tags):

variables:
tags: '{"Cost Center":"DevTest","Location":"West US"}'
steps:
- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'ResourceManager-connection'
subscriptionId: '9f241d6e-16e2-4b2b-a485-cc546f04799b'
action: 'Create Or Update Resource Group'
resourceGroupName: 'rg-wus2-exampletest'
location: 'West US 2'
templateLocation: 'Linked artifact'
csmFile: 'storageaccount/example.azuredeploy.json'
csmParametersFile: 'storageaccount/azuredeploy.parameters.json'
overrideParameters: '-resourceName oweruhso11w -resourceTags $(tags)'
deploymentMode: 'Complete'

另外(作为旁注),由于某种原因,模块需要有一个 csmParametersFile ,否则它会失败,并出现 RESOURCEGROUP 全部大写失败的情况。从命令行部署不需要参数文件,但管道模块似乎需要它。一个 csmParamters 文件,里面几乎什么都没有,但似乎是需要的。

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

这也可以使用pool image: windows-latest

非常感谢 ToMakesSense 在 https://github.com/MicrosoftDocs/azure-devops-docs/issues/9051 的帖子中

关于azure - 使用 Azure Devops Pipeline 将标签(带空格)传递到 ARM 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63364524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com