问:我可以插入整数/数字参数代码为Terraform Azure的JSON模板代码?

问题描述 投票:2回答:1

我们希望我们的部署通过Terraform在Azure云计算基础设施。我想申请该代码使用Azure的本身造成的JSON模板代码。

代码(洗涤和除去不重要JSON代码):

resource "azurerm_resource_group" "docker" {
  name     = "CSI-DockerSwarm"
  location = "West Europe"
}

 resource "azurerm_template_deployment" "Docker" {
   name                = "Example-Deployment"
   resource_group_name = "${azurerm_resource_group.docker.name}"

    template_body = <<DEPLOY        # JSON beginning
    {
     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
     "contentVersion": "1.0.0.0",
     "parameters": {
       "agentCount": {
         "allowedValues": [
           1,
           2,
           3,
           4,
         ],
         "defaultValue": 2,
         "metadata": {
            "description": "The number of agents for the cluster. This value can be from 1 to 100"
         },
         "type": "int"
        },

    DEPLOY      # JSON ending

      # these key-value pairs are passed into the ARM Template's `parameters` block
      parameters {
        "agentCount" = "3"          # Should be a integer/number?
        "agentEndpointDNSNamePrefix" = "-secret-"
        "agentSubnet" = "10.0.0.0/16"
        "agentVMSize" = "Standard_D2_v2"
        "firstConsecutiveStaticIP" = "172.16.0.5"
        "linuxAdminUsername" = "-secret-"
        "masterEndpointDNSNamePrefix" = "-secret-"
        "masterSubnet" = "172.16.0.0/24"
        "masterVMSize" = "Standard_D2_v2"
        "sshRSAPublicKey" = "-secret-"
        "targetEnvironment" = "AzurePublicCloud"
      }

      deployment_mode = "Incremental"
    }

问题

agentCount参数值不起作用。

错误

Error: Error applying plan:

1 error(s) occurred:

* azurerm_template_deployment.Docker: 1 error(s) occurred:

* azurerm_template_deployment.Docker: Error creating deployment: resources.DeploymentsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidTemplate" Message="Deployment template validation failed: 'The provided value for the template parameter 'agentCount' at line '1' and column '494' is not valid.'."

问:我怎样才能使“agentCount” JSON参数的整数?

json azure terraform hcl
1个回答
4
投票

不幸的是terraform不能传递整数参数。我们通过所有参数为字符串,然后转换那些像整型变量:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToConvert": { 
            "type": "string",
            "defaultValue": "4"
        }
    },
    "variables": {
        "integerFromString": "[int(parameters('stringToConvert'))]"
    }
    "resources": [],
    "outputs": {
        "intResult": {
            "type": "int",
            "value": "[variables('integerFromString')]"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.