6

我们希望通过 Azure 云上的 Terraform 部署我们的基础架构。我要应用的代码使用 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 参数设为整数?

4

1 回答 1

10

不幸的是 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')]"
        }
    }
}
于 2019-02-04T09:40:47.827 回答