Skip to content

Instantly share code, notes, and snippets.

@bailantaotao
Last active January 20, 2018 15:29
Show Gist options
  • Save bailantaotao/18584ebc2ae0e6df1e9e59296274a496 to your computer and use it in GitHub Desktop.
Save bailantaotao/18584ebc2ae0e6df1e9e59296274a496 to your computer and use it in GitHub Desktop.
A example of template_file using Terraform

Using terraform_file

$ terraform init
$ terraform plan
$ terraform apply

$ cat output.json // you can see any variable have been replaced with Vars

{
  "apiVersion": "vlabs",
  "properties": {
    "orchestratorProfile": {
      "orchestratorType": "Kubernetes"
    },
    "masterProfile": {
      "count": 1,
      "dnsPrefix": "edwin-k8s",
      "vmSize": "Standard_D2_v2"
    },
    "agentPoolProfiles": [
      {
        "name": "agentpool1",
        "count": 3,
        "vmSize": "Standard_D2_v2",
        "availabilityProfile": "AvailabilitySet"
      }
    ],
    "linuxProfile": {
      "adminUsername": "azureuser",
      "ssh": {
        "publicKeys": [
          {
            "keyData": "key_data"
          }
        ]
      }
    },
    "servicePrincipalProfile": {
      "clientId": "client_id",
      "secret": "secret"
    }
  }
}
{
"apiVersion": "vlabs",
"properties": {
"orchestratorProfile": {
"orchestratorType": "Kubernetes"
},
"masterProfile": {
"count": 1,
"dnsPrefix": "edwin-k8s",
"vmSize": "Standard_D2_v2"
},
"agentPoolProfiles": [
{
"name": "agentpool1",
"count": 3,
"vmSize": "Standard_D2_v2",
"availabilityProfile": "AvailabilitySet"
}
],
"linuxProfile": {
"adminUsername": "azureuser",
"ssh": {
"publicKeys": [
{
"keyData": "${KEY_DATA}"
}
]
}
},
"servicePrincipalProfile": {
"clientId": "${CLIENT_ID}",
"secret": "${SECRET}"
}
}
}
data "template_file" "init" {
template = "${file("./example.tpl")}"
vars {
KEY_DATA = "key_data"
CLIENT_ID = "client_id"
SECRET = "secret"
}
}
variable "output_file_name" {
default = "result.json"
}
resource "null_resource" "test" {
triggers {
test = "${data.template_file.init.rendered}"
}
provisioner "local-exec" {
command = "${format("cat <<\"EOF\" > \"%s\"\n%s\nEOF", var.output_file_name, data.template_file.init.rendered)}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment