2015-05-08 03:20:05 +02:00
|
|
|
---
|
|
|
|
layout: "template"
|
|
|
|
page_title: "Template: template_file"
|
2016-05-22 00:09:55 +02:00
|
|
|
sidebar_current: "docs-template-datasource-file"
|
2015-05-08 03:20:05 +02:00
|
|
|
description: |-
|
|
|
|
Renders a template from a file.
|
|
|
|
---
|
|
|
|
|
|
|
|
# template\_file
|
|
|
|
|
|
|
|
Renders a template from a file.
|
|
|
|
|
|
|
|
## Example Usage
|
|
|
|
|
2017-01-09 09:36:16 +01:00
|
|
|
Option 1: From a file:
|
|
|
|
|
|
|
|
Reference the template path:
|
2016-11-07 17:42:46 +01:00
|
|
|
|
2015-05-08 03:20:05 +02:00
|
|
|
```
|
2016-05-22 00:09:55 +02:00
|
|
|
data "template_file" "init" {
|
2015-11-17 23:45:08 +01:00
|
|
|
template = "${file("${path.module}/init.tpl")}"
|
2015-05-08 03:20:05 +02:00
|
|
|
|
|
|
|
vars {
|
|
|
|
consul_address = "${aws_instance.consul.private_ip}"
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 17:42:46 +01:00
|
|
|
```
|
|
|
|
|
2017-01-09 09:36:16 +01:00
|
|
|
Inside the file, reference the variable as such:
|
|
|
|
|
|
|
|
```
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
echo "CONSUL_ADDRESS = ${consul_address}" > /tmp/iplist
|
|
|
|
```
|
|
|
|
|
|
|
|
Option 2: Inline:
|
2015-05-08 03:20:05 +02:00
|
|
|
|
2016-11-07 17:42:46 +01:00
|
|
|
```
|
|
|
|
data "template_file" "init" {
|
|
|
|
template = "$${consul_address}:1234"
|
|
|
|
|
|
|
|
vars {
|
|
|
|
consul_address = "${aws_instance.consul.private_ip}"
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 03:20:05 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## Argument Reference
|
|
|
|
|
|
|
|
The following arguments are supported:
|
|
|
|
|
2015-11-13 18:07:02 +01:00
|
|
|
* `template` - (Required) The contents of the template. These can be loaded
|
|
|
|
from a file on disk using the [`file()` interpolation
|
|
|
|
function](/docs/configuration/interpolation.html#file_path_).
|
2015-05-08 03:20:05 +02:00
|
|
|
|
2016-07-11 22:39:34 +02:00
|
|
|
* `vars` - (Optional) Variables for interpolation within the template. Note
|
|
|
|
that variables must all be primitives. Direct references to lists or maps
|
|
|
|
will cause a validation error.
|
2015-05-08 03:20:05 +02:00
|
|
|
|
2015-11-13 18:07:02 +01:00
|
|
|
The following arguments are maintained for backwards compatibility and may be
|
|
|
|
removed in a future version:
|
|
|
|
|
2016-06-24 16:53:21 +02:00
|
|
|
* `filename` - _Deprecated, please use `template` instead_. The filename for
|
2015-11-13 18:07:02 +01:00
|
|
|
the template. Use [path variables](/docs/configuration/interpolation.html#path-variables) to make
|
|
|
|
this path relative to different path roots.
|
|
|
|
|
2015-05-08 03:20:05 +02:00
|
|
|
## Attributes Reference
|
|
|
|
|
|
|
|
The following attributes are exported:
|
|
|
|
|
2015-11-13 18:07:02 +01:00
|
|
|
* `template` - See Argument Reference above.
|
2015-05-08 03:20:05 +02:00
|
|
|
* `vars` - See Argument Reference above.
|
|
|
|
* `rendered` - The final rendered template.
|
|
|
|
|
2016-11-07 17:42:46 +01:00
|
|
|
## Template Syntax
|
|
|
|
|
|
|
|
The syntax of the template files is the same as
|
|
|
|
[standard interpolation syntax](/docs/configuration/interpolation.html),
|
|
|
|
but you only have access to the variables defined in the `vars` section.
|
|
|
|
|
|
|
|
To access interpolations that are normally available to Terraform
|
|
|
|
configuration (such as other variables, resource attributes, module
|
|
|
|
outputs, etc.) you'll have to expose them via `vars` as shown below:
|
|
|
|
|
|
|
|
```
|
|
|
|
data "template_file" "init" {
|
|
|
|
# ...
|
|
|
|
|
|
|
|
vars {
|
|
|
|
foo = "${var.foo}"
|
|
|
|
attr = "${aws_instance.foo.private_ip}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Inline Templates
|
|
|
|
|
|
|
|
Inline templates allow you to specify the template string inline without
|
|
|
|
loading a file. An example is shown below:
|
|
|
|
|
|
|
|
```
|
|
|
|
data "template_file" "init" {
|
|
|
|
template = "$${consul_address}:1234"
|
|
|
|
|
|
|
|
vars {
|
|
|
|
consul_address = "${aws_instance.consul.private_ip}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
-> **Important:** Template variables in an inline template (such as
|
|
|
|
`consul_address` above) must be escaped with a double-`$`. Unescaped
|
|
|
|
interpolations will be processed by Terraform normally prior to executing
|
|
|
|
the template.
|
|
|
|
|
|
|
|
An example of mixing escaped and non-escaped interpolations in a template:
|
|
|
|
|
|
|
|
```
|
|
|
|
variable "port" { default = 80 }
|
|
|
|
|
|
|
|
data "template_file" "init" {
|
|
|
|
template = "$${foo}:${var.port}"
|
|
|
|
|
|
|
|
vars {
|
|
|
|
foo = "${count.index}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In the above example, the template is processed by Terraform first to
|
|
|
|
turn it into: `$${foo}:80`. After that, the template is processed as a
|
|
|
|
template to interpolate `foo`.
|
2015-05-27 12:22:52 +02:00
|
|
|
|
2016-11-07 17:42:46 +01:00
|
|
|
In general, you should use template variables in the `vars` block and try
|
|
|
|
not to mix interpolations. This keeps it understandable and has the benefit
|
|
|
|
that you don't have to change anything to switch your template to a file.
|