简体   繁体   中英

Pass tags (with spaces) to an ARM template using Azure Devops Pipeline

I am using Azure DevOps Pipelines to deploy an ARM template. My template has a tags parameter that I pass into the pipeline using AzureResourceManagerTemplateDeployment@3 .

My ARM template has a value in parameters section as an object. tags is an object, which is what many of the sample templates show:

{
  "$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')]"
    }
  ]
}

[Edited to match the thread that follows]

I am using ubuntu-latest for my pool. Tags may have spaces.

In my pipeline for simplicity , I set the tags to a variable.

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

When I call the template deployment, I am passing in the tags as an 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)'

So far I have not understood how AzureResourceManagerTemplateDeployment@3 on Ubuntu is expecting the tags to be sent.

In each case, the template fails to deploy.

Is this scenario supported in Azure DevOps Pipeline?

Anyone with a suggestion?

The format for the tags that worked in Azure DevOps pipeline AzureResourceManagerTemplateDeployment@3 is to use JSON for your ARM Template objects, such as tags.

  • Left curly bracket, colon separating the key value pairs, and comma separating the tags.
  • Each key and the value in my case worked with quotes.
  • A right curly bracket.

But following templates works by passing in a JSON object: {"Cost Center":"DevTest","Location":"West US"} as your template parameter. In context this looks like:

- 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'

This Pipeline module is expecting JSON objects, not the same format used elsewhere with PowerShell ( https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resource ) to deploy from the command line.

Also as a side note, other posts suggested that you name use something other than tags for your tags parameter. The one that worked for me is resourceTags . Here is my ARM template:

{
  "$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')]"
    }
  ]
}

If you want to set your template object as a the variable, you then can pass it in using the DevOps variable such as $(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'

Also (as a side note), for some reason, the module needed to have a csmParametersFile or it would just fail with something about failing with RESOURCEGROUP in all caps. The param file is not needed to deploy from the command line, but Pipelines modules does seem to require it. A csmParamters file that can have just about nothing in it, but it seems to be needed.

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

This also works using the pool image: windows-latest

Many thanks to ToMakesSense in the post at https://github.com/MicrosoftDocs/azure-devops-docs/issues/9051

If the parameter value you're using has multiple words, enclose them in quotes, even if you're passing them using variables.

For example, -name "parameter value" -name2 "$(var)".

Try to use below format:

overrideParameters: -location "${{ parameters.location }}" -tags "$(tags)"

More details please refer our official doc here-- Azure Resource Group Deployment task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM