Azure DevOps YAML流水线动态传入Terraform Tags字典参数不生效问题求助
各位大佬好,我目前在做一个通过Azure DevOps YAML流水线部署Terraform管理的Azure VM的项目,我的Terraform代码有main.tf和var.tf,希望能动态传入符合如下格式的Tags字典:
"Tags": {"Application": "Marklogic", "CostCenter": "CoE", "Environment": "Test", "Support": "xyz.com", "AvailabilityTime": "24*7", "Owner": "xyza@.com", "BU": "CIS"}
但现在遇到的问题是:当我通过Postman传递自定义的Tags值时,流水线始终使用参数里的默认Tags,完全不替换我传入的动态值,而且默认值应该不是必填项,应该能支持传入任意格式的合法Tags字典。我怀疑是语法哪里写错了,麻烦帮忙排查解决下,提前谢过大家!
我的Azure DevOps YAML流水线代码
trigger: none ######################### # Declare Build Agents:- ######################### pool: vmImage: ubuntu-latest ###################### #DECLARE PARAMETERS:- ###################### parameters: - name: ResourceGroup displayName: Please Provide the Resource Group Name:- type: string default: <Please provide the required RG Name> - name: Location displayName: Please Provide the Location:- type: string default: <Please provide the required location> - name: Virtual_Network displayName: Please Provide the Virtual_Network Name:- type: object default: <Please provide the required Name> - name: Subnet displayName: Please Provide the subnet name:- type: object default: <Please provide the required Name> - name: Public_ip displayName: Please Provide the IP Name:- type: object default: <Please provide the required Name> - name: Dns displayName: Please Provide the Dns name:- type: object default: <Please provide the required Name> - name: Network_Security_Group displayName: Please Provide the NSG Name:- type: object default: <Please provide the required Name> - name: Network_Interface displayName: Please Provide the NIC name:- type: object default: <Please provide the required Name> - name: Virtual_Machine_Name displayName: Please Provide the VM Name:- type: object default: <Please provide the required Name> - name: Size displayName: Please Provide the Size of vm:- type: object default: <Please provide the required Name> - name: Username displayName: Please Provide the UserName:- type: object default: <Please provide the required Name> - name: Password displayName: Please Provide the password:- type: object default: <Please provide the required Name> - name: Tags displayName: Tags type: object default: | { "Application": "C360", "CostCenter": "test", "Environment": "QA", "Support": "CIS-Cloud_support", "AvailabilityTime": "test", "Owner": "xyth@.com", "BU": "CIS" } ###################### #DECLARE VARIABLES:- ###################### variables: TF_VAR_RESOURCEGROUP: ${{ parameters.ResourceGroup }} TF_VAR_LOCATION: ${{ parameters.Location }} TF_VAR_VIRTUAL_NETWORK: ${{ parameters.Virtual_Network }} TF_VAR_SUBNET: ${{ parameters.Subnet }} TF_VAR_PUBLIC_IP: ${{ parameters.Public_ip }} TF_VAR_DNS: ${{ parameters.Dns }} TF_VAR_NETWOK_SECURITY_GROUP: ${{ parameters.Network_Security_Group }} TF_VAR_NETWORK_INTERFACE: ${{ parameters.Network_Interface }} TF_VAR_VIRTUAL_MACHINE_NAME: ${{ parameters.Virtual_Machine_Name }} TF_VAR_SIZE: ${{ parameters.Size }} TF_VAR_USERNAME: ${{ parameters.Username }} TF_VAR_PASSWORD: ${{ parameters.Password }} TF_VAR_TAGS: ${{ parameters.Tags }} ################### # Declare Stages:- ################### stages: - stage: tfvalidate jobs: - job: validate variables: - group: AzureDevopsCred continueOnError: false steps: - task: TerraformInstaller@0 inputs: terraformVersion: 'latest' - task: TerraformTaskV3@3 displayName: init inputs: provider: 'azurerm' command: 'init' workingDirectory: 'LaunchStack/terraform/azure/testmarklogic' backendServiceArm: 'SaaS_Automationaccount_OIDC_TEST' backendAzureRmResourceGroupName: $(ResourceGroup) backendAzureRmStorageAccountName: $(StorageAccount) backendAzureRmContainerName: $(Container) backendAzureRmKey: $(TfstateFile) - stage: tfdeploy condition: succeeded('tfvalidate') dependsOn: tfvalidate jobs: - job: apply variables: - group: AzureDevopsCred steps: - task: TerraformInstaller@0 inputs: terraformVersion: 'latest' - task: TerraformTaskV3@3 displayName: init inputs: provider: 'azurerm' command: 'init' workingDirectory: 'LaunchStack/terraform/azure/testmarklogic' backendServiceArm: 'SaaS_Automationaccount_OIDC_TEST' backendAzureRmResourceGroupName: $(ResourceGroup) backendAzureRmStorageAccountName: $(StorageAccount) backendAzureRmContainerName: $(Container) backendAzureRmKey: $(TfstateFile) - task: TerraformTaskV3@3 displayName: validate inputs: provider: 'azurerm' command: 'validate' workingDirectory: 'LaunchStack/terraform/azure/testmarklogic' - task: TerraformTaskV3@3 displayName: plan inputs: provider: 'azurerm' command: 'plan' workingDirectory: 'LaunchStack/terraform/azure/testmarklogic' - task: TerraformTaskV3@3 displayName: apply inputs: provider: 'azurerm' command: 'apply' workingDirectory: 'LaunchStack/terraform/azure/testmarklogic' commandOptions: '-auto-approve' environmentServiceNameAzureRM: 'SaaS_Automationaccount_OIDC_TEST'
Terraform var.tf代码
variable "RESOURCEGROUP" { type = string } variable "LOCATION" { type = string } variable "VIRTUAL_NETWORK" { type = string } variable "SUBNET" { type = string } variable "PUBLIC_IP" { type = string } variable "DNS" { type = string } variable "TAGS" { type = map(string) }
Terraform main.tf代码(关键部分)
resource "azurerm_linux_virtual_machine" "machine" { name = var.VIRTUAL_MACHINE_NAME resource_group_name = azurerm_resource_group.windows.name location = azurerm_resource_group.windows.location size = var.SIZE admin_username = var.USERNAME admin_password = var.PASSWORD network_interface_ids = [ azurerm_network_interface.windows.id, ] depends_on = [ azurerm_network_interface.windows ] plan { publisher = "marklogic" product = "marklogic-developer-9" name = "ml9011-centos" } os_disk { name = "myOsDisk" caching = "ReadWrite" storage_account_type = "Standard_LRS" } tags = var.TAGS source_image_reference { publisher = "marklogic" offer = "marklogic-developer-9" sku = "ml9011-centos" version = "latest" } computer_name = "redhatVM" disable_password_authentication = false }
备注:内容来源于stack exchange,提问作者DSH
相关产品推荐
相关产品推荐

