能否在Terraform的Azure JSON模板参数代码中插入整数/数字?
Absolutely! You can absolutely use integer/numeric parameters in your Azure JSON (ARM) template when deploying via Terraform—here's how to do it smoothly:
Step 1: Define integer parameters in your ARM template
First, declare your integer parameter directly in the JSON template'sparametersblock. You can set a default value and description for clarity:{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "workerNodeCount": { "type": "int", "defaultValue": 2, "metadata": { "description": "Number of Docker Swarm worker nodes to deploy" } } }, "resources": [ // Use the parameter in your resource definitions, e.g., for loops or resource properties: // "[parameters('workerNodeCount')]" ] }Step 2: Pass integer values from Terraform
In yourazurerm_template_deploymentresource, you can either use the template's default integer value or override it via theparametersblock. Terraform handles numeric values natively here, no extra formatting needed: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 { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "workerNodeCount": { "type": "int", "defaultValue": 2 } }, "resources": [ // Your resource definitions using the integer parameter go here ] } DEPLOY // Override the default integer value if needed parameters = { workerNodeCount = 4 // Directly pass an integer here } }Bonus: Use Terraform variables for dynamic integers
If you want to make the value configurable in your Terraform setup, define a numeric variable and reference it:variable "swarm_worker_count" { type = number description = "Number of Docker Swarm worker nodes to provision" default = 2 } // Then in your template deployment: parameters = { workerNodeCount = var.swarm_worker_count }
This approach keeps your setup flexible, follows ARM template best practices, and plays nicely with Terraform's type system.
内容的提问来源于stack exchange,提问作者U.Durk

